If you’ve read my 5 minute guide What Is Polymorphism? and want to see it in action, here’s some C++ code that illustrates the example I mentioned in that post.
I’ve kept the code to header files only for brevity, but as you well know, in the real world, objects would be split out into .h and .cpp files. The program consists of the main.cpp file, a base class called Mammal, and three derived classes called Dog, Cat and Mouse.
1. main.cpp
This is where it all comes together and you see polymorphism in action. I’ve declared an array of pointers to the Mammal base class, and for each one have called the SendLoudNoise() method. The output of the program should look like this:
Woof woof! --- Twitch my tail --- --- Sniff the air ---
Which means that although I am calling the method on the base class, it is the derived class that is responding, as it overrides the SendLoudNoise() method in each case.
#include <iostream> #include "Mammal.h" #include "Dog.h" #include "Cat.h" #include "Mouse.h" const int cNumAnimals = 3; int main() { Mammal* pAnimal[cNumAnimals] = { new Dog, new Cat, new Mouse }; for (int i = 0 ; i < cNumAnimals ; ++i) { std::cout << pAnimal[i]->SendLoudNoise() << std::endl; delete pAnimal[i]; } return 0; }
2. Mammal.h
The Mammal class, our base class, defines the virtual method SendLoudNoise(), but we never see the text that this method outputs. This proves that our polymorphism example is working correctly.
Note that methods you want to override must be declared as virtual. If you remove this one keyword, the output of the program would be 3 lines of “I am a generic mammal”!
#ifndef MAMMAL_H_ #define MAMMAL_H_ #include <string> class Mammal { public: Mammal() {}; virtual ~Mammal() {}; virtual std::string SendLoudNoise() { std::string str("I am a generic mammal"); return str; } }; #endif /* MAMMAL_H_ */
3. Dog.h
The next three files are very similar. Each one inherits from the Mammal class and then overrides the SendLoudNoise() method. Aside from that, they are almost identical.
You don’t have to override a method in a derived class. If you wanted default behaviour, you could specify that in your base class and not mention it in your derived class. Then when that method is called, the object would default to using the base class version.
#ifndef DOG_H_ #define DOG_H_ #include "Mammal.h" class Dog : public Mammal { public: Dog() {}; virtual ~Dog() {}; std::string SendLoudNoise() { std::string str("Woof woof!"); return str; } };
4. Cat.h
#ifndef CAT_H_ #define CAT_H_ #include "Mammal.h" class Cat : public Mammal { public: Cat() {}; virtual ~Cat() {}; std::string SendLoudNoise() { std::string str("--- Twitch my tail ---"); return str; } }; #endif /* CAT_H_ */
5. Mouse.h
#ifndef MOUSE_H_ #define MOUSE_H_ #include "Mammal.h" class Mouse : public Mammal { public: Mouse() {}; virtual ~Mouse() {}; std::string SendLoudNoise() { std::string str("--- Sniff the air ---"); return str; } }; #endif /* MOUSE_H_ */
And that’s all there is to it – a very basic example of using polymorphism to enable interaction with different objects via a base class pointer.
I wrote an article on 4 polymorphisms in C++ the other day. Check it out here: http://www.catonmat.net/blog/cpp-polymorphism/
Very interesting article – some terms in there I haven’t seen in use before. I probably should have stated that I was referring specifically to subtype polymorphism, but that’s usually what’s talked about in C++, so I didn’t make it explicit :-).
I’m in Love!
Hi faye williams
My name is kishan and today i saw ur this page and i really like it and your logic.
Actully i search on polymorphism so that’s why i saw this page.
Thank you dear