c++类——继承(三)

本文最后更新于:1 年前

本部分主要是继承的应用实例:

考察一个点、圆、圆柱体的层次结构。

为其各自设计输出运算符的友元重载。

Point数据成员为x和y,能够重写点位置和获得点位置,能输出点位置;

Circle继承Point,除了Point的成员和功能外,同时拥有自己的数据成员radius,能够获得和重写radius,也可以计算并获得出其面积;

Cylinder继承Circle,除了Circle的成员和功能外,同时拥有自己的数据成员weight,能够获得和重写weight,也可以计算并获得出其面积;

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
#include<iostream>
#include<iomanip>
using namespace std;
class Point
{
friend ostream& operator<<(ostream& output, const Point& p);
protected:
double x;
double y;
public:
Point(double d1 = 0.0, double d2 = 0.0) :x(d1), y(d2) {}
void setPoint(double d1, double d2)
{
x = d1, y = d2;
return;
}
double getX()const { return x; }
double getY()const { return y; }
};
class Circle :public Point
{
friend ostream& operator<<(ostream& output, const Circle& c);
protected:
double radius;
public:
Circle(double r = 0.0, double d1 = 0.0, double d2 = 0.0):radius(r),Point(d1,d2) {}
void setRadius(double r)
{
radius = (r > 0 ? r : 0);
return;
}
double getRadius()const { return radius; }
double area()const
{
return acos(-1) * radius * radius;
}
};
class Cylinder :public Circle
{
friend ostream& operator<<(ostream& output, const Cylinder& c);
protected:
double height;
public:
Cylinder(double h=0.0,double r=0.0,double d1=0.0,double d2=0.0):height(h),Circle(r,d1,d2){}
void setHeight(double h)
{
height = (h > 0 ? h : 0);
return;
}
double getHeight()const { return height; }
double area()const
{
return 2 * Circle::area() + 2 * acos(-1) * radius * height;
}
double volume()const
{
return Circle::area() * height;
}

};

ostream& operator<<(ostream& output, const Point& p)
{
output << "Point: (" << p.x << "," << p.y << ")" << endl << endl;
return output;
}
ostream& operator<<(ostream& output, const Circle& c)
{
output << "Circle:" << endl;
output << "Center: (" << c.x << "," << c.y << ")" << endl;
output << "Radius:" << setiosflags(ios::fixed | ios::showpoint) << setprecision(2) << c.radius << endl;
return output;
}
ostream& operator<<(ostream& output, const Cylinder& c)
{
output << "Cylinder:" << endl;
output << "Center: (" << c.x << "," << c.y << ")" << endl;
output << "Radius:" << setiosflags(ios::fixed | ios::showpoint) << setprecision(2) << c.radius << endl;
output << "Height:" << c.height << endl;
return output;
}
int main()
{
Point p(2.5, 3.5);
cout << "p:" << endl;
cout << p;
p.setPoint(9, 10);
cout << "change Point p:" << endl;
cout << p;

Circle c(8.1, 3.1, 4.1);
cout << "c:" << endl;
cout << c;
cout << "c area:" << c.area() << endl;
cout << endl;
c.setRadius(8);
c.setPoint(1, 2);
cout << "chaneg Circle c:" << endl;
cout << c;
cout << "c area:" << c.area() << endl;
cout << endl;

Cylinder cy(5.5, 2.5, 3.5, 4.5);
cout << "cy:" << endl;
cout << cy;
cout << "cy area:" << cy.area() << endl;
cout << "cy volume:" << cy.area() << endl;
cout << endl;
cy.setHeight(5);
cy.setRadius(2);
cy.setPoint(3, 4);
cout << "chaneg Cylinder cy:" << endl;
cout << cy;
cout << "cy area:" << cy.area() << endl;
cout << "cy volume:" << cy.volume() << endl;

return 0;
}
//运行结果:
//p:
//Point: (2.5, 3.5)
//
//change Point p :
//Point: (9, 10)
//
//c :
// Circle :
// Center : (3.1, 4.1)
// Radius : 8.10
// c area : 206.12
//
// chaneg Circle c :
//Circle:
//Center: (1.00, 2.00)
//Radius : 8.00
//c area : 201.06
//
//cy :
// Cylinder :
// Center : (3.50, 4.50)
// Radius : 2.50
// Height : 5.50
// cy area : 125.66
// cy volume : 125.66
//
// chaneg Cylinder cy :
//Cylinder:
//Center: (3.00, 4.00)
//Radius : 2.00
//Height : 5.00
//cy area : 87.96
//cy volume : 62.83

c++类——继承(三)
https://github.com/xiaohei07/xiaohei07.github.io/2023/04/05/c++类——继承(三)/
作者
07xiaohei
发布于
2023年4月5日
许可协议