Unit 6 Virtual Function, Polymorphism, and Other C++ Features
Unit 6 Virtual Function, Polymorphism, and Other C++ Features
Polymorphism
In C++, polymorphism causes a member function to behave differently based on the object
that calls/invokes it. Polymorphism is a Greek word that means to have many forms. It occurs
when you have a hierarchy of classes related through inheritance.
Types of Polymorphism
C++ supports two types of polymorphism:
1. Compile-time polymorphism, and
2. Runtime polymorphism
// Base Class
class BaseClass {
public:
int var_base;
base_class_pointer->var_base = 34;
base_class_pointer->var_base = 3400;
base_class_pointer->display();
DerivedClass* derived_class_pointer;
derived_class_pointer = &obj_derived;
derived_class_pointer->var_base = 9448;
derived_class_pointer->var_derived = 98;
derived_class_pointer->display();
return 0;
}
Output:
Displaying Base class variable var_base: 34
Displaying Base class variable var_base: 3400
Displaying Base classvariable var_base: 9448
Displaying Derived class variable var_derived: 98
Virtual Function
- A C++ virtual function is a member function in the base class that you redefine in a derived
class. It is declared using the virtual keyword.
- It is used to tell the compiler to perform dynamic linkage or late binding on the function.
- There is a necessity to use the single pointer to refer to all the objects of the different
classes. So, we create the pointer to the base class that refers to all the derived objects.
But, when base class pointer contains the address of the derived class object, always
executes the base class function. This issue can only be resolved by using the 'virtual'
function.
- A 'virtual' is a keyword preceding the normal declaration of a function.
- When the function is made virtual, C++ determines which function is to be invoked at the
runtime based on the type of the object pointed by the base class pointer.
Example:
#include <iostream>
using namespace std;
class A
{
public:
virtual void display()
{
cout << "Base class is invoked"<<endl;
}
};
class B:public A
{
public:
void display()
{
cout << "Derived Class is invoked"<<endl;
}
};
int main()
{
A* a; //pointer of base class
B b; //object of derived class
a = &b;
a->display(); //Late Binding occurs
}
Example:
#include <iostream>
using namespace std;
class Base
{
public:
virtual void show() = 0;
};
class Derived : public Base
{
public:
void show()
{
std::cout << "Derived class is derived from the base
class." << std::endl;
}
};
int main()
{
Base *bptr;
//Base b;
Derived d;
bptr = &d;
bptr->show();
return 0;
}
Output:
Derived class is derived from the base class.
int main() {
Base *b;
Derived dobj;
b = &dobj;
b->show();
}
Output:
Virtual Function in Derived class
Concrete Classes
Classes that can be used to instantiate objects are called concrete classes.
Virtual Destructor
Deleting a derived class object using a pointer of base class type that has a non-virtual
destructor results in undefined behaviour. To correct this situation, the base class should be
defined with a virtual destructor.
For example, following program results in undefined behaviour.
class base {
public:
base()
{ cout << "Constructing base\n"; }
~base()
{ cout<< "Destructing base\n"; }
};
int main()
{
derived *d = new derived();
base *b = d;
delete b;
getchar();
return 0;
}
Output:
Constructing base
Constructing derived
Destructing base
Example:
Use of virtual destructor
// A program with virtual destructor
#include <iostream>
class base {
public:
base()
{ cout << "Constructing base\n"; }
int main()
{
derived *d = new derived();
base *b = d;
delete b;
getchar();
return 0;
}
Output:
Constructing base
Constructing derived
Destructing derived
Destructing base