Wednesday, 29 April 2026

Polymorphism and Virtual Functions in C++


Polymorphism and Virtual Functions in C++
    A true object-oriented programming paradigm must consist of three items: data abstraction and encapsulation, inheritance and polymorphism. Data abstraction and encapsulation are the processes of defining some new data types. This involves the internal representation of the data members and the methods of declaration, definition and uses in a program. Inheritance involves the creation of a new type from an existing type in some hierarchical fashion.

    The word ‘poly’ originated from a Greek word meaning many and ‘morphism’ from a Greek word meaning form, and thus ‘polymorphism’ means many forms. In object-oriented programming, polymorphism refers to identically named methods (member functions) that have different behavior depending on the type of object they refer.

    Polymorphism is the process of defining a number of objects of different classes into a group and call the methods to carry out the operation of the objects using different function calls. In other words, polymorphism means ‘to carry out different processing steps by functions having same messages’. It treats objects of related classes in a generic manner. The keyword virtual is used to perform the polymorphism concept in C++. Polymorphism refers to the run-time binding to a pointer to a method.

EARLY BINDING
    Selecting a function in normal way, during compilation time is called as early binding or static binding or static linkage. During compilation time, the C++ compiler determines which function is used based on the parameters passed to the function or the function’s return type. The compiler then substitutes the correct function for each invocation. Such compiler-based substitutions are called static linkage. By default, C++ follows early binding. With early binding, one can achieve greater efficiency. Function calls are faster in this case because all the information necessary to call the function are hard coded.

    The purest object-oriented programming language like Small talk permits only run-time binding of the methods, whereas C++ allows both compile-time binding and run-time binding. In that sense, C++ is a hybrid language as it generates the code of both procedural and object-oriented programming paradigm.

Example 1.
A program to demonstrate the operation of the static binding. In this program, a message is given to access the methods of the derived class from the base class members through pointer techniques.

//demonstration of static binding
#include <iostream>
using namespace std;
class square {
protected:
int x;
public:
void getdata();
void display();
int area();
};
class rectangle : public square
{
protected:
int y;
public:
void getdata();
void display();
int area();
};
void square:: getdata()
{
cout << “ enter the value of side x? \n”;
cin >> x;
}
void square:: display()
{
cout << “ value of x = y = ” << x << endl;
cout << “ Area of the square = ” << area();
cout << endl;
}
int square :: area()
{
int temp = x*x;
return(temp);
}
void rectangle :: getdata()
{
cout << “ enter the value of sides x and y ? \n”;
cin >> x >> y;
}
void rectangle:: display()
{ cout << “ value of x = ” << x << “ and y = ” << y << endl;
cout << “ Area of the rectangle = ” << area();
cout << endl;
}
int rectangle :: area()
{
int temp = x*y;
return(temp);
}
int main()
{
square sqobj;
rectangle rectobj;
square *ptr;
ptr = &sqobj;
ptr = &rectobj;
ptr->getdata();
ptr->area();
ptr->display();
return 0;
}
Output of the above program
enter the value of side x?
10
value of x = y = 10
Area of the square = 100

The derived class rectangle is inherited from the base class square through public derivation. It is known that an object of a derived class not only inherits characteristics from the base class but also has characteristics that are specific to the derived class.

Example 2.
A program to demonstrate the compile time binding of the member functions of the class. The same message is given to access the derived class member functions from the array of pointers. As functions are declared as non-virtual, the C++ compiler invokes only the static binding.

//nonvirtual function
//demonstration of compile time binding using
//array of pointers
#include <iostream>
using namespace std;
class baseA {
public :
void display() {
cout << “One \n”;
}
};
class derivedB: public baseA
{
public:
void display(){
cout << “ Two \n”;
}
};
class derivedC: public derivedB
{
public:
void display(){
cout << “ Three \n”;
}
};
int main()
{
//defi ne three objects
baseA obja;
derivedB objb;
derivedC objc;
baseA *ptr[3]; //defi ne an array of pointers to baseA
ptr[0] = &obja;
ptr[1] = &objb;
ptr[2] = &objc;
for (int i = 0; i<=2; i++)
ptr[i]->display(); //same message for all objects
return 0;
}
Output of the above program
One
One
One

0 comments

Post a Comment