-
Notifications
You must be signed in to change notification settings - Fork 7
/
PolymorphismExample.cpp
200 lines (174 loc) · 4.56 KB
/
PolymorphismExample.cpp
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include <iostream>
// 定义动物基类
class Animal
{
public:
std::string name = "Animal";
virtual void eat()
{
std::cout << "Animal is eating..." << std::endl;
}
// 方法重载:同名方法,不同参数列表
virtual void eat(std::string food)
{
std::cout << "Animal is eating " << food << "..." << std::endl;
}
virtual std::string getName()
{
return name;
}
};
// 定义狗类,继承自动物基类
class Dog : public Animal
{
public:
std::string name = "Dog";
// 方法重写:子类重写父类的方法
void eat() override
{
std::cout << "Dog is eating..." << std::endl;
}
// 方法重写:子类重写父类的方法
void eat(std::string food) override
{
std::cout << "Dog is eating " << food << "..." << std::endl;
}
// 子类自有方法
void bark()
{
std::cout << "Dog is barking..." << std::endl;
}
// 方法重写:子类重写父类的方法
std::string getName() override
{
return name;
}
};
// 定义猫类,继承自动物基类
class Cat : public Animal
{
public:
std::string name = "Cat";
// 方法重写:子类重写父类的方法
void eat() override
{
std::cout << "Cat is eating..." << std::endl;
}
// 方法重写:子类重写父类的方法
void eat(std::string food) override
{
std::cout << "Cat is eating " << food << "..." << std::endl;
}
// 子类自有方法
void meow()
{
std::cout << "Cat is meowing..." << std::endl;
}
// 方法重写:子类重写父类的方法
std::string getName() override
{
return name;
}
};
// 定义猪类,继承自动物基类
class Pig : public Animal
{
public:
// 方法重写:子类重写父类的方法
void eat() override
{
std::cout << "Pig is eating..." << std::endl;
}
// 方法重写:子类重写父类的方法
void eat(std::string food) override
{
std::cout << "Pig is eating " << food << "..." << std::endl;
}
// 子类自有方法
void oink()
{
std::cout << "Pig is oinking..." << std::endl;
}
};
int main()
{
// 创建动物数组,类型向上转型,都是 Animal
Animal *animals[4];
animals[0] = new Dog(); // 第一个元素是 Dog 对象
animals[1] = new Cat(); // 第二个元素是 Cat 对象
animals[2] = new Pig(); // 第三个元素是 Pig 对象
animals[3] = new Animal(); // 第四个元素是 Animal 对象
// 遍历动物数组,调用各自的方法
for (int i = 0, count = sizeof(animals) / sizeof(animals[0]); i < count; i++)
{
Animal *animal = animals[i];
std::cout << "name: " << animal->getName() << std::endl;
// 调用对象的 eat 方法
animal->eat();
// 调用对象的 eat 方法的重载形式
animal->eat("some food");
// 类型强制转换,从而可以调用子类的自有方法
if (dynamic_cast<Dog *>(animal))
{
Dog *dog = dynamic_cast<Dog *>(animal);
dog->bark();
}
else if (dynamic_cast<Cat *>(animal))
{
Cat *cat = dynamic_cast<Cat *>(animal);
cat->meow();
}
else if (dynamic_cast<Pig *>(animal))
{
Pig *pig = dynamic_cast<Pig *>(animal);
pig->oink();
}
}
/*** 分割线 ***/
Animal *dog = new Dog(); // 向上转型
Animal *cat = new Cat(); // 向上转型
Animal *pig = new Pig(); // 向上转型
// 多态特性,在运行时根据对象的实际类型来确定,而不是引用的类型。
dog->eat();
((Dog *)dog)->bark();
cat->eat();
((Cat *)cat)->meow();
pig->eat();
((Pig *)pig)->oink();
for (Animal *animal : animals)
{
delete animal;
}
return 0;
}
/**
jarry@jarrys-MBP polymorphism % g++ --version
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include/c++/4.2.1
Apple clang version 12.0.0 (clang-1200.0.32.29)
Target: x86_64-apple-darwin19.6.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
jarry@jarrys-MBP polymorphism % g++ -std=c++11 PolymorphismExample.cpp -o polymorphism_example_cpp
jarry@jarrys-MBP polymorphism % ./polymorphism_example_cpp
name: Dog
Dog is eating...
Dog is eating some food...
Dog is barking...
name: Cat
Cat is eating...
Cat is eating some food...
Cat is meowing...
name: Animal
Pig is eating...
Pig is eating some food...
Pig is oinking...
name: Animal
Animal is eating...
Animal is eating some food...
Dog is eating...
Dog is barking...
Cat is eating...
Cat is meowing...
Pig is eating...
Pig is oinking...
*/