C++ Library Extensions 2022.12.09
To help learn modern C++ programming
27-dynamic_polymorphism.cpp
Go to the documentation of this file.
1#include <tpf_output.hpp>
2
5
6/*
7 https://edotor.net/
8
9 graph Animal
10 {
11 Animal -- Canis;
12 Animal -- Bird;
13 Canis -- Wolf;
14 Canis -- Dog;
15
16 Dog -- JindoDog;
17 Dog -- ChowChow;
18
19 Bird -- Sparrow;
20 Bird -- Eagle;
21 Bird -- Parrot;
22
23 }
24 */
25
26class Animal
27{
28 public:
29
30 // once we declare virtual abstract member function
31 // in the base class, all inherited class should define
32 // its body construct
33 virtual void species() = 0;
34 virtual void speak() = 0;
35
36 // this virtual destructor
37 // in the base class of the inheritance chain
38 // makes all inherited classes create a default virtual destructor
39 virtual ~Animal() = default;
40};
41
42class Canis: public Animal
43{
44 public:
45
46 virtual void bark() = 0;
47
48 void species() override
49 {
50 stream <<" Species: Canis ";
51 }
52
53};
54
55class Wolf: public Canis
56{
57 public:
58
59 void bark() override
60 {
61 stream << "Wolf: howling howling "<< endl;
62 }
63
64 void speak() override
65 {
66 stream << " I am a wolf ";
67 bark();
68 }
69};
70
71class Dog: public Canis
72{
73 public:
74
75 void bark() override
76 {
77 stream << " Dog: baw baw baw bow"<< endl;
78 }
79
80 void speak() override
81 {
82 stream << "I am a dog."<<endl;
83 bark();
84 }
85};
86
87class JindoDog: public Dog
88{
89 public:
90
91 void bark() override
92 {
93 stream << " JindoDog: jindo jindo" << endl;
94 }
95
96 void speak() override
97 {
98 stream << " I am a dog from Jindo, Korea " << endl;
99 bark();
100 }
101};
102
103class ChowChow: public Dog
104{
105 public:
106
107 void bark() override
108 {
109 stream << " Chow Chow: chow chow" << endl;
110 }
111
112 void speak() override
113 {
114 stream << " I am a dog from China " << endl;
115 bark();
116 }
117
118};
119
120class Bird: public Animal
121{
122 public:
123
124 virtual void chirp() = 0;
125
126 void species() override
127 {
128 stream <<" Species: Bird ";
129 }
130
131 void speak() override
132 {
133 stream << "I am a bird. " << endl;
134
135 chirp();
136 }
137
138};
139
140class Sparrow: public Bird
141{
142 public:
143
144 void chirp() override
145 {
146 stream << "Sparrow chirping: sparrow sparrow" << endl;
147 }
148};
149
150class Eagle: public Bird
151{
152 public:
153
154 void chirp() override
155 {
156 stream << " Eagle: chirping eagle eagle " << endl;
157 }
158
159};
160
161class Parrot: public Bird
162{
163 public:
164
165 void chirp() override
166 {
167 stream << " Parrot: singing parrow parrow " << endl;
168 }
169};
170
171void animal_speak(Animal& my_pet)
172{
173 my_pet.speak();
174}
175
176void canis_bark(Canis& my_canis)
177{
178 my_canis.bark();
179}
180
181void bird_chirp(Bird& my_bird)
182{
183 my_bird.chirp();
184}
185
187{
188 Wolf w;
189 JindoDog j;
190 ChowChow c;
191 Sparrow s;
192 Eagle e;
193 Parrot p;
194
195 stream <<"Canis can bark"<< endl << endl;
196 canis_bark(w);
197 canis_bark(j);
198 canis_bark(c);
199
200 stream <<"\nBirds can chirp"<< endl << endl;
201 bird_chirp(e);
202 bird_chirp(p);
203
204 stream <<"\nAnimals can speak" << endl << endl;
205 animal_speak(w);
206 animal_speak(j);
207 animal_speak(c);
208 animal_speak(s);
209 animal_speak(e);
210 animal_speak(p);
211}
212
213int main()
214{
215 test_animals();
216}
void animal_speak(Animal &my_pet)
tpf::sstream stream
void test_animals()
void bird_chirp(Bird &my_bird)
void canis_bark(Canis &my_canis)
virtual void species()=0
virtual ~Animal()=default
virtual void speak()=0
void species() override
virtual void chirp()=0
void speak() override
void species() override
virtual void bark()=0
void speak() override
void bark() override
void chirp() override
void bark() override
void speak() override
void chirp() override
void chirp() override
void bark() override
void speak() override
constexpr auto endl
Definition: tpf_output.hpp:973
void speak() override
void bark() override
Stream output operators << are implemented.