Click here to Skip to main content
15,914,327 members
Please Sign up or sign in to vote.
1.00/5 (7 votes)
See more:
what is polymorphism??

[edit]Code block removed, spelling corrected: polymorphisum -> polymorphism - OriginalGriff[/edit]
Posted
Updated 4-Sep-11 3:59am
v2
Comments
OriginalGriff 4-Sep-11 9:58am    
Misspelled.
OriginalGriff 4-Sep-11 10:00am    
Reason for my vote of one: Too lazy to google.

Google is your friend: Be nice and visit him often. He can answer questions a lot more quickly than posting them here...


http://www.google.co.uk/search?sourceid=chrome&ie=UTF-8&q=polymorphism[^]
Top result: Wikipedia[^]
 
Share this answer
 
Polimorphism is one of the fundamental pillars of Object Oriented Programming. You can't hope to learn it from a 'quick answer'. Buy a (good) OOP book and study it.
 
Share this answer
 
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





C++
#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!"; }
};
 
// prints the following:
//
// Missy: Meow!
// Mr. Mistoffelees: Meow!
// Lassie: 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>
 
Share this answer
 
It[^] could be also designed :) :
C++
class Animal : public Individual
{
 //..
public:
 virtual LPCTSTR SayHello() = 0;
 //..
};

class Cow : public Animal
{
  //..
public:
  virtual LPCTSTR SayHello() { return _T("Muh"); };
  //..
};

class Dog : public Animal
{
  //..
public:
  virtual LPCTSTR SayHello() { return _T("Wow"); };
  //..
};

void test(Covey& covey)
{
  POSITION pos(covey.GetFirstPosition());
  while (pos) {
    Animal* pAnimal(covey.GetNext(pos));
    if (pAnimal) {
      pAnimal->SayHello(); // anybody can it in its own way
    }
  }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900