Example : A C++ Polymorphism Examples in C++ to show simple Compile Time/Early Binding/Static Polymorphism.

Click this link to access the example of C++ Function Overloading.

Example : A C++ program to show the Pre Dynamic/Run Time/Late Binding Polymorphism concept.
#include <iostream>
using namespace std;

class A 
{
  	public:
  	void display1()
  	{
     	cout<<"Base Class Called"<<endl;
  	}
};

class B: public A
{
  	public:
  	void display2()
  	{
     	cout<<"Child Class Called";
  	}
};

int main() 
	{
           B y;
  	   y.display1();
  	   y.display2();
  		
	   return 0;
	}

Output :
Base Class Called
Child Class Called

// -------  OR  -------

#include <iostream>
using namespace std;

class A 
{
  	public:
  	void display()
  	{
     	cout<<"Base Class Called"<<endl;
  	}
};

class B: public A
{
  	public:
  	void display()
  	{
     	cout<<"Child Class Called";
  	}
};

int main() 
	{
  
  	   A x;
  	   x.display();
  
  	   B y;
  	   y.display();
  		
	   return 0;
	}

Output :
Base Class Called
Child Class Called

//  -----  OR  ---------

#include <iostream>
using namespace std;

class A 
{
  	public:
  	void display()
  	{
     	cout<<"Base Class Called"<<endl<<endl;
  	}
};

class B: public A
{
  	public:
  	void display()
  	{
     	cout<<"Child Class Called"<<endl;
  	}
};

int main() 
	{
  	   B y;
  	   y.display();
  	   y.display();  		
	   return 0;
	}

Output :
Child Class Called
Child Class Called

//  -----  OR  ---------

#include <iostream>
using namespace std;

class A 
{
  	public:
  	void display()
  	{
     	cout<<"Base Class Called"<<endl<<endl;
  	}
};

class B: public A
{
  	public:
  	void display()
  	{
     	cout<<"Child Class Called"<<endl;
  	}
};

int main() 
	{
  	   B y;
  	   y.A::display();
  	   y.display();
  		
	   return 0;
	}

Output :
Base Class Called
Child Class Called

//  -----  OR  ---------

#include <iostream>
using namespace std;

class A 
{
  	public:
  	 virtual void display()
  	{
     	cout<<"Base Class Called"<<endl<<endl;
  	}
};

class B: public A
{
  	public:
  	void display()
  	{
     	cout<<"Child Class Called"<<endl;
  	}
};

int main() 
	{
  		B y;
  	   	y.display();
  	   	y.display();  		
	        return 0;
	}

Output :
Child Class Called
Child Class Called
Example : A C++ program to show actual Dynamic/Run Time/Late Binding Polymorphism using virtual function.
#include <iostream>
using namespace std;

class A 
{ 
   int x; 
   public:
    
       A() 
       {
          x = 100; 
       } 
       void display() 
       { 
          cout <<x<<endl;
       } 
};

class B: public A 
{ 
   int y; 
   public: 
       B() 
       { 
          y = 200; 
       }
       void display() 
        { 
          cout <<y; 
	}		
}; 

int main() 
{ 
   A f,*a;  // *a is base class pointer object
   B b; 
   a = &f; 
   a->display();
		  
   a=&b;    // Base class pointer to derived class object
   a->display();
		   
   return 0; 
}

Output :
100
100

NB : 
(i) Here, the compiler looks at the base pointer type (*a) first. It calls A’s function.
(ii) when no base class virtual function found then compiler ignores the actual object type (B, a=&b;). This shows static binding (compile-time binding) hence called base class display function.

//  -----  OR  -----

#include <iostream>
using namespace std;

class A 
{ 
   int x; 
   public:
    
       A() 
       {
          x = 100; 
       } 
       virtual void display() 
       { 
          cout <<x<<endl;
       } 
};

class B: public A 
{ 
   int y; 
   public: 
       B() 
       { 
          y = 200; 
       }
       void display() 
        { 
          cout <<y; 
	}		
}; 

int main() 
{ 
   A f,*a;   // *a is base class pointer object
   B b; 
   a = &f; 
   a->display();
		  
   a=&b;   // Base class pointer to derived class object
   a->display();
		   
return 0; 
}

Output :
100
200

NB : 
(i) Here, the compiler looks at the base pointer type (*a) first. It calls A’s function.
(ii) When a [base class] virtual function found then compiler waits until runtime and it checks the actual object (B, a=&b;).It calls B’s (overridden) function. This shows dynamic binding (runtime polymorphism).
Example : A C++ program to show Pure Virtual Function to show Dynamic Polymorphism.
#include <iostream>

class Animal 
{
    public:
    virtual void makeSound() = 0; // pure virtual function
};

class Dog : public Animal 
{
    public:
    void makeSound() 
    {
        std::cout << "Dogs sound Woof!" << std::endl;
    }
};

class Cat : public Animal
 {
    public:
    void makeSound() 
    {
        std::cout << "Cats sound Meow!" << std::endl;
    }
};

int main() {
    
    Animal* animalPtr;
    Dog dog;
    Cat cat;

    animalPtr = &dog;
    animalPtr->makeSound();

    animalPtr = &cat;
    animalPtr->makeSound();

    return 0;
}

Output:
Dogs sound Woof!
Cats sound Meow!
Example : A C++ program to show Virtual and Pure Virtual Functions to show Dynamic Polymorphism.
#include <iostream>
using namespace std;

// Base class Shape with one virtual function and one pure virtual function
class Shape 
{
	public:
    // Virtual function - can be overridden by derived classes
    virtual void area() 
    {
        cout << "Calculating area in base Shape class" << endl;
    }

    // Pure virtual function - must be overridden by all non-abstract derived classes
    virtual void draw() = 0;
};

// Derived class Circle from Shape
class Circle : public Shape 
{
	public:
    // Override area() function
    void area() 
	{
        cout << "The formula of Area of Circle is = 3.14*r*r" << endl;
        }

    // Implement the pure virtual function draw()
    void draw() 
	{
        cout << "Drawing Circle" << endl;
        }
};

// Derived class Rectangle from Shape
class Rectangle : public Shape 
{
	public:
    // Override area() function
    void area()
	{
        cout << "The formula of Area of Rectangle is = l * b" << endl;
        }

    // Implement the pure virtual function draw()
    void draw() 
	{
        cout << "Drawing Rectangle" << endl;
        }
};

int main() 
{
    // Pointer to base class Shape
    Shape *ptr;

    // Create objects of derived classes
    Circle c;
    Rectangle r;

    // Point to Circle object
    ptr = &c;
    ptr->area();   // Calls Circle's area()
    ptr->draw();   // Calls Circle's draw()

    // Point to Rectangle object
    ptr = &r;
    ptr->area();   // Calls Rectangle's area()
    ptr->draw();   // Calls Rectangle's draw()

    return 0;
}

Output:
The formula of Area of Circle is = 3.14*r*r
Drawing Circle
The formula of Area of Rectangle is = l * b
Drawing Rectangle

NB: 
(i) Here,Circle and Rectangle are abstract classes because they inherit from Shape which has a pure virtual function draw() and hence is abstract class and also 
we must override draw() in these derive class.
(ii) Here, Virtual functions give us optional overriding and Pure virtual functions enforce overriding (makes the base class abstract).This approach is ideal for building flexible polymorphic systems. 
(iii) Here, we can't create objects of abstract class Shape (such as Shape s, *ptr) rather we only declare pointer for the abstract class(such as Shape *ptr) .

Loading

Categories: C++

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.