Implementing polymorphically

Apr 22, 2019 at 12:59pm

We want to allow our pets to communicate, so let's add a public member function to the Pet class called speak(). This should be implemented polymorphically so that when you ask a Cat instance to speak(), it will write "Meow" to stdout, and if you ask a Dog to speak(), it will write "Woof" to stdout.


What should this look like and what would be a good way to understand Polymorphically?

The example problem I'm working through is fairly straight forward and I've gotten to the above instruction, I was hoping to find some guidance. Thank you.

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
  class Pet
{
public:
    std::string petName;

    Pet();

    Pet(std::string petName,);

    ~Pet();

    void speak();


};

class Dog : public Pet
{

};

class Cat : public Pet
{

};
Apr 22, 2019 at 1:18pm
In polymorphism, both of the classes that extends the superclass (Dog and Cat) must implement differently the methods that are performed differently by each of them. So, when you extend a superclass that has the void speak() method in it, if you want them to perform differently the speak method, you should implement it again on both subclasses; Something like that:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

class Dog : public Pet {
    void speak(){
	cout << "bark" << endl;
    }
	
};

class Cat : public Pet {
    void speak(){
	cout << "meow" << endl;
    }
	
};
Apr 22, 2019 at 1:18pm
Dynamic polymorphism is done in C++ through inheritance and virtual member functions.

In the base class, Pet, you need to declare a virtual function.
e.g.
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Example program
#include <iostream>
#include <string>

class Pet
{
  public:   
    virtual ~Pet() { }   
    virtual void speak();
};

void Pet::speak()
{
    std::cout << "Generic pet sound!\n";   
}

class Dog : public Pet
{
  public:
    void speak() override;
};

void Dog::speak()
{
    std::cout << "Woof!\n";   
}

class Cat : public Pet
{
  public:
    void speak() override;
};

void Cat::speak()
{
    std::cout << "Meow!\n";
}

void func(Pet& pet) // passed by REFERENCE
{
    pet.speak();
}

int main()
{
    
    //
    // Polymorphism can be invoked through a pointer:
    //
    
    Pet* pet = new Pet;
    Dog* dog = new Dog;
    Cat* cat = new Cat;
    
    pet->speak();
    dog->speak();
    cat->speak();
    
    delete pet;
    delete dog;
    delete cat;
    
    //
    // or through references:
    //
    
    Pet pet2;
    Dog dog2;
    Cat cat2;
    
    Pet& pet2_ref = pet2;
    Dog& dog2_ref = dog2;
    Cat& cat2_ref = cat2;
    
    pet2_ref.speak();
    dog2_ref.speak();
    cat2_ref.speak();
       
    //
    // Even more useful when passing to a function:
    //
    
    func(dog2);
}



Generic pet sound!
Woof!
Meow!

Generic pet sound!
Woof!
Meow!

Woof!


The usefulness of this shows itself when passing a polymorphic object to a function.

You can pass a dog object to something expecting a Pet reference, and the behavior will differ based on what the subclass of the reference is. See the "func" function.
Last edited on Apr 22, 2019 at 1:27pm
Topic archived. No new replies allowed.