forked from ZYNsama666/OOP_resource
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Polymorphism.cpp
50 lines (40 loc) · 1.01 KB
/
Polymorphism.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
#include<iostream>
using namespace std;
class animal{
public:
virtual void speak()=0;
animal(){cout<<"animal 构造函数"<<endl;}
virtual ~animal(){cout<<"animal 析构函数"<<endl;}
};
class cat:public animal{
public:
void speak(){cout<<"喵"<<endl;}
cat(){cout<<"cat 构造函数"<<endl;}
~cat(){cout<<"cat 析构函数"<<endl;}
};
class dog:public animal{
public:
void speak(){cout<<"汪"<<endl;}
dog(){cout<<"dog 构造函数"<<endl;}
~dog(){cout<<"dog 析构函数"<<endl;}
};
int main(){
//直接声明dog对象
dog a;
a.speak();
cout<<"——————————分界线————————————" <<endl;
//直接声明cat对象
cat b;
b.speak();
cout<<"——————————分界线————————————" <<endl;
//动态声明dog对象
animal *c=new dog;
c->speak();
delete c;
cout<<"——————————分界线————————————" <<endl;
//动态声明cat对象
animal *d=new cat;
d->speak();
delete d;
cout<<"——————————分界线————————————" <<endl;
}