C++ Polymorphism
Polymorphism
Polymorphism means "many forms", and it occurs when we have many classes that are related to each other by inheritance.
As you learned in the Inheritance chapter, inheritance lets us inherit attributes and methods from another class. Polymorphism uses those methods to perform different tasks. This allows us to perform a single action in different ways.
For example, imagine a base class Animal with a method called animalSound(). Derived classes of Animals could be Pigs, Cats, Dogs, Birds, etc. Every animal can "make a sound", but each one sounds different:
- Pig: wee wee
- Dog: bow wow
- Bird: tweet tweet
This is polymorphism - the same action (making a sound) behaves differently for each animal:
Example
// 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";
}
};
Remember from the Inheritance chapter that we use the : symbol to inherit from a class.
Now we can create Pig and
Dog objects and override the animalSound() method:
Example
// 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;
}
Try it Yourself »
Why And When To Use "Inheritance" and "Polymorphism"?
- Inheritance lets you reuse attributes and methods of an existing class when you create a new class.
- Polymorphism lets related classes share the same method name, while each class does the work in its own way.
In the example above, we call animalSound() directly on each object, so C++ always knows which version to use. In the next chapter, you will see what happens when you call it through a pointer to the Animal class, and why you then need virtual functions.