Runtime Polymorphism in various types of Inheritance in C++

16 Apr, 2020
C++ allows users to use the concept of Run-Time Polymorphism using Virtual Functions for any type of Inheritance .
Below is how to implement Run-Time Polymorphism in all types of inheritance:
Single Inheritance:// C++ program to demonstrate Run Time// Polymorphism in Single Inheritance  #include <iostream>using namespace std;  // Base Classclass Base {  public:    // Virtual function    virtual void funct1()    {        cout << "Base::funct1() is called\n";    }      // Virtual function    virtual void funct2(int x)    {        cout << "Base's Val of x:"             << x << endl;    }      // Non-Virtual Function    void funct3()    {        cout << "Base is the Parent class!"             << endl;    }};  // Derived Class or Sub Classclass Derived : public Base {private:    // Virtual Functions    // can also be Private!    void funct1()    {        cout << "Derived::funct1() is called\n";    }    void funct2(int x)    {        cout << "Derived Class's Val of x:"             << x << endl;    }    void funct3()    {        cout << "It's the Derived class's"             << " funct3() called!" << endl;    }};  int main(){      // Run-Time Polymorphism    // in Single Inheritance    Base* bptr = new Derived();      // virtual function    bptr->funct1();      // virtual function    bptr->funct2(12);      // Non-virtual function    bptr->funct3();      return 0;}Output:Derived::funct1() is called
Derived Class's Val of x:12
Base is the Parent class!
Multiple Inheritance:#include <iostream>using namespace std;  // Parent to Derived classclass Base1 {public:    // Non-Virtual function    void funct1()    {        cout << "Base1::funct1() is called\n";    }      // Virtual function    virtual void funct2(int x)    {        cout << "Base1's Val of x:"             << x << endl;    }      // Non-Virtual Function    void funct3()    {        cout << "Base1 is the Parent class!"             << endl;    }};  // Second Parent to Derived classclass Base2 {public:    void funct1()    {        cout << "Base2::funct1() is called\n";    }    void funct2(int x)    {        cout << "Base2's Val of x:" << x << endl;    }      // Only Virtual Function    // in Base2 Parent class    virtual void funct3()    {        cout << "Base2 is Also a Parent class!"             << endl;    }};  // Derived Class of Base1 and Base2class Derived : public Base1, public Base2 {private:    void funct1()    {        cout << "Derived::funct1() is called\n";    }    void funct2(int x)    {        cout << "Derived Class's Val of x:"             << x << endl;    }    void funct3()    {        cout << "Derived::funct3() is called "             << "and not Base2::funct3() due"             << " to RTP" << endl;    }};  int main(){    Derived d;      // Run-Time Polymorphism    // in Multiple Inheritance    Base1* b1ptr = &d;      // Compile-Time Binding,    // Hence Base1::funct1() will be called!    b1ptr->funct1();      // virtual function of Base1    // RunTime PolyMorphism    b1ptr->funct2(10);      // Now Parent Class Base2    // is also pointed to object 'd'    // of Derived (to demonstrate RTP)    Base2* b2ptr = &d;      // virtual function of Base2    // RunTime PolyMorphism    b2ptr->funct3();      return 0;}Output:Base1::funct1() is called
Derived Class's Val of x:10
Derived::funct3() is called and not Base2::funct3() due to RTP
Note: Here Both Base1 pointer and Base2 pointer may be pointing to the same Derived Class object ‘d’ but actually the Compiler selects difference Virtual Functions during Run-Time due to the use of different Base Class pointers.Multi-level inheritance:// C++ Program to illustrate Run-Time// Polymorphism in multi-level inheritance  #include <iostream>using namespace std;  // Parent Classclass Base1 {public:    // Virtual function    virtual void funct1()    {        cout << "Base1::funct1() is called\n";    }      // Virtual function    virtual void funct2(int x)    {        cout << "Base1's Val of x:"             << x << endl;    }      // Non-Virtual Function    void funct3()    {        cout << "Base1 is the Parent class!"             << endl;    }};  // Derived Class of Base1// but Parent to Base3class Base2 : public Base1 {      // Virtual Functions can be Private!private:    void funct1()    {        cout << "Base2::funct1() is called\n";    }    void funct2(int x)    {        cout << "Base2's Val of x:"             << x << endl;    }    void funct3()    {        cout << "Base2 is the first "             << "Derived class!" << endl;    }};  // Derived Class of Base2// but Parent to Derivedclass Base3 : public Base2 {private:    void funct1()    {        cout << "Base3::funct1() is called\n";    }    void funct2(int x)    {        cout << "Base3's Val of x:"             << x << endl;    }    void funct3()    {        cout << "Class Base3 is second "             << "Derived class!" << endl;    }};  // 3 Levels of Multi-Level Inheritance// and final Child Classclass Derived : public Base3 {private:    void funct1()    {        cout << "Derived::funct1() is called\n";    }    void funct2(int x)    {        cout << "Derived Class's Val of x:"             << x << endl;    }    void funct3()    {        cout << "Class Derived is Final"             << " Child class!" << endl;    }};  int main(){      // Run-Time Polymorphism    // in multi-level Inheritance    Base1* b1ptr = new Derived;      b1ptr->funct1();    b1ptr->funct2(30);      // Compile-Time Binding    b1ptr->funct3();      return 0;}Output:Derived::funct1() is called
Derived Class's Val of x:30
Base1 is the Parent class!
Explanation : In the above Example, the Derived class is the final Child class which inherits from Base3 which inherits from Base2 which again finally inherits from the Base1 (Parent Class to Base2). But if you see the Run-Time Polymorphism works even when you are trying to use Virtual Functions in Base1 Class and point its pointer to Derived Class (Which is the great grand-Child of Base1). Hence, even here Run-Time Polymorphism works according to the standard Rules.Hierarchical inheritance:// C++ Program to illustrate Run-Time// Polymorphism in Hierarchical inheritance  #include <iostream>using namespace std;  class Base1 {public:    // Virtual function of Parent Class    virtual void funct1()    {        cout << "Base1::funct1() is called\n";    }    virtual void funct2(int x)    {        cout << "Base1's Val of x:" << x << endl;    }      // Non-Virtual Function    void funct3()    {        cout << "Base1 is the Parent class!"             << endl;    }};  class Base2 : public Base1 {private:    void funct1()    {        cout << "Base2::funct1() is called\n";    }    void funct2(int x)    {        cout << "Base2's Val of x:"             << x << endl;    }    void funct3()    {        cout << "Base2 is the first"             << " Derived class!" << endl;    }};  class Base3 : public Base1 {private:    void funct1()    {        cout << "Base3::funct1() is called\n";    }    void funct2(int x)    {        cout << "Base3's Val of x:"             << x << endl;    }    void funct3()    {        cout << "Class Base3 is second"             << " Derived class!" << endl;    }};  // Grand-Child_1 of Base1 classclass Derived1 : public Base3 {private:    void funct1()    {        cout << "Derived1::funct1() is called\n";    }    void funct2(int x)    {        cout << "Derived1 Class's Val of x:"             << x << endl;    }    void funct3()    {        cout << "Class Derived1 is Good!!"             << endl;    }};  // Grand-Child_2 of Base1 classclass Derived2 : public Base3 {private:    void funct1()    {        cout << "Derived2::funct1()"             << " is called\n";    }    void funct2(int x)    {        cout << "Derived2 Class's Val "             << "of x:" << x << endl;    }    void funct3()    {        cout << "Class Derived2 is Good!!"             << endl;    }};  // Run-Time Polymorphism// in Hierarchical Inheritanceint main(){      // Base1 class's(Parent class's)    // pointer points to Derived1 class    Base1* b1ptr = new Derived1();      // Run-Time Polymorphism    b1ptr->funct1();    Derived2 d2;      // Now the Base1 class pointer    // points to d2 object(Derived2 class)    b1ptr = &d2;      // Run-Time Polymorphism    b1ptr->funct2(30);      // Compile-Time Binding    b1ptr->funct3();      return 0;}Output:Derived1::funct1() is called
Derived2 Class's Val of x:30
Base1 is the Parent class!
Explanation: Here, the Parent is Base1 and its Grand-children are Derived1 Class and Derived2 class. Even in this case, When the Base1 Class pointer is pointed to Derived1 object or Derived2 object, due to the Virtual Functions (‘VPTR’ and ‘VTABLE’), we can apply Run-Time Polymorphism here.
Single Inheritance:// C++ program to demonstrate Run Time// Polymorphism in Single Inheritance  #include <iostream>using namespace std;  // Base Classclass Base {  public:    // Virtual function    virtual void funct1()    {        cout << "Base::funct1() is called\n";    }      // Virtual function    virtual void funct2(int x)    {        cout << "Base's Val of x:"             << x << endl;    }      // Non-Virtual Function    void funct3()    {        cout << "Base is the Parent class!"             << endl;    }};  // Derived Class or Sub Classclass Derived : public Base {private:    // Virtual Functions    // can also be Private!    void funct1()    {        cout << "Derived::funct1() is called\n";    }    void funct2(int x)    {        cout << "Derived Class's Val of x:"             << x << endl;    }    void funct3()    {        cout << "It's the Derived class's"             << " funct3() called!" << endl;    }};  int main(){      // Run-Time Polymorphism    // in Single Inheritance    Base* bptr = new Derived();      // virtual function    bptr->funct1();      // virtual function    bptr->funct2(12);      // Non-virtual function    bptr->funct3();      return 0;}Output:Derived::funct1() is called
Derived Class's Val of x:12
Base is the Parent class!

// C++ program to demonstrate Run Time// Polymorphism in Single Inheritance  #include <iostream>using namespace std;  // Base Classclass Base {  public:    // Virtual function    virtual void funct1()    {        cout << "Base::funct1() is called\n";    }      // Virtual function    virtual void funct2(int x)    {        cout << "Base's Val of x:"             << x << endl;    }      // Non-Virtual Function    void funct3()    {        cout << "Base is the Parent class!"             << endl;    }};  // Derived Class or Sub Classclass Derived : public Base {private:    // Virtual Functions    // can also be Private!    void funct1()    {        cout << "Derived::funct1() is called\n";    }    void funct2(int x)    {        cout << "Derived Class's Val of x:"             << x << endl;    }    void funct3()    {        cout << "It's the Derived class's"             << " funct3() called!" << endl;    }};  int main(){      // Run-Time Polymorphism    // in Single Inheritance    Base* bptr = new Derived();      // virtual function    bptr->funct1();      // virtual function    bptr->funct2(12);      // Non-virtual function    bptr->funct3();      return 0;}
Derived::funct1() is called
Derived Class's Val of x:12
Base is the Parent class!

Multiple Inheritance:#include <iostream>using namespace std;  // Parent to Derived classclass Base1 {public:    // Non-Virtual function    void funct1()    {        cout << "Base1::funct1() is called\n";    }      // Virtual function    virtual void funct2(int x)    {        cout << "Base1's Val of x:"             << x << endl;    }      // Non-Virtual Function    void funct3()    {        cout << "Base1 is the Parent class!"             << endl;    }};  // Second Parent to Derived classclass Base2 {public:    void funct1()    {        cout << "Base2::funct1() is called\n";    }    void funct2(int x)    {        cout << "Base2's Val of x:" << x << endl;    }      // Only Virtual Function    // in Base2 Parent class    virtual void funct3()    {        cout << "Base2 is Also a Parent class!"             << endl;    }};  // Derived Class of Base1 and Base2class Derived : public Base1, public Base2 {private:    void funct1()    {        cout << "Derived::funct1() is called\n";    }    void funct2(int x)    {        cout << "Derived Class's Val of x:"             << x << endl;    }    void funct3()    {        cout << "Derived::funct3() is called "             << "and not Base2::funct3() due"             << " to RTP" << endl;    }};  int main(){    Derived d;      // Run-Time Polymorphism    // in Multiple Inheritance    Base1* b1ptr = &d;      // Compile-Time Binding,    // Hence Base1::funct1() will be called!    b1ptr->funct1();      // virtual function of Base1    // RunTime PolyMorphism    b1ptr->funct2(10);      // Now Parent Class Base2    // is also pointed to object 'd'    // of Derived (to demonstrate RTP)    Base2* b2ptr = &d;      // virtual function of Base2    // RunTime PolyMorphism    b2ptr->funct3();      return 0;}Output:Base1::funct1() is called
Derived Class's Val of x:10
Derived::funct3() is called and not Base2::funct3() due to RTP
Note: Here Both Base1 pointer and Base2 pointer may be pointing to the same Derived Class object ‘d’ but actually the Compiler selects difference Virtual Functions during Run-Time due to the use of different Base Class pointers.
#include <iostream>using namespace std;  // Parent to Derived classclass Base1 {public:    // Non-Virtual function    void funct1()    {        cout << "Base1::funct1() is called\n";    }      // Virtual function    virtual void funct2(int x)    {        cout << "Base1's Val of x:"             << x << endl;    }      // Non-Virtual Function    void funct3()    {        cout << "Base1 is the Parent class!"             << endl;    }};  // Second Parent to Derived classclass Base2 {public:    void funct1()    {        cout << "Base2::funct1() is called\n";    }    void funct2(int x)    {        cout << "Base2's Val of x:" << x << endl;    }      // Only Virtual Function    // in Base2 Parent class    virtual void funct3()    {        cout << "Base2 is Also a Parent class!"             << endl;    }};  // Derived Class of Base1 and Base2class Derived : public Base1, public Base2 {private:    void funct1()    {        cout << "Derived::funct1() is called\n";    }    void funct2(int x)    {        cout << "Derived Class's Val of x:"             << x << endl;    }    void funct3()    {        cout << "Derived::funct3() is called "             << "and not Base2::funct3() due"             << " to RTP" << endl;    }};  int main(){    Derived d;      // Run-Time Polymorphism    // in Multiple Inheritance    Base1* b1ptr = &d;      // Compile-Time Binding,    // Hence Base1::funct1() will be called!    b1ptr->funct1();      // virtual function of Base1    // RunTime PolyMorphism    b1ptr->funct2(10);      // Now Parent Class Base2    // is also pointed to object 'd'    // of Derived (to demonstrate RTP)    Base2* b2ptr = &d;      // virtual function of Base2    // RunTime PolyMorphism    b2ptr->funct3();      return 0;}
Base1::funct1() is called
Derived Class's Val of x:10
Derived::funct3() is called and not Base2::funct3() due to RTP

Note: Here Both Base1 pointer and Base2 pointer may be pointing to the same Derived Class object ‘d’ but actually the Compiler selects difference Virtual Functions during Run-Time due to the use of different Base Class pointers.
Multi-level inheritance:// C++ Program to illustrate Run-Time// Polymorphism in multi-level inheritance  #include <iostream>using namespace std;  // Parent Classclass Base1 {public:    // Virtual function    virtual void funct1()    {        cout << "Base1::funct1() is called\n";    }      // Virtual function    virtual void funct2(int x)    {        cout << "Base1's Val of x:"             << x << endl;    }      // Non-Virtual Function    void funct3()    {        cout << "Base1 is the Parent class!"             << endl;    }};  // Derived Class of Base1// but Parent to Base3class Base2 : public Base1 {      // Virtual Functions can be Private!private:    void funct1()    {        cout << "Base2::funct1() is called\n";    }    void funct2(int x)    {        cout << "Base2's Val of x:"             << x << endl;    }    void funct3()    {        cout << "Base2 is the first "             << "Derived class!" << endl;    }};  // Derived Class of Base2// but Parent to Derivedclass Base3 : public Base2 {private:    void funct1()    {        cout << "Base3::funct1() is called\n";    }    void funct2(int x)    {        cout << "Base3's Val of x:"             << x << endl;    }    void funct3()    {        cout << "Class Base3 is second "             << "Derived class!" << endl;    }};  // 3 Levels of Multi-Level Inheritance// and final Child Classclass Derived : public Base3 {private:    void funct1()    {        cout << "Derived::funct1() is called\n";    }    void funct2(int x)    {        cout << "Derived Class's Val of x:"             << x << endl;    }    void funct3()    {        cout << "Class Derived is Final"             << " Child class!" << endl;    }};  int main(){      // Run-Time Polymorphism    // in multi-level Inheritance    Base1* b1ptr = new Derived;      b1ptr->funct1();    b1ptr->funct2(30);      // Compile-Time Binding    b1ptr->funct3();      return 0;}Output:Derived::funct1() is called
Derived Class's Val of x:30
Base1 is the Parent class!
Explanation : In the above Example, the Derived class is the final Child class which inherits from Base3 which inherits from Base2 which again finally inherits from the Base1 (Parent Class to Base2). But if you see the Run-Time Polymorphism works even when you are trying to use Virtual Functions in Base1 Class and point its pointer to Derived Class (Which is the great grand-Child of Base1). Hence, even here Run-Time Polymorphism works according to the standard Rules.
// C++ Program to illustrate Run-Time// Polymorphism in multi-level inheritance  #include <iostream>using namespace std;  // Parent Classclass Base1 {public:    // Virtual function    virtual void funct1()    {        cout << "Base1::funct1() is called\n";    }      // Virtual function    virtual void funct2(int x)    {        cout << "Base1's Val of x:"             << x << endl;    }      // Non-Virtual Function    void funct3()    {        cout << "Base1 is the Parent class!"             << endl;    }};  // Derived Class of Base1// but Parent to Base3class Base2 : public Base1 {      // Virtual Functions can be Private!private:    void funct1()    {        cout << "Base2::funct1() is called\n";    }    void funct2(int x)    {        cout << "Base2's Val of x:"             << x << endl;    }    void funct3()    {        cout << "Base2 is the first "             << "Derived class!" << endl;    }};  // Derived Class of Base2// but Parent to Derivedclass Base3 : public Base2 {private:    void funct1()    {        cout << "Base3::funct1() is called\n";    }    void funct2(int x)    {        cout << "Base3's Val of x:"             << x << endl;    }    void funct3()    {        cout << "Class Base3 is second "             << "Derived class!" << endl;    }};  // 3 Levels of Multi-Level Inheritance// and final Child Classclass Derived : public Base3 {private:    void funct1()    {        cout << "Derived::funct1() is called\n";    }    void funct2(int x)    {        cout << "Derived Class's Val of x:"             << x << endl;    }    void funct3()    {        cout << "Class Derived is Final"             << " Child class!" << endl;    }};  int main(){      // Run-Time Polymorphism    // in multi-level Inheritance    Base1* b1ptr = new Derived;      b1ptr->funct1();    b1ptr->funct2(30);      // Compile-Time Binding    b1ptr->funct3();      return 0;}
Derived::funct1() is called
Derived Class's Val of x:30
Base1 is the Parent class!

Explanation : In the above Example, the Derived class is the final Child class which inherits from Base3 which inherits from Base2 which again finally inherits from the Base1 (Parent Class to Base2). But if you see the Run-Time Polymorphism works even when you are trying to use Virtual Functions in Base1 Class and point its pointer to Derived Class (Which is the great grand-Child of Base1). Hence, even here Run-Time Polymorphism works according to the standard Rules.
Hierarchical inheritance:// C++ Program to illustrate Run-Time// Polymorphism in Hierarchical inheritance  #include <iostream>using namespace std;  class Base1 {public:    // Virtual function of Parent Class    virtual void funct1()    {        cout << "Base1::funct1() is called\n";    }    virtual void funct2(int x)    {        cout << "Base1's Val of x:" << x << endl;    }      // Non-Virtual Function    void funct3()    {        cout << "Base1 is the Parent class!"             << endl;    }};  class Base2 : public Base1 {private:    void funct1()    {        cout << "Base2::funct1() is called\n";    }    void funct2(int x)    {        cout << "Base2's Val of x:"             << x << endl;    }    void funct3()    {        cout << "Base2 is the first"             << " Derived class!" << endl;    }};  class Base3 : public Base1 {private:    void funct1()    {        cout << "Base3::funct1() is called\n";    }    void funct2(int x)    {        cout << "Base3's Val of x:"             << x << endl;    }    void funct3()    {        cout << "Class Base3 is second"             << " Derived class!" << endl;    }};  // Grand-Child_1 of Base1 classclass Derived1 : public Base3 {private:    void funct1()    {        cout << "Derived1::funct1() is called\n";    }    void funct2(int x)    {        cout << "Derived1 Class's Val of x:"             << x << endl;    }    void funct3()    {        cout << "Class Derived1 is Good!!"             << endl;    }};  // Grand-Child_2 of Base1 classclass Derived2 : public Base3 {private:    void funct1()    {        cout << "Derived2::funct1()"             << " is called\n";    }    void funct2(int x)    {        cout << "Derived2 Class's Val "             << "of x:" << x << endl;    }    void funct3()    {        cout << "Class Derived2 is Good!!"             << endl;    }};  // Run-Time Polymorphism// in Hierarchical Inheritanceint main(){      // Base1 class's(Parent class's)    // pointer points to Derived1 class    Base1* b1ptr = new Derived1();      // Run-Time Polymorphism    b1ptr->funct1();    Derived2 d2;      // Now the Base1 class pointer    // points to d2 object(Derived2 class)    b1ptr = &d2;      // Run-Time Polymorphism    b1ptr->funct2(30);      // Compile-Time Binding    b1ptr->funct3();      return 0;}Output:Derived1::funct1() is called
Derived2 Class's Val of x:30
Base1 is the Parent class!
Explanation: Here, the Parent is Base1 and its Grand-children are Derived1 Class and Derived2 class. Even in this case, When the Base1 Class pointer is pointed to Derived1 object or Derived2 object, due to the Virtual Functions (‘VPTR’ and ‘VTABLE’), we can apply Run-Time Polymorphism here.
// C++ Program to illustrate Run-Time// Polymorphism in Hierarchical inheritance  #include <iostream>using namespace std;  class Base1 {public:    // Virtual function of Parent Class    virtual void funct1()    {        cout << "Base1::funct1() is called\n";    }    virtual void funct2(int x)    {        cout << "Base1's Val of x:" << x << endl;    }      // Non-Virtual Function    void funct3()    {        cout << "Base1 is the Parent class!"             << endl;    }};  class Base2 : public Base1 {private:    void funct1()    {        cout << "Base2::funct1() is called\n";    }    void funct2(int x)    {        cout << "Base2's Val of x:"             << x << endl;    }    void funct3()    {        cout << "Base2 is the first"             << " Derived class!" << endl;    }};  class Base3 : public Base1 {private:    void funct1()    {        cout << "Base3::funct1() is called\n";    }    void funct2(int x)    {        cout << "Base3's Val of x:"             << x << endl;    }    void funct3()    {        cout << "Class Base3 is second"             << " Derived class!" << endl;    }};  // Grand-Child_1 of Base1 classclass Derived1 : public Base3 {private:    void funct1()    {        cout << "Derived1::funct1() is called\n";    }    void funct2(int x)    {        cout << "Derived1 Class's Val of x:"             << x << endl;    }    void funct3()    {        cout << "Class Derived1 is Good!!"             << endl;    }};  // Grand-Child_2 of Base1 classclass Derived2 : public Base3 {private:    void funct1()    {        cout << "Derived2::funct1()"             << " is called\n";    }    void funct2(int x)    {        cout << "Derived2 Class's Val "             << "of x:" << x << endl;    }    void funct3()    {        cout << "Class Derived2 is Good!!"             << endl;    }};  // Run-Time Polymorphism// in Hierarchical Inheritanceint main(){      // Base1 class's(Parent class's)    // pointer points to Derived1 class    Base1* b1ptr = new Derived1();      // Run-Time Polymorphism    b1ptr->funct1();    Derived2 d2;      // Now the Base1 class pointer    // points to d2 object(Derived2 class)    b1ptr = &d2;      // Run-Time Polymorphism    b1ptr->funct2(30);      // Compile-Time Binding    b1ptr->funct3();      return 0;}
Derived1::funct1() is called
Derived2 Class's Val of x:30
Base1 is the Parent class!

Explanation: Here, the Parent is Base1 and its Grand-children are Derived1 Class and Derived2 class. Even in this case, When the Base1 Class pointer is pointed to Derived1 object or Derived2 object, due to the Virtual Functions (‘VPTR’ and ‘VTABLE’), we can apply Run-Time Polymorphism here.
C++-Inheritance
C++-Virtual Functions
C++
C++ Programs
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Sorting a vector in C++
Polymorphism in C++
Friend class and function in C++
std::string class in C++
Pair in C++ Standard Template Library (STL)
Header files in C/C++ and its uses
Sorting a Map by value in C++ STL
Program to print ASCII Value of a character
How to return multiple values from a function in C or C++?
Shallow Copy and Deep Copy in C++