按以下提示信息,由基类的设计和测试开始,逐渐地完成各个类的设计,并且完成要求的功能。 ①设计一个Point(点)类,包含数据成员x、y(坐标点)。 ②以Point为基类,派生出一个Circle(圆)类,增加数据成员r(半径)。 ③以Circle类为直接基类,派生出一个Cylinder(圆柱体)类,再增加数据成员h(高)。 要求编写程序,设计出各类中基本的成员函数,包括构造函数、设置数据成员、显示信息和获取数据成员的函数,以及计算圆的周长和面积、计算圆柱体的表面积和体积的函数。
时间: 2024-02-06 07:09:09 浏览: 67
基于Java和Kotlin的Android常用工具类和基类设计源码
以下是基类Point的设计:
```cpp
class Point{
public:
Point(double x_, double y_): x(x_), y(y_){} //构造函数
void setX(double x_){ x = x_; } //设置x坐标
void setY(double y_){ y = y_; } //设置y坐标
double getX() const { return x; } //获取x坐标
double getY() const { return y; } //获取y坐标
void display() const { //显示信息
std::cout << "Point(" << x << ", " << y << ")\n";
}
protected:
double x, y;
};
```
接下来是派生类Circle的设计:
```cpp
class Circle: public Point{
public:
Circle(double x_, double y_, double r_): Point(x_, y_), r(r_){} //构造函数
void setR(double r_){ r = r_; } //设置半径
double getR() const { return r; } //获取半径
double perimeter() const{ //计算周长
return 2 * M_PI * r;
}
double area() const{ //计算面积
return M_PI * r * r;
}
void display() const { //显示信息
std::cout << "Circle(" << x << ", " << y << ", " << r << ")\n";
}
private:
double r;
};
```
最后是派生类Cylinder的设计:
```cpp
class Cylinder: public Circle{
public:
Cylinder(double x_, double y_, double r_, double h_): Circle(x_, y_, r_), h(h_){} //构造函数
void setH(double h_){ h = h_; } //设置高度
double getH() const { return h; } //获取高度
double surfaceArea() const{ //计算表面积
return 2 * Circle::area() + 2 * M_PI * r * h;
}
double volume() const{ //计算体积
return Circle::area() * h;
}
void display() const { //显示信息
std::cout << "Cylinder(" << x << ", " << y << ", " << r << ", " << h << ")\n";
}
private:
double h;
};
```
测试代码如下:
```cpp
int main(){
Point p(1, 2);
p.display();
std::cout << "x: " << p.getX() << ", y: " << p.getY() << '\n';
p.setX(3);
p.setY(4);
p.display();
std::cout << "x: " << p.getX() << ", y: " << p.getY() << '\n';
Circle c(1, 2, 3);
c.display();
std::cout << "x: " << c.getX() << ", y: " << c.getY() << ", r: " << c.getR() << '\n';
std::cout << "perimeter: " << c.perimeter() << ", area: " << c.area() << '\n';
c.setR(4);
c.display();
std::cout << "x: " << c.getX() << ", y: " << c.getY() << ", r: " << c.getR() << '\n';
std::cout << "perimeter: " << c.perimeter() << ", area: " << c.area() << '\n';
Cylinder cy(1, 2, 3, 4);
cy.display();
std::cout << "x: " << cy.getX() << ", y: " << cy.getY() << ", r: " << cy.getR() << ", h: " << cy.getH() << '\n';
std::cout << "surface area: " << cy.surfaceArea() << ", volume: " << cy.volume() << '\n';
cy.setR(5);
cy.setH(6);
cy.display();
std::cout << "x: " << cy.getX() << ", y: " << cy.getY() << ", r: " << cy.getR() << ", h: " << cy.getH() << '\n';
std::cout << "surface area: " << cy.surfaceArea() << ", volume: " << cy.volume() << '\n';
return 0;
}
```
输出结果:
```
Point(1, 2)
x: 1, y: 2
Point(3, 4)
x: 3, y: 4
Circle(1, 2, 3)
x: 1, y: 2, r: 3
perimeter: 18.8496, area: 28.2743
Circle(1, 2, 4)
x: 1, y: 2, r: 4
perimeter: 25.1327, area: 50.2655
Cylinder(1, 2, 3, 4)
x: 1, y: 2, r: 3, h: 4
surface area: 131.946, volume: 113.097
Cylinder(1, 2, 5, 6)
x: 1, y: 2, r: 5, h: 6
surface area: 376.991, volume: 471.238
```
阅读全文