Polymorphism in C
Polymorphism in C
The word polymorphism means having many forms. In simple words, we can define
polymorphism as the ability of a message to be displayed in more than one form.
Real life example of polymorphism, a person at the same time can have different
characteristic. Like a man at the same time is a father, a husband, an employee. So the same
person posses different behavior in different situations. This is called polymorphism.
Polymorphism is considered as one of the important features of Object Oriented
Programming.
In C++ polymorphism is mainly divided into two types:
• Compile time Polymorphism
• Runtime Polymorphism
• Function Overloading: When there are multiple functions with same name but
different parameters then these functions are said to be overloaded. Functions can
be overloaded by change in number of arguments or/and change in type of
arguments.
int main() {
Geeks obj1;
In the above example, a single function named func acts differently in three
different situations which is the property of polymorphism.
class Complex {
private:
int real, imag;
public:
Complex(int r = 0, int i =0) {real = r; imag = i;}
int main()
{
Complex c1(10, 5), c2(2, 4);
Complex c3 = c1 + c2; // An example call to "operator+"
c3.print();
}
Output:
12 + i9
In the above example the operator ‘+’ is overloaded. The operator ‘+’ is an
addition operator and can add two numbers(integers or floating point) but here the
operator is made to perform addition of two imaginary or complex numbers. To
learn operator overloading in details visit this link.
#include <bits/stdc++.h>
using namespace std;
class base
{
public:
virtual void print ()
{ cout<< "print base class" <<endl; }
void show ()
{ cout<< "show base class" <<endl; }
};
void show ()
{ cout<< "show derived class" <<endl; }
};
//main function
int main()
{
base *bptr;
derived d;
bptr = &d;
return 0;
}
Output:
print derived class
show base class
#include <iostream>
using namespace std;
class base {
public:
virtual void print()
{
cout << "print base class" << endl;
}
void show()
{
cout << "show base class" << endl;
}
};
void show()
{
cout << "show derived class" << endl;
}
};
int main()
{
base* bptr;
derived d;
bptr = &d;
NOTE: If we have created a virtual function in the base class and it is being overridden in the
derived class then we don’t need virtual keyword in the derived class, functions are
automatically considered as virtual functions in the derived class.
Working of virtual functions(concept of VTABLE and VPTR)
As discussed here, If a class contains a virtual function then compiler itself does two things:
1. If object of that class is created then a virtual pointer(VPTR) is inserted as a data
member of the class to point to VTABLE of that class. For each new object created, a
new virtual pointer is inserted as a data member of that class.
2. Irrespective of object is created or not, a static array of function pointer called
VTABLE where each cell contains the address of each virtual function contained in
that class.
Consider the example below:
class base {
public:
void fun_1() { cout << "base-1\n"; }
virtual void fun_2() { cout << "base-2\n"; }
virtual void fun_3() { cout << "base-3\n"; }
virtual void fun_4() { cout << "base-4\n"; }
};
int main()
{
base* p;
derived obj1;
p = &obj1;
Explanation: Initially, we create a pointer of type base class and initialize it with the address
of the derived class object. When we create an object of the derived class, the compiler
creates a pointer as a data member of the class containing the address of VTABLE of the
derived class.
Similar concept of Late and Early Binding is used as in above example. For fun_1()
function call, base class version of function is called, fun_2() is overridden in derived class so
derived class version is called, fun_3() is not overridden in derived class and is virtual
function so base class version is called, similarly fun_4() is not overridden so base class
version is called.
NOTE: fun_4(int) in derived class is different from virtual function fun_4() in base class as prototype
of both the function is different.
A pure virtual function (or abstract function) in C++ is a virtual function for which we don’t
have implementation, we only declare it. A pure virtual function is declared by assigning 0 in
declaration. See the following example.
// An abstract class
class Test
{
// Data members of class
public:
// Pure Virtual Function
virtual void show() = 0;
/* Other members */
};
A complete example:
A pure virtual function is implemented by classes which are derived from a Abstract class.
Following is a simple example to demonstrate the same.
#include<iostream>
using namespace std;
class Base
{
int x;
public:
virtual void fun() = 0;
int getX() { return x; }
};
int main(void)
{
Derived d;
d.fun();
return 0;
}
Output:
fun() called