C++ Notes
C++ Notes
C++ Notes
Introduction:
C++ is a powerful computer Object Oriented programming language. C++ is an advance version of C language or
we can say that C++ is an incremented form of C language or we can also say that C++ is a super set of C
language because C++ is the ability to perform all the operation of structured programming language C that is (if,
loops, function, array etc) as well as C++ is the ability to perform the advance feature of Object Oriented
Programming language (OOP) that is (Class, Object, inheritance, data encapsulation and polymorphism) etc.
History:
In 1972 Dennis Ritche developed C language at AT and Ts bell Laboratories of USA. C is a very
powerful language but the major drawbacks of C language are Data security and Data hiding, to remove
these drawbacks an advance version of C comes to existence known as C++.
C++ developed by Bjarne stroustrup in 1980s at Bell laboratories new jursey USA.
It first name was c with classes but later on in 1983 a new name was given C++.
void display(void)
{
cout<<"Hello";
}
5) Prototype Of A Function:
In C++ prototype of each and every function must be defined before using/calling a function. If the
prototype is not known then the file containing the prototype of the function must be included in the
program using #include directive.
For user defined functions if the function is defined before the main( ) function then writing its
prototype is optional.
For example:
#include<conio.h>
#include<iostream.h>
void display(void)
{
cout<<"Hello";
}
void main()
{
clrscr();
display();
getch();
}
If a function is defined after main( ) function then its prototype must be declared before main( )
function.
For example:
#include<conio.h>
#include<iostream.h>
void display(void);
void main()
{
clrscr();
display();
getch();
}
void display(void)
{
cout<<"Hello";
}
6) STRUCTURES:
In C, structures were used to make user-defined data types. In C a structure do
not have member functions. In the general syntax of making a structure variable you
must use struct keyword. But in C++ a structure may contain function members
along with data members and the struct keyword is optional in the syntax for declaring
a structure variable. In C once you have declared a variable of a structure, you can
gain access to its data elements, but this is not the case in C++, you cannot access
private data members of a structure.
For example:
#include<conio.h>
#include<iostream.h>
void main()
{
struct student
{
int id;
char *name;
float per;
void stud(void)
{
id=10;
name="Imran";
per=85.50;
cout<<id<<endl<<name<<endl<<per;
}
};
student stu;
clrscr();
stu.stud();
getch();
}
Function in C++:
Function is a self contain block of statements that perform some specific task.
For example:
#include<conio.h>
#include<iostream.h>
void fun(void);
void main()
{
clrscr();
fun();
getch();
}
void fun(void)
{
cout<<"This is function fun";
}
Arguments passing by Value:
Here is a simple example to pass arguments by value in c++.
For example:
#include<conio.h>
#include<iostream.h>
void sum(int,int);
void main()
{
int a=10,b=20;
clrscr();
sum(a,b);
getch();
}
void sum(int x, int y)
{
cout<<x+y;
}
Arguments passing by Address:
Here is a simple example to pass arguments by address in c++.
For example:
#include<conio.h>
#include<iostream.h>
void sum(int*,int*);
void main()
{
int a=10,b=20;
clrscr();
sum(&a,&b);
getch();
}
void sum(int *x, int *y)
{
cout<<*x+*y;
}
Arguments passing by Reference:
Here is a simple example to pass arguments by reference in c++.
For example:
#include<conio.h>
#include<iostream.h>
void sum(int&,int&);
void main()
{
int a=10,b=20;
clrscr();
sum(a,b);
getch();
}
void sum(int &x, int &y)
{
cout<<x+y;
}
Inline Function:
Inline function is a function that works just like #define directive but with a little difference that the compiler takes
decisions either to replace the inline function body with the calling statement or to pass the control to the
function.
Inline function has to define before the main function using a key word inline before the declarator.
For example:
#include<conio.h>
#include<iostream.h>
inline void sum(void)
{
cout<<"this inline function called";
}
void main()
{
clrscr();
sum();
getch();
}
we can also pass arguments to the inline function and return values from inline function like a simple function.
For example:
#include<conio.h>
#include<iostream.h>
inline int sum(int y, int z)
{
return(y+z);
}
void main()
{
int a=10,b=20,res;
clrscr();
res=sum(a,b);
cout<<res;
getch();
}
Default Arguments:
Default arguments are those arguments, which are initialize during the function prototype.
Always remember that the optional argument comes from right end neither in middle nor at left end.
For example:
#include<conio.h>
#include<iostream.h>
void def(int=500,int=500);
void main()
{
clrscr();
def(50,100);
def(250);
def();
getch();
}
void def(int a,int b)
{
cout<<a+b<<endl;
}
Example2:
#include<conio.h>
#include<iostream.h>
void def(char='*',char []="pakistan",int=5);
void main()
{
clrscr();
def('+',"iran",2);
def('=',"afghan");
def();
getch();
}
void def(char a,char b[15],int n)
{
for(int i=1;i<=n;i++)
{
cout<<a<<b<<endl;
}
}
Class:
Like structure class is also used to create user define data types. In class we can define both data-items and
functions. The function of the class is used to perform operation on the data-items.
The data items of the class are known as data-members and the functions of the class are known as member-
functions.
When a class is defined, it does not occupy any space in the computer memory. It only defines the data-members
and member-function of the class.
Syntax:
Syntax of class is similar as that of a structure. The key word class is used to define a class.
The general syntax of a class is.
Class class-name
{
Body of the class;
};
Object:
The class does not perform any operation by its self there fore we declare a variable for a class so this
variable of class is known as object.
When we declare an object of a class then it reserve space in computer memory for the members of that
class.
Syntax:
The syntax for declaring objects of a class is similar as that of declaring variables of any data-type
or structure.
Class-name object1, object2;
Or
Class class-name object1, object2;
Here the class keyword is optional.
Encapsulation:
To declare both data-members and member-function in one unit (means class) then this process is called
encapsulation.
void main()
{
clrscr();
first obj;
obj.fun();
getch();
}
void first::fun(void)
{
a=10,b=20;
cout<<a<<endl<<b;
}
Objects & Memory Model:
When an object of the class is created then space is reserved for that object in memory to hold its data
members. Note that every object of a class reserve separate space in memory for their class data members.
But the member functions of a class are store at only one place and all objects of the class share the
member functions of the class.
For example:
#include<conio.h>
#include<iostream.h>
class temp
{
private:
int a;
float b;
public:
void getdata(void)
{
cout<<"Enter value for a"; cin>>a;
cout<<"Enter value for b"; cin>>b;
}
void showdata(void)
{
cout<<"Value of a is ="<<a<<endl;
cout<<"Value of b is ="<<b<<endl;
}
};
void main()
{
temp ob1,ob2;
clrscr();
cout<<"Enter data for object 1"<<endl;
ob1.getdata();
cout<<"Enter data for object 2"<<endl;
ob2.getdata();
cout<<endl;
cout<<"Data of object 1"<<endl;
ob1.showdata();
cout<<"Data of object 2"<<endl;
ob2.showdata();
getch();
}
The memory map of the above program is as follows:
ob1 ob2
a a
b b
getdata
showdata
Constructor:
A constructor is a member function of a class that is called and executed automatically when an object of
that class is created.
Constructor name must be same as that of the class.
Constructor may have an arguments but it cannot return any value.
For example:
#include<conio.h>
#include<iostream.h>
class temp
{
public:
temp(void)
{
cout<<"This is constructor called"<<endl;
}
};
void main()
{
clrscr();
temp ob1,ob2;
getch();
}
Initializing data members using Constructors:
Constructors are normally used to initialize values in the data members of a class when the program is
executed. This type of initialization is called automatic initialization.
For example:
#include<conio.h>
#include<iostream.h>
class sum
{
private:
int a,b,s;
public:
sum(int x,int y)
{
s=x+y;
}
void printsum(void)
{
cout<<s<<endl;
}
};
void main()
{
clrscr();
sum ob1(10,20),ob2(100,200);
ob1.printsum();
ob2.printsum();
getch();
}
Constructor Overloading:
When we define more that one constructor with different arguments is known as constructor
overloading. Constructor overloading is same as that of function overloading.
For example:
#include<conio.h>
#include<iostream.h>
class sum
{
public:
sum(int x,int y)
{
cout<<"Sum of two values is="<<(x+y)<<endl;
}
sum(int x,int y,int z)
{
cout<<"Sum of three vlaues is="<<(x+y+z)<<endl;
}
};
void main()
{
clrscr();
sum ob1(10,20),ob2(100,200,300);
getch();
}
Destructors:
Destructor is a function of a class that is automatically executed when an object of the class comes to the
end of its life. The destructor function is the same name as that of the class but a tilde sign (~) is written
before its name.
Destructors do not return any value and do not take any arguments. The destructor function is
commonly used to free the space that was reserved by an object.
For example:
#include<conio.h>
#include<iostream.h>
class cd
{
public:
cd()
{
cout<<"this is constructor"<<endl;
}
~cd()
{
cout<<"this is destructor"<<endl;
getch();
}
};
void main()
{
clrscr();
cd ob1;
getch();
}
Passing Objects as Arguments:
Objects can also be passed as arguments to member functions like passing simple arguments to a function.
Fro example:
#include<conio.h>
#include<iostream.h>
class abc
{
private:
int id;
char *name;
public:
void get(void)
{
cin>>id;
cin>>name;
}
void show(abc obj1)
{
cout<<obj1.id<<obj1.name;
}
};
void main()
{
abc obj1,obj2;
clrscr();
obj1.get();
obj2.show(obj1);
getch();
}
Friend Function:
A friend function is not a member function it is an outsider function, which can access the private
and protected members of the class to which it has been a friend.
The main purpose of a friend function is to act as a bridge between different classes and the
other purpose is to access the private and protected members from out side of a class.
To create a friend function declare a function prototype with keyword (Friend) at the start
and define the body of the friend function out side a class with out keyword friend and SRO (::).
Note:
1) We can call a friend function like simple function with out using any object of
the class because it is not a member function of a class it is only a friend of a class.
2) No matter if the friend function prototype is declare as private or public or
protected.
3) We can create any number of friend functions.
4) We can also over load friend function.
Syntax:
Class
{
friend return-type function-name(arguments);
};
void main()
{
}
void function()// this is friend function definition.
{
body;
}
Example 1:
Here is a simple example of Friend function. Here we use four friend functions and also pass
argument in one function and also use function overloading and return value in friend function.
#include<conio.h>
#include<iostream.h>
class abc
{
private:
friend void frf1();
public:
friend void frf2(int);
friend int frf2(void);
protected:
friend void frf3();
};
void main()
{
clrscr();
frf1();
frf2(10);
cout<<frf2()<<endl;
frf3();
getch();
}
void frf1(void)
{
cout<<"friend Fuction frf1"<<endl;
}
void frf2(int a)
{
int b;
b=a;
cout<<"Friend Function frf2 ";
cout<<"value of a is="<<b<<endl;
}
int frf2(void)
{
return(100);
}
void frf3(void)
{
cout<<"Friend function frf3"<<endl;
}
Example2:
Here we access the private data members of a class through friend function.
#include<conio.h>
#include<iostream.h>
class abc
{
private:
int id;
char *name;
friend void frf(abc);
};
void main()
{
abc obj;
clrscr();s
frf(obj);
getch();
}
void frf(abc obj)
{
obj.id=20;
obj.name="imran";
cout<<obj.id;
cout<<obj.name;
}
For example 3:
In this example a friend function work as a bridge between two classes.
#include<conio.h>
#include<iostream.h>
class xyz;
class abc
{
private:
int a,b;
public:
abc()
{
a=10,b=20;
}
friend int frf(abc,xyz);
};
class xyz
{
private:
int x,y;
public:
xyz()
{
x=100,y=200;
}
friend int frf(abc,xyz);
};
void main()
{
clrscr();
abc obj1;
xyz obj2;
cout<<frf(obj1,obj2);
getch();
}
int frf(abc ab,xyz xy)
{
return(ab.a + ab.b + xy.x + xy.y);
}
Inheritance:
Inheritance is an other most important and powerful feature of object-oriented programming. Inheritance
is the process of creating new class called derived class or child class, from existing class called base class or
child class. Inheritance allows us code reusability. Reusing existing code saves time and increases a programs
reliability.
There are two category of inheritance
1) Single Inheritance.
2) Multiple inheritance
1) Single inheritance:
Single inheritance is a type of inheritance in which the new class is derived from only one base
class.
Syntax:
Class base_class_name
{
body;
};
class derived_class_name : access specifier base_class_name
{
body;
};
void main()
{
body;
}
Type of Inheritance:
There are three types of inheritance
1) Public Inheritance
2) Private Inheritance
3) Protected inheritance
1) Public Inheritance
In public inheritance the members of the base class that can access by the derived class and its
objects are as follows.
1) Public members:
Public members of the base class can access from with in the derived class.
Public members of the base class also access through the object of the derived class.
2) Protected members:
Protected members of the base class can access from with in the derived class.
Protected members of the base class cannot access through the object of the derived
class
3) Private members:
Private members of the base class cannot access from with in the derived class.
Private members of the base class cannot access through the object of the derived
class.
For example:
#include<conio.h>
#include<iostream.h>
class parent
{
public:
void pub(void)
{
cout<<"\tthis is public function"<<endl;
}
protected:
void pro(void)
{
cout<<"\tthis is protected function"<<endl;
}
private:
void pri(void)
{
cout<<"\tthis is private function";
}
};
class child : public parent
{
public:
void ch_fun(void)
{
cout<<endl<<"Accessing through Class\n";
pub();
pro();
};
void main()
{
child obj;
clrscr();
obj.ch_fun();
cout<<"Now accessing through object"<<endl;
obj.pub();
//obj.pro();
//obj.pri();
getch();
}
2) Protected Inheritance
In protected inheritance the members of the base class that can access by the derived class and its
objects are as follows.
1) Public members:
Public members of the base class can access from with in the derived class.
Public members of the base class cannot access through the object of the derived
class.
2) Protected members:
Protected members of the base class can access from with in the derived class.
Protected members of the base class cannot access through the object of the derived
class
3) Private members:
Private members of the base class cannot access from with in the derived class.
Private members of the base class cannot access through the object of the derived
class.
For example:
#include<conio.h>
#include<iostream.h>
class parent
{
public:
void pub(void)
{
cout<<"\tthis is public function"<<endl;
}
protected:
void pro(void)
{
cout<<"\tthis is protected function"<<endl;
}
private:
void pri(void)
{
cout<<"\tthis is private function";
}
};
class child : protected parent
{
public:
void ch_fun(void)
{
cout<<endl<<"Accessing through Class\n";
pub();
pro();
};
void main()
{
child obj;
clrscr();
obj.ch_fun();
cout<<"Accessing through object"<<endl;
//obj.pub();
//obj.pro();
//obj.pri();
getch();
}
3) Private Inheritance
In private inheritance the members of the base class that can access by the derived class and its
objects are as follows.
1) Public members:
Public members of the base class can access from with in the derived class.
Public members of the base class cannot access through the object of the derived
class.
2) Protected members:
Protected members of the base class can access from with in the derived class.
Protected members of the base class cannot access through the object of the derived
class
3) Private members:
Private members of the base class cannot access from with in the derived class.
Private members of the base class cannot access through the object of the derived
class.
For example:
#include<conio.h>
#include<iostream.h>
class parent
{
public:
void pub(void)
{
cout<<"\tthis is public function"<<endl;
}
protected:
void pro(void)
{
cout<<"\tthis is protected function"<<endl;
}
private:
void pri(void)
{
cout<<"\tthis is private function";
}
};
class child : private parent
{
public:
void ch_fun(void)
{
cout<<endl<<"Accessing through Class\n";
pub();
pro();
}
};
void main()
{
child obj;
clrscr();
obj.ch_fun();
cout<<"Accessing through object"<<endl;
//obj.pub();
//obj.pro();
//obj.pri();
getch();
}
Example:
Simple program of public inheritance in which we access members of base class through object of derived
class.
#include<conio.h>
#include<iostream.h>
#include<stdio.h>
class student
{
private:
char name[20];
char add[50];
public:
void getstu(void)
{
cout<<"Enter Name="; gets(name);
cout<<"Enter Address="; gets(add);
}
void showstu(void)
{
puts(name);
puts(add);
}
};
class marks:public student
{
private:
int c,vb,java,tot;
public:
void getmarks(void)
{
cout<<"Enter Marks for C="; cin>>c;
cout<<"Enter Marks for C++="; cin>>vb;
cout<<"Enter Marks for java=";cin>>java;
}
void showmarks(void)
{
cout<<"------------------------\n";
showstu();
cout<<"Marks of C="<<c<<endl;
cout<<"Marks of VB="<<vb<<endl;
cout<<"Marks of java="<<java<<endl;
tot=c+vb+java;
cout<<"Total marks ="<<tot;
}
};
void main()
{
clrscr();
marks ob;
ob.getstu();
ob.getmarks();
ob.showmarks();
getch();
}
2) Multiple inheritance:
In multiple inheritance, a class is derived from more then one base classes. In multiple inheritance the
derived class can access members of more then one class.
Syntax:
Class derived_class_name : access specifier base_class1_name , access specifier base_class2_name
{
body;
};
For example:
#include<conio.h>
#include<iostream.h>
#include<stdio.h>
class student
{
private:
char name[20];
char add[50];
public:
void getstu(void)
{
cout<<"Enter Name="; gets(name);
cout<<"Enter Address="; gets(add);
}
void showstu(void)
{
puts(name);
puts(add);
}
};
class marks
{
private:
int c,vb,tot;
public:
void getmarks(void)
{
cout<<"Enter Marks for C="; cin>>c;
cout<<"Enter Marks for C++="; cin>>vb;
}
void showmarks(void)
{
cout<<"Marks of C="<<c<<endl;
cout<<"Marks of VB="<<vb<<endl;
tot=c+vb;
cout<<"Total marks ="<<tot;
}
};
class show : public student,public marks
{
public:
void getrec(void)
{
getstu();
getmarks();
}
void showrec(void)
{
cout<<"------------------------\n";
showstu();
showmarks();
}
};
void main()
{
clrscr();
show ob;
ob.getrec();
ob.showrec();
getch();
}
Function Overriding:
The process of defining more then one function with the same name in different classes are know as
function overriding.
We can access the same function of base class from the derived class object with the help of
SRO(::).
For example:
#include<conio.h>
#include<iostream.h>
class parent
{
private:
int a,b;
public:
void showrec()
{
a=10;b=20;
cout<<"Parnet Class"<<endl;
cout<<"Value of A="<<a<<endl;
cout<<"value of B="<<b<<endl;
}
};
class child:public parent
{
private:
int x,y;
public:
void showrec()
{
x=10;y=20;
cout<<"Child Class"<<endl;
cout<<"Value of x="<<x<<endl;
cout<<"value of y="<<y<<endl;
}
};
void main()
{
child ob;
clrscr();
ob.showrec();
ob.parent::showrec();
getch();
}
Note:
The statement ( ob.showrec() ) in the above example generate error in multiple inheritance.
Example of Function overriding in multiple inheritance:
#include<conio.h>
#include<iostream.h>
class parent
{
private:
int a,b;
public:
void showrec()
{
a=10;b=20;
cout<<"Parnet Class"<<endl;
cout<<"Value of A="<<a<<endl;
cout<<"value of B="<<b<<endl;
}
};
class child
{
private:
int x,y;
public:
void showrec()
{
x=10;y=20;
cout<<"Child Class"<<endl;
cout<<"Value of x="<<x<<endl;
cout<<"value of y="<<y<<endl;
}
};
class grandchild: public parent,public child
{
};
void main()
{
grandchild ob;
clrscr();
// ob.showrec(); generate Error Message.
ob.child::showrec();
ob.parent::showrec();
getch();
}
};
void main()
{
emp *p;
clrscr();
p->get();
p->show();
getch();
}
Polymorphism:
Polymorphism is another very important feature of Object Orient programming.
Polymorphism is a silent feature of OOP
Poly means many and morphism means forms or shapes. Which means to have different forms for a single
substance.
Polymorphism is a technique to call different functions performing different tasks by a single same line.
There are some requirements for the implementations of polymorphism in a C++ program. They are as follows:
//parent pa; we can also make object of parent class and then assign the
//p=&pa; the object address to the pointer object.
//p->show();
p->show(); // this is statement is also true because p is the pointer of parent class.
p=&ch;
p->show();
p=&gch;
p->show();
getch();
}
Out put will be:
this is parent class function
this is parent class function
this is parent class function
Early Binding:
The events that take place at the compile-time are called early binding. Early binding is also known as
static binding. In early binding the compiler knows before the execution of the program that which
function is to be executed.
For example:
p-> show();
Here the compiler knows before the execution of the program to call the show() function.
The accessing of member functions of a class through the pointers is an example of early binding.
In the above program example, the compiler decides at compile-time to always execute the member
function of that class which declares the pointer object.
Virtual Function:
Virtual means existing in appearance but not in reality. A virtual function is a special type of member
function that is declare with the keyword Virtual before the function declaration in the base class.
It is defined in the base class and may be redefined in any class derived from this base class.
Its name must be same in the base class and derived class.
Once a function is declared virtual in a base class then it becomes virtual in all the derived classes.
The keyword virtual is optional in the derived classes.
For example:
#include<conio.h>
#include<iostream.h>
class parent
{
public:
virtual void show(void){cout<<"this is parent Class Fucnion"<<endl;}
};
class child:public parent
{
public:
virtual void show(void){cout<<"this is child Class Fucnion"<<endl;} // virtual is optional
};
class gchild:public parent
{
public:
void show(void){cout<<"this is Grand Child Class Fucnion"<<endl;}
};
void main()
{
parent *p;
child ch;
gchild gch;
clrscr();
//p->show(); generate error due to virtual fucntion.
p=&ch;
p->show();
p=&gch;
p->show();
getch();
}
Output will be:
this is child Class Fucnion
this is Grand Child Class Fucnion
Note:
We can call virtual function of the base class for example:
Void main()
{
parent *p;
parent pa;
clrscr();
p=&pa;
p->show();
getch();
}
But we not call it in a program because it is called automatically if we skip writing virtual function in
the derived class.
For example:
#include<conio.h>
#include<iostream.h>
class parent
{
public:
virtual void show(void){cout<<"this is parent Class Fucnion"<<endl;}
};
class child:public parent
{
public:
/*void show(void){cout<<"this is child Class Fucnion"<<endl;}*/
};
class gchild:public parent
{
public:
void show(void){cout<<"this is Grand Child Class Fucnion"<<endl;}
};
void main()
{
parent *p;
child ch;
gchild gch;
clrscr();
p=&ch;
p->show();
p=&gch;
p->show();
getch();
}
Output will be:
this is parent Class Fucnion
this is Grand Child Class Fucnion
Late Binding:
The events that take place at the time of execution of the program is called late binding.
Late binding is also known as dynamic binding.
If the keyword virtual is used then the system uses late binding.
The execution of virtual functions through pointers is an example of late binding. While executing a
virtual function through a pointer variable, the computer decides at the time of execution.
It executes the function depending upon the value in the pointer during execution of the program.