polymorphism in the context of object-oriented programming, is the ability to create a variable, a function, or an object that has more than one form.
Polymorphism is not the same as method overloading or method overriding.[1] Polymorphism is only concerned with the application of specific implementations to an interface or a more generic base class. Method overloading refers to methods that have the same name but different signatures inside the same class. Method overriding is where a subclass replaces the implementation of one or more of its parent's methods. Neither method overloading nor method overriding are by themselves implementations of polymorphism.[2]
Please have a look in the following program.
Just have it compiled and debug. you will see the wonderful nature or polymorphism.
if you dont understand revert back will explain..
source - wikipedia
#include <iostream>
#include <string>
using namespace std;
class Animal {
public:
Animal(const string& name) : name(name) {}
virtual string talk() = 0;
const string name;
};
class Cat : public Animal {
public:
Cat(const string& name) : Animal(name) {}
virtual string talk() { return "Meow!"; }
};
class Dog : public Animal {
public:
Dog(const string& name) : Animal(name) {}
virtual string talk() { return "Woof! Woof!"; }
};
int main() {
Animal* animals[] = {
new Cat("Missy"),
new Cat("Mr. Mistoffelees"),
new Dog("Lassie")
};
for (int i = 0; i < 3; ++i) {
cout << animals[i]->name << ": " << animals[i]->talk() << endl;
delete animals[i];
}
}
</string></iostream>