Polymorphism and virtual functions
Polymorphism and virtual functions
Inheritance
• Definition: In C++, inheritance is a process in which one object acquires all
the properties and behaviors of its parent object automatically.
• Code reusability: We can reuse, extend or modify the attributes and behaviors
which are defined in other class
• Single inheritance
• Multilevel inheritance
• Multiple inheritance
• Hierarchical inheritance
• Hybrid inheritance
MULTIPLE
SINGLE MULTI-LEVEL
Derived Class
HIERARCHICAL
Base Class
• Means having many forms. Occurs when we have many classes that are related to
each other by inheritance.
• Two types
1) Compile-time polymorphism (static) - Method overloading and operator
overloading
2) Run-time polymorphism (dynamic) - Method overriding
Overloading
Function overloading :
• Two or more function with the same name, but different in parameters is known as
function overloading in C++.
• The function is redefined by using either different types of arguments or a different
number of arguments. It is only through these differences' compiler can differentiate
between the functions.
void display()
{ ... .. ... }
int display()
{ ... .. ... } // overloaded function distinguished
only by return type
Overloading
Operator overloading:
Operator overloading is a feature in C++ that allows operators such as +, -, *, /, =, and many
others to be redefined for user-defined types like classes.
• If an object of the derived class accesses such a function, the derived class’s
function is called, which leads to function overriding.
• * The data members and functions in B with same name as in A hide (or
shadow) those in A. Scope resolution operator can be used to access them.
Method overriding
• Class Box • Int main()
{
….
{
void draw(){print(‘Base’)} TextBox tb;
}; tb.draw();
Class TextBox : public Box
tb.Box::draw();
{
….
void draw(){print(‘Derived’)}
};
The function in the derived class overrides the function in base class.
• The compiler gets confused if the derived class object tries to access one of
the similarly named member functions of the base classes.
• * The data members and functions in B with same name as in A hide (or
shadow) those in A. Scope resolution operator can be used to access them.
Method overriding
• Class Box Class TextBox : public Box, public shape
{ {
…. };
void draw(){print(‘Box’)}
}; Int main(){
If not, use scope resolution operator to invoke corresponding base class function
Virtual Functions
class Base
{ void main()
public: {
void print() Derived derived1;
{ // code } Base* base1 = &derived1;
}; base1->print();
}
class Derived : public Base
{
public:
void print()
{ // code }
};
• A virtual function is a member function in the base class that we expect to redefine in
derived classes.
• The above program does not override the print() function in Base class. To make it
override use virtual function in the base class by placing virtual key word before the
function.
Override identifier in c++
class Base
{
public: void main()
virtual void print() {
{ // code } Derived derived1;
}; Base* base1 = &derived1;
base1->print();
class Derived : public Base }
{
public:
void print(int a) override
{ // code }
};
• By using override identifier, we are informing the compiler that we want to override the definition
of a base class virtual function in the derived class.
• In the above program, the user wanted to override the base class virtual function but by mistake
used a different signature of the print() function by including parameters.
• If the override keyword is not present, then the program compiles successfully, and prints body
of base class print() function as overriding will not occur.
Virtual Inheritance Here, the compiler does not understand if it
should access A through C or through B and
class A hence throws error.
{ class D : public B, public C
int x =5; {
}; Int I = 7; The addition of virtual keyword helps in inheriting
}; class A virtually, which guarantees that there is
class B : public A only one instance of base class A among the
{ derived classes.
Int I = 10;
}; void main()
{ Do the following changes to achieve virtual
class C : public A D obj; inheritance
{ cout<<obj.x<<endl;
Int I = 7; }
class B : virtual public A
};
class C : virtual public A
Scope resolution operator can also be used to solve the issue as follows:
cout<<obj.B::x<<endl;
cout<<obj.C::x<<endl;
Abstract Classes and Pure Virtual Functions
• When a class has a pure virtual function as member function, it becomes an
abstract base class.
• Pure virtual function will make sure that certain members are always present in
all the derived classes.
• Abstract base classes can never be instantiated i.e. an object cannot be created
for this class.
Early and Late Binding
Late Binding
• In late binding, the compiler adds code that identifies the type of object at runtime then
matches the call with the right function definition.
• This can be achieved using virtual keyword (virtual functions) or function overriding.
Early vs Late binding
class Base
{ void main()
public: {
virtual void print() Derived derived1;
{ // code } Base* base1 = &derived1;
}; base1->print();
}
class Derived : public Base
{
public:
void print()
{ // code }
};
class Base
{ void main()
public: virtual ~Base() {
{“destructor invoked”} Base* base1 = new derived;
}; delete b1;
}
class Derived : public Base
{
public: ~Derived()
{ “Destructor invoked”}
};
• Friend class: A class that can access the private and protected members of a
class in which it is declared as friend.