Definition P: - PabitraBhandari/2143-OOP-Bhandari GitHub Wiki

Polymorphism:

Definition:

"Polymorphism" itself means having many forms. It can be defined as the ability of a function to be appear in different forms.

In C++, There are two types of polymorphism:

  1. Run-time Polymorphism
  2. Compile-time Polymorphism

Image:

Example Code:

// Base class
class Animal {
  public:
    void Sound() {
    cout << "The animal makes a sound"<<endl;
  }
};

// Derived class
class Bob : public Animal {
  public:
    void Sound() {
    cout << " wee wee"<<endl;
  }
};

// Derived class
class Peet : public Animal {
  public:
    void Sound() {
    cout << " bow wow"<<endl;
  }
};

Source:

https://www.w3schools.com/cpp/cpp_polymorphism.asp