HOME C C++ PYTHON JAVA HTML CSS JAVASCRIPT BOOTSTRAP JQUERY REACT PHP SQL AJAX JSON DATA SCIENCE AI

C++ Polymorphism

Definition:

Polymorphism means "many forms", and it occurs when we have many classes that are related to each other by inheritance.
Polymorphism is an important concept of object-oriented programming. It simply means more than one form. That is, the same entity (function or operator) behaves differently in different scenarios.


Example:


    #include <iostream>
    #include <string>
    using namespace std;

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

   // Derived class
   class Pig : public Animal {
     public:
        void animalSound() {
        cout << "The pig says: wee wee \n";
    }
  };

   // Derived class
   class Dog : public Animal {
      public:
      void animalSound() {
      cout << "The dog says: bow wow \n";
    }
  };

  int main() {
  Animal myAnimal;
  Pig myPig;
  Dog myDog;

  myAnimal.animalSound();
  myPig.animalSound();
  myDog.animalSound();
  return 0;
  }


Output