C++ Polymorphism
C++ Polymorphism
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
C++ Polymorphism
❮ Previous Next ❯
Polymorphism
Polymorphism means "many forms", and it occurs when we have many classes that
are related to each other by inheritance.
Like we specified in the previous 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, think of a base class called Animal that has a method called
animalSound() . Derived classes of Animals could be Pigs, Cats, Dogs, Birds - And
they also have their own implementation of an animal sound (the pig oinks, and the
cat meows, etc.):
Example
// Base class
class Animal {
public:
void animalSound() {
cout << "The animal makes a sound \n";
}
};
// Derived class
class Pig : public Animal {
public:
void animalSound() {
https://www.w3schools.com/cpp/cpp_polymorphism.asp 1/6
1/26/24, 10:23 PM C++ Polymorphism
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";
https://www.w3schools.com/cpp/cpp_polymorphism.asp 2/6
1/26/24, 10:23 PM C++ Polymorphism
}
}; Tutorials Exercises Services Sign Up Log in
HTML CSS
int main() {JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
Animal myAnimal;
Pig myPig;
Dog myDog;
myAnimal.animalSound();
myPig.animalSound();
myDog.animalSound();
return 0;
}
Try it Yourself »
https://www.w3schools.com/cpp/cpp_polymorphism.asp 3/6
1/26/24, 10:23 PM C++ Polymorphism
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
COLOR PICKER
REPORT ERROR
Top Tutorials
HTML Tutorial
CSS Tutorial
https://www.w3schools.com/cpp/cpp_polymorphism.asp 4/6
1/26/24, 10:23 PM C++ Polymorphism
JavaScript Tutorial
How To Tutorial
Tutorials
SQL Tutorial
Exercises Services Sign Up Log in
Python Tutorial
HTML
CSS W3.CSS Tutorial
JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
Bootstrap Tutorial
PHP Tutorial
Java Tutorial
C++ Tutorial
jQuery Tutorial
Top References
HTML Reference
CSS Reference
JavaScript Reference
SQL Reference
Python Reference
W3.CSS Reference
Bootstrap Reference
PHP Reference
HTML Colors
Java Reference
Angular Reference
jQuery Reference
FORUM ABOUT
W3Schools is optimized for learning and training. Examples might be simplified to
improve reading and learning.
Tutorials, references, and examples are constantly reviewed to avoid errors, but we
cannot warrant full correctness
of all content. While using W3Schools, you agree to have read and accepted our
terms of use, cookie and privacy policy.
https://www.w3schools.com/cpp/cpp_polymorphism.asp 5/6
1/26/24, 10:23 PM C++ Polymorphism
W3.CSS.
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
https://www.w3schools.com/cpp/cpp_polymorphism.asp 6/6