What are the different types of polymorphism in C++ ?

  • Polymorphism means multiple forms that means having more than one function with the same name but with different functionalities.
  • In C++ program there are 2 types of polymorphism, they are:
    • Compile-time polymorphism
    • Run-time polymorphism

Compile-time polymorphism

  • At compile time which is implemented known as Compile-time polymorphism. It is otherwise known as static polymorphism.
  • Method overloading is also an example for compile-time polymorphism.
  • Method overloading which allows you to have more than one function with the same function name but with different functionality.

Sample Code

[pastacode lang=”cpp” manual=”using%C2%A0namespace%C2%A0std%3B%C2%A0%C2%A0%0A%0Aclass%C2%A0Multiply%C2%A0%C2%A0%0A%0A%7B%C2%A0%C2%A0%0A%0A%C2%A0%C2%A0%C2%A0public%3A%C2%A0%C2%A0%0A%0A%C2%A0%C2%A0%C2%A0int%C2%A0mul(int%C2%A0a%2Cint%C2%A0b)%C2%A0%C2%A0%0A%0A%C2%A0%C2%A0%C2%A0%7B%C2%A0%C2%A0%0A%0A%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0return(a*b)%3B%C2%A0%C2%A0%0A%0A%C2%A0%C2%A0%C2%A0%7D%C2%A0%C2%A0%0A%0A%C2%A0%C2%A0%C2%A0int%C2%A0mul(int%C2%A0a%2Cint%C2%A0b%2Cint%C2%A0c)%C2%A0%C2%A0%0A%0A%C2%A0%C2%A0%C2%A0%7B%C2%A0%C2%A0%0A%0A%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0return(a*b*c)%3B%C2%A0%C2%A0%0A%0A%C2%A0%C2%A0%7D%C2%A0%C2%A0%0A%0A%C2%A0%7D%3B%C2%A0%C2%A0%0A%0Aint%C2%A0main()%C2%A0%C2%A0%0A%0A%7B%C2%A0%C2%A0%0A%0A%C2%A0%C2%A0%C2%A0%C2%A0Multiply%C2%A0multi%3B%C2%A0%C2%A0%0A%0A%C2%A0%C2%A0%C2%A0%C2%A0int%C2%A0res1%2Cres2%3B%C2%A0%C2%A0%0A%0A%C2%A0%C2%A0%C2%A0%C2%A0res1%3Dmulti.mul(2%2C3)%3B%C2%A0%C2%A0%0A%0A%C2%A0%C2%A0%C2%A0%C2%A0res2%3Dmulti.mul(2%2C3%2C4)%3B%C2%A0%C2%A0%0A%0A%C2%A0%C2%A0%C2%A0%C2%A0cout%3C%3C%22%5Cn%22%3B%C2%A0%C2%A0%0A%0A%C2%A0%C2%A0%C2%A0%C2%A0cout%3C%3Cres1%3B%C2%A0%C2%A0%0A%0A%C2%A0%C2%A0%C2%A0%C2%A0cout%3C%3C%22%5Cn%22%3B%C2%A0%C2%A0%0A%0A%C2%A0%C2%A0%C2%A0%C2%A0cout%3C%3Cres2%3B%C2%A0%C2%A0%0A%0A%C2%A0%C2%A0%C2%A0%C2%A0return%C2%A00%3B%C2%A0%C2%A0%0A%0A%7D%C2%A0%C2%A0″ message=”” highlight=”” provider=”manual”/]

Output

Run-time polymorphism

  • Run-time polymorphism is otherwise known as dynamic polymorphism.
  • Function overriding is an example of runtime polymorphism that means when the child class contains the method which is already present in the parent class.
  • In function overriding, parent and child class both contains the same function with the different definition.
  • The call to the function is determined at runtime is known as runtime polymorphism.

Sample Code

[pastacode lang=”cpp” manual=”using%C2%A0namespace%C2%A0std%3B%C2%A0%C2%A0%0A%0Aclass%C2%A0Base%C2%A0%C2%A0%0A%0A%7B%C2%A0%0A%0A%C2%A0%C2%A0%C2%A0%C2%A0public%3A%C2%A0%C2%A0%0A%0A%C2%A0%C2%A0%C2%A0%C2%A0virtual%C2%A0void%C2%A0show()%C2%A0%C2%A0%0A%0A%C2%A0%C2%A0%C2%A0%C2%A0%7B%C2%A0%C2%A0%0A%0A%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0cout%3C%3C%22Wikitechy%22%3B%C2%A0%C2%A0%0A%0A%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%7D%C2%A0%C2%A0%0A%0A%7D%3B%C2%A0%C2%A0%0A%0Aclass%C2%A0Derived%3Apublic%C2%A0Base%C2%A0%C2%A0%0A%0A%7B%C2%A0%C2%A0%0A%0A%C2%A0%C2%A0%C2%A0%C2%A0public%3A%C2%A0%C2%A0%0A%0A%C2%A0%C2%A0%C2%A0%C2%A0void%C2%A0show()%C2%A0%C2%A0%0A%0A%C2%A0%C2%A0%C2%A0%C2%A0%7B%C2%A0%C2%A0%0A%0A%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0cout%3C%3C%22Welcome%20to%20Wikitechy%22%3B%C2%A0%C2%A0%0A%0A%C2%A0%C2%A0%C2%A0%C2%A0%7D%C2%A0%C2%A0%0A%0A%7D%3B%C2%A0%C2%A0%0A%0A%C2%A0%C2%A0%0A%0Aint%C2%A0main()%C2%A0%C2%A0%0A%0A%7B%C2%A0%C2%A0%0A%0A%C2%A0%C2%A0%C2%A0%C2%A0Base*%C2%A0b%3B%C2%A0%C2%A0%0A%0A%C2%A0%C2%A0%C2%A0%C2%A0Derived%C2%A0d%3B%C2%A0%C2%A0%0A%0A%C2%A0%C2%A0%C2%A0%C2%A0b%3D%26d%3B%C2%A0%C2%A0%0A%0A%C2%A0%C2%A0%C2%A0%C2%A0b-%3Eshow()%3B%C2%A0%C2%A0%0A%0A%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0return%C2%A00%3B%C2%A0%C2%A0%0A%0A%7D%C2%A0%C2%A0″ message=”” highlight=”” provider=”manual”/]

Output

Leave a Reply

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