PART
I
Ch-
4
Wide Char:
Wide Char is an enhanced
type of the simple char data type. Where the wide char is capable to
hold some chararacter which does not come under ASCII format. (e.g.
some kind of Japaneese characters).
The size of the wide
char is 4 bytes.
Syntax: wchar_t <var
name>
Bool datatype:
Bool data type is
capable to hold both true as well as false. Its similar to C.
Ch-5:
String datatype:
String is a dataype in
cpp which is capable to hold the strings directly, which reduces
the complexity of declaration of arrays. Also declaration of array of
any data type is allowed as well.
syntax: string
<var name>
example: string arr
ch- 8:
Control
Statements:
It can
classified into two types
a) conditional control statements
example:for, while, do-while
and
b)non conditional conrol statements
example: break,continue, goto
Note: 1)The body of
for loop is simple and at one place we can find
intialization,conditon check , ++/- - when compared with while
loop.
2)Difference
between while and do-while is that ,the do-while execute the
statements at least once where as while is based on the condition
of the statement.
3)Switch
statement is similar to nested if statement . For code optimaztion
prefer switch within another switch statement.(for single switch
statement traversing the condition will take more).
ch-9:
scope:The
scope of a variable is the area of program ,where variable is valid.
Broadly scope classified into different categeories
i)Block
scope
ii)Function
scope
iii)File
scope
Purpose: For hiding the data ,reuse of variable
Storage
class: It is of two
types. a)storage class specifier b)storage class modifiers
Storage
class specifies where to allocate memory for variables i.e
heap, stack, register etc.
The storage class specifiers are auto, extern ,
static ,global
Note:The extern and static can be applied for
functions. By default function is extern.
Stroage class modifiers(qualifiers) specifies the
compiler how to access these variables
The
storage class qualifiers are const ,volatile.
Namespace:
A namespace
is an abstract container or environment created to hold a logical
grouping of unique identifier or symbols (i.e., names).
The namespace should be access
by using the command given below
using namespace
Name_of_Namespace;
The other way access name space
is using the ::(scope resolution operator)
i.e
name_of_namespace::variable_name;
Namespace syntax:
namespace
namespace_name
{
//body of namespace
};
Namespace alias:
namespace
namesapce_name=alias_name;
Note: A namespace without identifer is know
as unnamed namespace
Part-III:
ch-12:
Struct:
It is collection of disimilar elements. It of two types a)packed
structure(bit members) and b)un packed structure
Example for packed struct:
struct item
{
unsigned int list:1; // True if item is in the
list
unsigned int seen:1; // True if this item has
been seen
unsigned int number:14; // Item number
};
Example of unpacked struct:
struct item
{
int a;
float b;
};
Note:In c++ struct also includes
member functions which by default public.
Union:It
is similar to struct. Single memory location can be given to
different member variables i.e single copy of memory
for different variables.
ch-13:
Class:
Class is a collection of member functions and member variables.
By default the members of class
are private.
Syntax:
class class_name
{
//members
vraibales and functions
};
Ch-14:
Friend
function and class:
Friend
fuction:
A function which is not member of class but need to access the
private data of that class. Then this function should be declared
as friend to that class.
So that it can access the private date members.
Syntax:
class student
{
int m1,m2,m3;
public:
get_mark();//input
m1,m2, m3
friend void avg(
student s) ;
};
void
avg(student s )
{
int avg=(s.m1+s.m2+s.m3)/3; //here u r accessing
the private members of a class
}
int main()
{
student st;
st.get_mark();
avg( st);
}
Friend class: In order to access the
private data members of main class in external class ,we need
to declare external class as friend in the main class
Example:
class secret
{
int a;
public:
friend class lok; //friend for class lok
};
class lok
{
secret s;
public:
lok(int x) // constructor
{ s.a = x; }
void printout()
{ cout << s.a << endl; }
};
main()
{
lok t(4);
t.printout();
}
const data member and functions:
const function:
The const keyword is used for function to declare it as a const
function so that the variables within this function can't be
modified.
Syntax and example:
class test
{
public:
int set_const()const; // A read-only functiot
void get_data( int mn ); // A write function;
can't be const
private:
int month;
};
int test::set_const() const
{
return month; // Doesn't modify anything
}
void test::get_data( int mn )
{
month = mn; // Modifies data member
}
int main()
{
int n;
test t;
t.get_data(12) ; // Okay
n= t.set_const() ;// Okay
t.get_data(34); // Error ,it is
const
}
Note: const can't overload
constructors and destructors
const
data members:
In side the class ,class
data members can be declared but can't be intialized
so they should be initilized
either out side the class (global) or with different
syntax for constructor in that class
shown below.
const int data_size = 1024; //global initlization
class data_list {
public:
const int data_size; // Number of items in
the list
// ... rest of the class
};
[
or
]
class data_list {
public:
const int data_size;
data_list( ) : data_size(1024) { //using
constructor
}
};
Static
data members and functions:
static
data members:In
classes static data members can be declared but can't be
initialized. Ther is special sytax to initalize static variables is
as shown below
int stack::stack_count = 0; //intialization
class stack {
private:
static int stack_count; //declaration
int count;
};
Static
member function:Static
member functions only access only static member data and static
member function.Static member functions are very limited. They
can't access nonstatic member variables or functions in the class
example:
class test
{
static int count;
int value;
public:
static int test_count()
{
value++ ; // invalid because non static member
count++;
cout<<"count increment="<<count<<endl;
return count;
} ;
int test::count=0;
ch-15:
const
pointer:
A const pointer is the pointer whose address can't be changed. The
given example simplifies this
char
a[10]=”msys” , b[10]=”Tech”
char *const
ptr=a;
ptr=b; //error u r modifing the address stored in
it which is illegal
*ptr=”Tech”; // here u r not changing the
address but put the value in that address
Note:1) on pointer variables
,we can't perform multification and divsion
2)pointer variables can't be added or subtracted
i.e int *ptr1, *ptr2, *ptr;
ptr=ptr1+ptr2; //illegal operations
ptr=ptr1-ptr2; //illegal operations
3)pointer variable addition or subtraction with
numbers(int i=3,char c=-2) is possible if the number is valid
one.
4)Using more pointers concepts leads to programm
optimization and performance in terms of speed.
Reinterpret_cast:
Reinterpret_cast converts any pointer type to any other pointer
type, even of unrelated classes. With this casting bit values will
not be changed where as static cast bit values is will be changed.
syntax:
classA {};
classB {};
A * a =
newA;
B * b =
reinterpret_cast<B*>(a);
ch-16:
Predefined I/O
class varibles:
VARIABLE USE
cin console input
(standard input)
cout console output
(standard output)
cerr console error
(standard error)
clog console log
End of line:
In C++, end of line can
be done by "\n" and also it supports a new type i.e.
"<<endl"
example:
cout<<"Welcome
to Cpp"<<endl;
printing
to stdout:
In
cpp we can print a data stream to the standard out (console) by using
the function “cout” and the overloaded operator “<<”.
Example:
cout<<”Welcome
to cpp\n”;
getting
input through standard input:
like
the printing to standard out is quiet differnt from C language,
taking input is also the same.
So
the input can be given using “cin” and “>>”
Example:
cin>>i; //will wait to take the input for the
variable i
Files:A file is a collection of related
data. C++ treats a file as a series of bytes.
They are two type of
operation , a)Binary and b)ASCII
The different modes of operation on the files
are a)ostream b)istream and c)fstream
The file operations on files
includes
a)open file
b)read file
c)write file
d)close file etc.
Example:
int main()
{
string
bufw,bufr;
fstream myfile(“file_name.txt” ,
ios::in,ios::out)
cout<<”enter the string”<<endl;
cin>>bufw>>endl;
myfile<<bufw;
myfile>>bufr;
cout<<”read the data from file”<<bufr<<end;
myfile.close();
}
ch-18:
- Operator over loading is a new concept invented for OOPs
- In case of object, if we need to make some mathematical operations (e.g. Addition, substraction etc etc) with the given objects thn we can't do those operations simply by using the operators.
- For the we need overload the perticular operator.
- Syntax:
return_type class_name::operator symbol (arguments)example:
void Simple::operator+(int)
ch-20:
Keyword new:
it
returns the starting address of the memory on success
Keyword
Delete:
This
key word is used to delete the dynamic allocated memory.
(new
and delete are same as malloc and free respectively as in C)
Part-IV:
ch-21:
Derived
class:It
is the sub class which inherits some/all the properites of base
class.
Syntax: base
class
{
//data
members
public:
//member functions
};
class sub_class :public base class {
};
Purpose:Resuse of code
Virtual
Function:
A virtual function or virtual method is a function whose behaviour
can be overridden within an inheriting class by a function with
the same signature.It supports the polymorphism.
Example:
class base
{
public:
virtual void display()
{
cout<<"\nBase";
}
};
class derived : public base
{
public:
void display()
{
cout<<"\nDerived";
}
};
int main()
{
base *ptr = new derived();
ptr->display();
}
Note:1)A class with at least one pure virtual function
is called abstract class.
{
protected:
virtualvoid draw() = 0;
//pure virtual function }
Virtual
class:A
virtual class is an inner classs that can be overridden by subclass
of the outer class
class room {
// ....
};
class garage_part : virtual public room {
// ....
};
class office_part : virtual public room {
// ....
};
public small_repair_shop: public office_part,
garage_part
{
// ...
};
Function
hiding:
A derived/base class may hide a member function declared in its
base/derived class by using the same function name, but with a
different signature, or list of arguments.
Example:
class simple {
public:
int do_it(int i, int j) { return (i*j); }
float do_it(float f) { return (f*2);}
};
class derived: public simple {
public:
int do_it(int i, int j) { return (i+j); }
};
int main( )
{
derived test; // Define a class for our
testing
int i; // Test variable
float f; // Test variable
i = test.do_it(1, 3); // Legal; returns 4 (1 +
3)
// f = test.do_it(4.0); // Illegal;
"do_it(float)" not defined in
} // the class "derived"
constructor
and Destructor:
define
constructor:A
function name same as the class name then it is a constructor.
Contructor will not have return type.
Constructor can be overloaded with different signature.
Types: a)Default constructor
b)Paramertized constructor
c)Copy consturctor
Note:1)Constructors and destructors behave differently
from normal member functions, especially when used with derived
classes. When a derived-class variable is created, the constructor
for the base class is called first, followed by the constructor
for the derived class.
2)If no constructor is defined ,c++ invokes a
default constructor, which allocates memory for the object but
does not intialize.
Syntax:
class msys
{
msys() //default constuctor
{
// fun'c def.
}
msys(int
a,int b) //parameterized constructor
{
//fun'c def.
}
msys(msys&) //copy constructor
{
//def.
};
Destructor:
It is similar to destructor but proceeded with ~(tilde charcter).
Syntax:
class msys
{
~msys()
{..//}
};
Note:1)The hierarchical calling method for
constructor is from base to derived class.
2)The hierarchical calling
mehtod for destructor is from derived to base class.
But this hierarchical can be changed by using
virtual destructor.
Dynamic_cast
Operator:The
dynamic_cast operator can be used to change a pointer or
reference to a base class to that of the derived class and vice
versa.
Example:
// Correct and safe
conversion -- no problem
office *office_ptr
= dynamic_cast<office *>(ptr);
// Incorrect
conversion -- throws an exception
garage *other_ptr =
dynamic_cast<office *>(ptr);
part-V:
ch-22:
Exceptions:
- This is to get executed a single option out of available multiple options.
- When the other options will not satisfy the conditions than the perticular option that satisfy the condition will be executed.
- All the opetions will be kept in separate blocks.
- Those blocks are of 3 types generally.
- They are Try block, Throw block and Catch block.
- Try block will get the input statement.
- If the condition does not satisfy than the Throw block which is kept inside the try block will throw the Exception to the immediate next Catch block.
- The Catch block will check with the conditions and decids wheather to execute or it can also pass it to be executed throw another such set of blocks.
- Multiple Try, Throw, Catch blocks can be kept inside a program.
ch-24:
Templates: It is
a Generic function for the modules.
Types: a)class
templates
b)Function templates
class
templates: class templates is similar to class but defination is
prefixed with template keyword. It is shown below,
template <class T>
class msys
{
T a,b;
msys( T c)
{
a=c;
}
};
Function template:
It is a generic funtion.
Syntax:
template<class T>
T fun_name(T a,T b)
{
T temp;
return temp=a+b;
}
STL
(Standard Template Library):
- STL is a Designed Template which is capable enough to handle the program in some easier way as most of the required functions are already implemented.
- Those fuctions again are combined to create a module.
- The main modules of a STL can be as:
- Containers
- Iterators
- Algorithms
- Again the sub modules available inside each modules are:
Containers:
- Vector: A Random access sequential container. It looks like an array, but it can be expanded by inserting elements and datas can also be removed.
- Dequeue: Similar to a vector but it is faster at inserting and deleting elements.
- List: A doubly linked list.
- Set: A set of items which are unordered and unique.
- Multiset: Permits multiple items with same value.
- Map: This is a container whose values can be looked up by a key. One value will be stored for each key.
- Multimap: Multiple values can be stored for a single key.
Iterators:Iterators allow to go through a container and access
the data inside.
- Forward iterator
- Reverse iterator
Algorithms:
Performs the various algorithms like sorting
mechanisms and all.
- Find: Finds an item in acontaner.
- Count: Counts the number of items in a container.
- Equal: Checks wheather the containers are equal or not.
- For_each: Runs each element of a container through a given function.
- Copy: Copies a container.
- Reverse: Reverses the order of the elements in the container.