虚函数实现多态性 代码参考

 1 #include <iostream>
 2 
 3 using namespace std;
 4 
 5 class Pet
 6 {
 7     public:
 8         virtual void Speak(){cout<<"How does a pet speak?"<<endl;}
 9 };
10 
11 class Cat:public Pet
12 {
13     public:
14         void Speak(){cout<<"miao!miao!"<<endl;}
15 };
16 
17 class Dog:public Pet
18 {
19     public:
20         void Speak(){cout<<"wang!wang!"<<endl;}
21 };
22 
23 int main()
24 {
25     Pet one, *p1,*p2,*p3;
26     Cat two;
27     Dog three;
28     p1=&one;
29     p2=&two;
30     p3=&three;
31     p1->Speak();
32     p2->Speak();
33     p3->Speak();
34     return 0;
35 }
原文地址:https://www.cnblogs.com/Conan-jine/p/12798691.html