Write a C++ Program to demonstrate Run time polymorphism. Here’s a Simple C++ Program to demonstrate Run time polymorphism in C++ Programming Language.
In place of static binding, one would like a binding method that is capable of determining which function should be invoked at run-time, on the basis of object type making call.
This type of binding is knows as late or dynamic binding, since the selection of the appropriate function is done dynamically at run-time.
To achieve dynamic binding, C++ offers a special feature known as virtual function.
A virtual function specification tells the compiler to create a pointer to a function but not to fill in the value of the pointer until the function is actually called.
Then at run-time and on the basis of the object making the call, the appropriate function address is used.
When a function with same name, same number of arguments and same type of arguments is used in both the base class and derived classes, the function in the base class can be made virtual by using a keyword virtual, preceding the function declaration.
For example, in the previous program, the function Display() in the base class B can be made virtual, by using the declaration as:
virtual void Display(void) ;
When a function is made virtual, C++ determines which function to use at run-time based on the type of the object pointed to by the base class pointer, rather than the type of the pointer.
Below is the source code for C++ Program to demonstrate Run time polymorphism which is successfully compiled and run on Windows System to produce desired output as shown below :
SOURCE CODE : :
/* C++ Program to demonstrate Run time polymorphism */
#include<iostream>
using namespace std;
class B // Base class
{
public:
virtual void Display(void) // virtual function
{
cout << "\n The member function Display( ) " ;
cout << "of the "Base Class B" is invoked \n" ;
}
};
class D1 : public B // First derived class
{
public:
void Display(void)
{
cout << "\n The member function Display( ) " ;
cout << "of the "Derived Class D1" is invoked \n" ;
}
};
class D2 : public B // Second derived class
{
public:
void Display(void)
{
cout << "\n The member function Display() " ;
cout << "of the "Derived Class D2" is invoked " ;
}
};
int main()
{
B* base_ptr ; // Pointer to the object of the base class
D1 der1_obj ; // Object of the first derived class D1
base_ptr = &der1_obj ; /* The address of the object der1_obj of the
first derived class D1 is assigned to the
pointer base_ptr of the base class B */
base_ptr->Display( ); // Accessing the member function Display( )
D2 der2_obj ; // Object of the second derived class D2
base_ptr = &der2_obj ; /* The address of the object der2_obj of the
second derived class D2 is assigned to the
pointer base_ptr of the base class B */
base_ptr->Display( ); // Accessing the member function Display( )
return 0;
}
OUTPUT : :
/* C++ Program to demonstrate Run time polymorphism */
The member function Display( ) of the "Derived Class D1" is invoked
The member function Display() of the "Derived Class D2" is invoked
Process returned 0
Above is the source code and output for C++ Program to demonstrate Run time polymorphism which is successfully compiled and run on Windows System to produce desired output.
Run time polymorphism
Below is the source code for C++ Program to demonstrate Run time polymorphism which is successfully compiled and run on Windows System to produce desired output as shown below :
SOURCE CODE : :
OUTPUT : :
Above is the source code and output for C++ Program to demonstrate Run time polymorphism which is successfully compiled and run on Windows System to produce desired output.
need an explanation for this answer? contact us directly to get an explanation for this answer