polymorphism in c++
Polymorphism in C++ means, the same entity (function or object) behaves differently in different scenarios. Consider this example: The “ +” operator in c++ can perform two specific functions at two different scenarios i.e when the “+” operator is used in numbers, it performs addition.
Source: www.mygreatlearning.com
C++ polymorphism
// Base class class Animal { public: void animalSound() { cout << "The animal makes a sound \n" ; } }; // Derived class class Dog : public Animal { public: void animalSound() { cout << "The dog says: bow wow \n" ; } }; int main() { Animal myAnimal; Dog myDog; myAnimal.animalSound(); myDog.animalSound(); return 0; }
C++ Polymorphism
// 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" ; } };