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 simple Dynamic/Run Time/Late Binding Polymorphism.

#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

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

#include <iostream>
using namespace std;

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

class B: public A
{
  	public:
  	virtual 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 simple 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; 
		  B b; 
		  a = &f; 
		  a->display();
		  
		  a=&b;
		  a->display();
		   
   		  return 0; 
}
Output :
100
100

NB : This is because second call of 'a' (after the getting the address of 'b')the display() function of base class replaces replaces the existence of display() function of derived class. 

//  -----  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; 
		  B b; 
		  a = &f; 
		  a->display();
		  
		  a=&b;
		  a->display();
		   
   		  return 0; 
}

Output :
100
200

NB : Here run-time binding is necessary to support polymorphism using the virtual keyword.

//  ---------  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; 
       }
       virtual void display() 
        { 
          cout <<y; 
	}		
}; 

int main() 
{ 
		  A f,*a; 
		  B b; 
		  a = &f; 
		  a->display();
		  
		  a=&b;
		  a->display();
		   
   		  return 0; 
}

Output :
100
200

Example : A C++ program to show Pure Virtual Function.

#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!

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.