-
Notifications
You must be signed in to change notification settings - Fork 0
/
11.cpp
95 lines (77 loc) · 1.35 KB
/
11.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
#include<iostream>
using namespace std;
/* 请在这里填写答案 */
/*
已知Pig类是MagicPig类的父类,
根据以下主函数完成上述两个类的定义,
以实现规定的输出。不允许改变主函数的内容。
Pig 申请了空间...
Pig 申请了空间...
MagicPig 申请了空间...
我只是个平凡的猪猪。
我能上树了耶!
Pig 释放了空间...
MagicPig 释放了空间...
Pig 释放了空间...
*/
class Pig{
public:
Pig(){
cout<<"Pig 申请了空间..."<<endl;
}
virtual void climb(){
cout<<"我只是个平凡的猪猪。"<<endl;
}
virtual ~Pig(){
cout<<"Pig 释放了空间..."<<endl;
}
};
class MagicPig:public Pig{
public:
MagicPig(){
cout<<"MagicPig 申请了空间..."<<endl;
}
void climb(){
cout<<"我能上树了耶!"<<endl;
}
~MagicPig(){
cout<<"MagicPig 释放了空间..."<<endl;
}
};
int main()
{
Pig *p1 = new Pig;
Pig *p2 = new MagicPig;
cout << endl;
p1->climb();
p2->climb();
cout << endl;
delete p1;
delete p2;
return 0;
}
//
//class Pig{
// public:
// Pig(){
// cout<<"Pig 申请了空间..."<<endl;
// }
// virtual void climb(){
// cout<<"我只是个平凡的猪猪。"<<endl;
// }
// virtual ~Pig(){
// cout<<"Pig 释放了空间..."<<endl;
// }
//};
//class MagicPig:public Pig{
// public:
// MagicPig(){
// cout<<"MagicPig 申请了空间..."<<endl;
// }
// void climb(){
// cout<<"我能上树了耶!"<<endl;
// }
// ~MagicPig(){
// cout<<"MagicPig 释放了空间..."<<endl;
// }
//};