C++ Notes

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 30

C++ Language

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++.

A simple example of C++ program:


#include<conio.h>
#include<iostream.h>
void main()
{
clrscr();
int a,b;
cout<<"Enter value for A and B=>";
cin>>a>>b;
cout<<"the value for a is"<<a<<endl;
cout<<"the vlaue for b is"<<b;
getch();
}
Explanation:
#include<filename.h>:
#include< > are preprocessor directive that is used to include header file in a program.
<Conio.h>:
conio.h stands for (CONsole input output). Console represents the computer display screen.
Clrscr() function and getch() function are the part of conio.h.
<iostrem.h>:
iostream.h stands for (input output stream). Flow of data form one location to an other location is
called stream. Cout and cin objects are the part of iostream.h.
cout:
cout is a predefine object and its stands for console output. Cout is used to display output on the
computer screen.
cin:
cin is predefine object and its stands for console input. Cin is used to get input form the keyboard
during the execution of a program.
<<:
This is known as (put to operator) or (insertion operator). It directs the output to the output device.
>>:
This is known as (get from operator) or (extraction operator). It gets an input form input device
and assign it to the variable.
endl:
endl stands for (end of line). Its work like \n.
Different between C and C++:
The difference between C and C++ are as follows.
1) COMMENTS:
The syntax of writing comments in C language is ( /* Comments */ ) for both single line comments
and multi line comments.
For example:
Single line comments:
/* my first program */

Multi line comments:


/* this is my first C
program */
In C++ the syntax of writing comments for single line is (// comments ) and the syntax for writing
multi line comments is (/* comments */).
For example:
Single line comments:
//my first program

Multi line comments:


/* this is my first C
program */
2) Declaration of Variables:
In C language we have to declare all the variables used in a C program at the start of a block.
This can be difficult sometimes in large programs containing instructions about 1000 and onwards
But in C++ you can declare a variable at any place in a program just before its usage. This
provides flexibility in program in such a way that a variable is declared where it is needed
For example:
#include <conio.h>
#include <stdio.h>
void main ( )
{
int num = 3 ;
clrscr( );
for( int i = 0 ; I <= 10 ; i++ )
cout << num << x << i << = << num * i <<endl ;
getch();
}
3) Format Specifier:
In C language we use format specifiers (%d, %f, %c etc) while printing the value of a variable using printf()
statement.
But in C++ we does not use format specifiers while printing the value of a variable using COUT
statement.
For example:
#include<conio.h>
#include<iostream.h>
void main()
{
int i=50;
float f=100.50;
char c='C';
clrscr();
cout<<i<<endl<<f<<endl<<c;
getch();
}
4) Function Must Return A Value:
In C++ each and every function must returns a value, so the data type must be declared for the
return value. If you dont want a function to return a value then the key word void must be used.
For example:
#include<conio.h>
#include<iostream.h>
void display(void);
void main()
{
clrscr();
display();
getch();
}

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;
}
}

Function Over loading:


To declare more than one function with the same name but different arguments is known as function over loading.
For example:
#include<conio.h>
#include<iostream.h>
void display(int);
void display(float);
void display(void);
void main()
{
clrscr();
display(10);
display(50.50f);
display();
getch();
}
void display(int a)
{
cout<<a<<endl;
}
void display(float a)
{
cout<<a<<endl;
}
void display(void)
{
cout<<"with out arguments";
}
Note:
write f at the end of the float constant while passing it to a function. Other wise it will generate error.
And if you pass float variable to a function then do not use f.

Object oriented programming:


The fundamental idea behind the object-oriented programming is to combine both data-items and function (that
operated on that data) in to a single unit that is called object is known as object orient programming.
OR
Object oriented programming is that type of programming in which we use the concept of data hiding, data
encapsulation, inheritance and polymorphism by using classes and their objects.

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.

Example of class and objects:


#include<conio.h>
#include<iostream.h>
class parent
{
public:
int id;
char *name;
void fun(void)
{
cout<<"this is My function";
}
};
class myclass ob; // class key word is optional
void main()
{
clrscr();
ob.id=15;
ob.name="imran";
cout<<ob.id<<endl;
cout<<ob.name<<endl;
ob.fun();
getch();
}
Member access Specifiers:
The commands that determine whether a member of a class can be accessed from out side the class or not
are called member access specifiers.
There are three types of member access specifiers.
1) Private
2) Public
3) Protected
1) Public Specifier:
The data-members and the member-functions of a class that comes after the public specifier are
known as public members.
Public members of a class can be accessed within the class and public members of a class can be
accessed form out side of a class through its objects.
Syntax:
public:
For example:
#include<conio.h>
#include<iostream.h>
class myclass
{
public:
int id;
char *name;
void fun(void)
{
id=20;
name="khan";
cout<<id<<endl<<name;
}
};
class myclass ob;
void main()
{
clrscr();
ob.id=10;
ob.name="ali";
cout<<ob.id<<endl<<ob.name<<endl;
ob.fun();
getch();
}
2) Private Specifier:
The data-members and the member-functions of a class that comes after the private specifier are
known as private members.
Private members of a class can only be access with in the class. Private members of a class cannot
be access form out side of a class even through its objects.
Syntax:
private:
For example:
#include<conio.h>
#include<iostream.h>
class myclass
{
private:
int id;
char *name;
public:
void fun(void)
{
id=20;
name="khan";
cout<<id<<endl<<name;
}
};
class myclass ob;
void main()
{
clrscr();
ob.fun();
getch();
}
3) Protectesd Specifier:
The data-members and the member-functions of a class that comes after the protected specifier are
known as protected members.
Protected members of a class can only be access with in the class. Protected members of a class
cannot be access form out side of a class through its objects.
Protected specifier is mostly used in inheritance.
Syntax:
protected:
For example:
#include<conio.h>
#include<iostream.h>
class myclass
{
protected:
int id;
char *name;
public:
void fun(void)
{
id=20;
name="khan";
cout<<id<<endl<<name;
}
};
class myclass ob;
void main()
{
clrscr();
ob.fun();
getch();
}
Data Hiding:
When we declare data members as private in the class and cannot access form out side of the class then
this process is known as data hiding.

Encapsulation:
To declare both data-members and member-function in one unit (means class) then this process is called
encapsulation.

Defining member function outside the Class:


We can also define member function out side the class by using scope resolution operator (::) in this case
only function prototype is declared inside the class.
Syntax:
Return-type class-name :: function-name(arguments)
{
Body of function.
}
For example:
#include<conio.h>
#include<iostream.h>
class first
{
private:
int a,b;
public:
void fun(void);
};

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.

Accessing members Members of Base Class


of Base class through Public Protected Private
Child Class
From with in a Class
Trough its Object

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.

Accessing members Members of Base Class


of Base class through Public Protected Private
Child Class
From with in a Class
Trough its Object

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.

Accessing members Members of Base Class


of Base class through Public Protected Private
Child Class
From with in a Class
Trough its Object

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();
}

Pointer to class Object:


The members of a class can be accessed through the pointer to the class object. In this case the arrow (->)
operator is used to access the members of the class instead of dot (.) operator. The arrow operator is also known as
member access operator. It is denoted by a hyphen (-) and greater than sign.
Syntax:
Pointer object name -> class member
For example:
#include<conio.h>
#include<iostream.h>
#include<string.h>
class emp
{
private:
int id;
char name[20];
float sal;
public:
void get(void)
{
id=20;
strcpy(name,"Imran");
sal=875.50;
}
void show(void)
{
cout<<"Employee id="<<id<<endl;
cout<<"Employee Name="<<name<<endl;
cout<<"Employee Salary="<<sal;
}

};
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:

The parent class must contain a virtual function.


Inheritance must be implemented.
Function Overriding must be implemented.
Pointer of parent class must be used.

Example with Out Virtual Function:


#include<conio.h>
#include<iostream.h>
class parent
{
public:
void show(void){cout<<"this is parent Class Function"<<endl;}
};
class child:public parent
{
public:
void show(void){cout<<"this is child Class Function"<<endl;}
};
class gchild:public parent
{
public:
void show(void){cout<<"this is Grand Child Class Function"<<endl;}
};
void main()
{
parent *p;
child ch;
gchild gch;
clrscr();

//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.

Pure Virtual Function:


The virtual function that is only declared but not defined in the base class is called the pure virtual
functions.
For example:
#include<conio.h>
#include<iostream.h>
class parent
{
public:
virtual void show(void)=0;
};
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();
}
Abstract base Class:

You might also like