用c加加写出私有继承的圆柱体的面积
时间: 2023-07-16 12:13:20 浏览: 92
以下是用 C++ 实现私有继承的圆柱体面积的示例代码:
```cpp
#include<iostream>
using namespace std;
class Circle {
private:
double radius;
public:
Circle(double r) {
radius = r;
}
double getArea() {
return 3.14 * radius * radius;
}
};
class Cylinder : private Circle {
private:
double height;
public:
Cylinder(double r, double h) : Circle(r) {
height = h;
}
double getVolume() {
return getArea() * height;
}
};
int main() {
Cylinder cylinder(5.0, 10.0);
cout << "The volume of the cylinder is " << cylinder.getVolume() << endl;
return 0;
}
```
在上述代码中,圆柱体类 `Cylinder` 私有继承自圆类 `Circle`,因此 `Cylinder` 类中的成员函数可以访问 `Circle` 类的公共成员函数和私有成员变量。由于 `Cylinder` 类私有继承了 `Circle` 类,因此在 `main` 函数中不能直接访问 `Circle` 类的成员函数,但是可以通过 `Cylinder` 类的成员函数进行访问。
阅读全文
相关推荐
















