-
Notifications
You must be signed in to change notification settings - Fork 0
/
virtualFunc.cpp
159 lines (135 loc) · 2.99 KB
/
virtualFunc.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
// 虚函数动态多态
// Created by wbzhang on 2020/9/19.
//
#include <iostream>
#include <string>
using namespace std;
struct Point2i
{
int x,y;
};
// 基类 Shape
class Shape
{
protected:
int width;
int height;
public:
static string ShapeName; // 静态成员
static void shapePrint()
{
std::printf("ShapeName is %s in stacic.",ShapeName.c_str() );
}
void setWidth(int w)
{
width = w;
}
void setHeight(int h)
{
height = h;
}
virtual void print()
{
cout<<"This is shape"<<endl;
}
// 构造函数
Shape():width(0),height(0)
{
cout<<"This Shape is constructed."<<endl;
}
// 父类析构函数申明为虚函数
virtual ~Shape()
{
cout<<"This Shape is destroyed."<<endl;
}
};
// 接口类,仅包含纯虚函数,并且不包含其他任何成员与方法
// 无法进行实例化
class PaintMethod
{
public:
virtual void getCube()=0;
virtual void getPencil()=0;
virtual void getBlank()=0;
};
// 基类 PaintCost,抽象类;无法进行实例化
class PaintCost
{
public:
int getCost(int area)
{
return area * 70;
}
// 声明一个纯虚函数
virtual void getCube() =0;
};
// 还有一种所有方法均public给外界调用的叫啥...给忘了,集成类??
// 派生类
class Rectangle: public Shape, public PaintCost
{
private:
Point2i startPt;
Point2i endPt;
public:
int getArea()
{
return (width * height);
}
void print()
{
cout<<"This is Rectangle"<<endl;
}
// 继承了抽象类的纯虚函数,则必须将所有纯虚函数均实现才可以实例化
void getCube()
{
cout<<"Just test for pure_virtual_class."<<endl;
}
// 子类构造函数
Rectangle()
{
cout<<"This Rectangle is constructed."<<endl;
}
// 子类析构函数
~Rectangle()
{
cout<<"This Rectangle is destroyed."<<endl;
}
};
int main(void)
{
// 1.构造函数
cout<<"------ Shape constructor func ------"<<endl;
Shape *shape = new Shape();
cout<<"------ Rectangle constructor func ------"<<endl;
Rectangle *rectangle = new Rectangle();
// 1.1 调用自身打印
cout<<"------ Self print ------"<<endl;
cout<<"Shape print: ";
shape->print();
cout<<"Rectangle print: ";
rectangle->print();
// 1.2 类的静态成员与静态方法,通过::获取
Shape::ShapeName = "I'm BJTUer.";
cout<<Shape::ShapeName<<endl;
Shape::shapePrint();
// 2 动态多态打印
cout<<"------ Dynamic constructor func ------"<<endl;
Shape *shape1 = new Rectangle();
// Shape *shape2 = rectangle;
cout<<"------ Dynamic bind print ------"<<endl;
shape1->print();
// 3 调用析构函数
cout<<"------ shape destructor func------"<<endl;
delete shape;
cout<<"------ rectangle destructor func------"<<endl;
delete rectangle;
cout<<"------ shape1 destructor func------"<<endl;
delete shape1;
return 0;
}
/* 总结:
* 1) 虚函数原理:虚表指针与虚函数表
* 2) 构造函数:先基类,再派生类的成员,再调用派生类构造函数
* 3) 析构函数:先调用子类析构函数,再调用父类析构函数
* 4) 纯虚函数-->接口类,抽象类
* */