I need help understanding Polymorphism

What's the difference between polymorphsm and inheritance?

Here is my explaintion in code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>

using namespace std;

class Mammal {

    bool hair;
    bool warmblooded;

public:
    Mammal() {

        hair = true;
        warmblooded = true;

    }

    bool hasHair() const {
        return hair;
    }

    bool isWarmblooded() const {
        return warmblooded;
    }

    virtual void speak() {};
};

class Dog : public Mammal {

public:
    void speak() {
        cout << "woof" << endl;
    }
};

class Cat : public Mammal {
public:
    void speak() {
        cout << "meow" << endl;
    }
};


int main() {

    Cat aCat;
    Dog aDog;

    //Polymorphism: a dog woofs, and cat meows when the both .speak()
    aCat.speak();
    aDog.speak();


    // Inheritance: a dog and a cat both have hair, because they are both Mammals
    if (aCat.hasHair())
        cout << "a cat has hair" << endl;

    if (aDog.hasHair()) {
        cout << "a dog has hair" << endl;
    }


    return 0;
}
meow
woof
a cat has hair
a dog has hair
Inheritance and Polymorphism are different things so comparing them isn't very easy.

Inheritance is when a class gets methods and stuff from its base class.
1
2
3
4
5
6
7
8
9
class Cat 
{
	public:
	   string getNoise() { return "MEOW"; }
}

class SmallCat : public Cat
{
}

In my example above SmallCat will also have a getNoise() method exactly the same as its base class, this is inherited. you dont need to specify it, your class will automatically get everything the base class has.


Polymorphism is a special kind of overriding. Whats overriding?

1
2
3
4
5
class BigCat : public Cat
{
	public:
	   string getNoise() { return "ROAR"; }
}


Now BigCat still has a getNoise() method but it contains different code to the one in the base class, this is overriding. the return type, name, and parameters must be the same, only the code can be different.

1
2
3
4
SmallCat kitty;
BigCat simba;
cout << kitty.getNoise(); // echo "MEOW"
cout << simba.getNoise(); // echo "ROAR" 


Polymorphism is a solution to a problem, if you dont have the problem, then you dont need to solution. The problem is i want a method that i can give a cat to and it doesnt care what type of cat it gets.

1
2
3
4
void ListenToCat(Cat* pCat)
{
   cout << pCat->getNoise();
}


1
2
3
4
SmallCat kitty;
BigCat simba;
ListenToCat(&kitty); // echo "MEOW"
ListenToCat(&simba); // echo "MEOW"!!!!! 


Why didn't ListenToCat(&simba) "ROAR"?

Its because ListenToCat() doesn't know you passed in a BigCat object! it thinks it's a Cat object (parameter type)
We need a way to call BigCat's overridden getNoise() even if we have a Cat*.
We do this by declaring getNoise() as virtual.

1
2
3
4
5
class Cat 
{
	public:
	   virtual string getNoise() { return "MEOW"; }
}

1
2
3
4
5
class BigCat : public Cat
{
	public:
	   virtual string getNoise() { return "ROAR"; }
}


Now when ListenToCat() calls getNoise() it will get the one from the object you passed instead of the class the parameter was declared as.
1
2
3
4
SmallCat kitty;
BigCat simba;
ListenToCat(&kitty); // echo "MEOW"
ListenToCat(&simba); // echo "ROAR"!!!!! 



This concept of "virtualising" functions is Polymorphism. It lets us create as many classes derived from Cat as we like and ListenToCat() can work with them all by using Cat*.

We can enforce this polymorphism by making Cat::getNoise() "pure virtual" this means all derived classes MUST override getNoise() because the base class doesn't have any code.

1
2
3
4
5
class Cat 
{
	public:
	   virtual string getNoise() = 0;  // = 0 pure virtual
}

1
2
3
4
5
class BigCat : public Cat
{
	public:
	   virtual string getNoise() { return "ROAR"; }
}


As i said, pure virtual forces the override, so we must also give SmallCat an override or it wont compile, there is no getNoise() code to inherit!

1
2
3
4
5
class SmallCat 
{
	public:
	   virtual string getNoise() { return "MEOW"; }
}


With this in mind, Bdanielz example above is not polymorphism, because he always uses the Dog and Cat objects, never using the Mammal to polymorph the different animals. Therefore his example is one of "overriding" and the virtual keywords are moot.
Last edited on
Thanks for pointing that out. I need to work on my example!
Topic archived. No new replies allowed.