Polymorphism in C++
Polymorphism in C++
Polymorphism in C++
Polymorphism means having multiple forms of one thing. In inheritance, polymorphism is
done, by method overriding, when both super and sub class have member function with
same declaration bu different definition.
In this example, function show() is overridden in the derived class. Now let us study how
these overridden functions are called in main() function.
Base class
Derived class
In the above example, we are calling the overrided function using Base class and
Derived class object. Base class object will call base version of the function and derived
class's object will call the derived version of the function.
Base class
In the above example, although, the object is of Derived class, still Base class's method
is called. This happens due to Early Binding.
Compiler on seeing Base class's pointer, set call to Base class's show() function,
without knowing the actual object type.
Virtual Keyword is used to make a member function of the base class Virtual.
class Base
public:
void show()
};
public:
void show()
int main()
b = &d;
b->show(); //Early Binding Ocuurs
Base class
When we use Base class's pointer to hold Derived class's object, base class pointer or
reference will always call the base version of the function
class Base
public:
};
public:
void show()
int main()
b = &d;
Derived class
On using Virtual keyword with Base class's function, Late Binding takes place and the
derived version of function will be called, because base class pointer pointes to Derived
class object.
#include <iostream>
class A
{
public:
};
class B: public A
private:
};
int main()
A *a;
B b;
a = &b;
a->show();
}
Derived class
To accomplich late binding, Compiler creates VTABLEs, for each class with virtual
function. The address of virtual functions is inserted into these tables. Whenever an
object of such class is created the compiler secretly inserts a pointer called vpointer,
pointing to VTABLE for that object. Hence when function is called, compiler is able to
resovle the call by binding the correct function using the vpointer.
3. Only the Base class Method's declaration needs the Virtual Keyword, not the
definition.
4. If a function is declared as virtual in the base class, it will be virtual in all its derived
classes.
5. The address of the virtual Function is placed in the VTABLE and the copiler
uses VPTR(vpointer) to point to the Virtual Function.
C++ TUTORIALS
class Base
public:
};
public:
void show()
};
int main()
Base *b;
Derived d;
b = &d;
b->show();
In the above example Base class is abstract, with pure virtual show() function, hence we
cannot create object of base class.
As the VTABLE for Abstract class is incomplete, hence the compiler will not let the
creation of object for such class and will display an errror message whenever you try to
do so.
class Base
public:
};
public:
void show()
};
int main()
Base *b;
Derived d;
b = &d;
b->show();
1. ← Prev
2. Next →
C++ TUTORIALS
int main()
{
Base* b = new Derived; // Upcasting
delete b;
}
Base Destructor
In the above example, delete b will only call the Base class destructor, which is
undesirable because, then the object of Derived class remains undestructed, because
its destructor is never called. Which results in memory leak.
Upcasting with Virtual Destructor in C++
Now lets see. what happens when we have Virtual destructor in the base class.
class Base
{
public:
virtual ~Base()
{
cout << "Base Destructor\n";
}
};
int main()
{
Base* b = new Derived; // Upcasting
delete b;
}
Derived Destructor
Base Destructor
When we have Virtual destructor inside the base class, then first Derived class's
destructor is called and then Base class's destructor is called, which is the desired
behaviour.
Pure Virtual Destructors in C++
10. Pure Virtual Destructors are legal in C++. Also, pure virtual Destructors must be
defined, which is against the pure virtual behaviour.
11. The only difference between Virtual and Pure Virtual Destructor is, that pure virtual
destructor will make its Base class Abstract, hence you cannot create object of that
class.
12. There is no requirement of implementing pure virtual destructors in the derived
classes.
class Base
{
public:
virtual ~Base() = 0; // Pure Virtual Destructor
};
• ← Prev
• Next →
C++ TUTORIALS
• Operator Overloading
• Operator Overloading
• Operator Overloading Examples
Almost any operator can be overloaded in C++. However there are few operator which
can not be overloaded. Operator that are not overloaded are follows
• Member Function
• Non-Member Function
• Friend Function
Operator overloading function can be a member function if the Left operand is an Object
of that class, but if the Left operand is different, then Operator overloading function must
be a non-member function.
Operator overloading function can be made friend function if it needs access to the
private and protected members of class.
1. ← Prev
2. Next →
C++ TUTORIALS
Available on:
• Overview of C++
• Introduction to C++
• OOPS concepts basic
• Basic Syntax and Structure
• Data types and Modifiers
• Variables in C++
• Operators in C++
• sizeof and typedef in C++
• Decision Making
• Loop types
• Storage Classes
• Functions
• Inheritance
• Introduction to Inheritance
• Types of Inheritance
• Order of Constructor Call
• Upcasting
• Polymorphism
• Function Overriding
• Virtual Functions
• Abstract class and Pure Virtual Functions
• Virtual Destructors
• Operator Overloading
• Operator Overloading
• Operator Overloading Examples
• C++ Miscellaneous
• File Streams
• Exception Handling
• Memory Management
• Multithreading
• Initializer List
• Defining Namespace
• STL in C++ →
• C++ Programs →
#include <conio.h>
class Time
{
int h,m,s;
public:
Time()
void setTime();
void show()
Time operator+(time);
};
Time t;
int a,b;
a = s+t1.s;
t.s = a%60;
b = (a/60)+m+t1.m;
t.m = b%60;
t.h = (b/60)+h+t1.h;
t.h = t.h%12;
return t;
void time::setTime()
cin >> h;
cin >> m;
cin >> s;
void main()
Time t1,t2,t3;
t1.setTime();
t2.setTime();
t1.show();
cout << "\n Second time ";
t2.show();
t3.show();
getch();
While normal addition of two numbers return the sumation result. In the case above we
have overloaded the + operator, to perform addition of two Time class objects. We add
the seconds, minutes and hour values separately to return the new value of time.
In the setTime() funtion we are separately asking the user to enter the values for hour,
minute and second, and then we are setting those values to the Time class object.
1:20:30
2:15:25
3:35:55
First two are values of t1 and t2 and the third is the result of their addition.
18. We can overload output operator << to print values for user defined datatypes.
19. We can overload output operator >> to input values for user defined datatypes.
You have seen above that << operator is overloaded with ostream class object cout to
print primitive datatype values to the screen, which is the default behavious
of << operator, when used with cout. In other words, it is already overloaded by default.
Similarly we can overload << operator in our class to print user-defined datatypes to
screen. For example we can overload << in our Time class to display the value
of Time object using cout rather than writing a custom member function like show()to
print the value of Time class objects.
Time t1(3,15,48);
NOTE: When the operator does not modify its operands, the best way to overload the
operator is via friend function.
#include<iostream.h>
#include<conio.h>
class Time
public:
// default constructor
Time()
{
// overloaded constructor
};
out << "Time is: " << tm.hr << " hour : " << tm.min << " min : "
<< tm.sec << " sec";
return out;
void main()
Time tm(3,15,45);
cout << tm;
This is simplied in languages like Core Java, where all you need to do in a class is
override the toString() method of the String class, and you can define how to print
the object of that class.
Let's take a quick example by overloading the ==operator in the Time class to directly
compare two objects of Time class.
class Time
public:
// default constructor
Time()
// overloaded constructor
{
hr=h, min=m; sec=s;
};
/*
their values
*/
void main()
Time t1(3,15,45);
Time t2(4,15,45);
if(t1 == t2)
{
cout << "Both the time values are equal";
else
As the hour value of object t1 is 3 and for object t2 it is 4, hence they are not equal.
Whereas, Copy constructor is a special constructor that initializes a new object from
an existing object.
• Next →