#include<iostream> using namespace std; #define PI 3.14 class circle { int r; public: circle(int radius = 0) { r = radius; } void setr(int radius) { r = radius; } int getr() { return r; } double area() { return PI*r*r; } void showcircle() { cout << "Radius:" << r << '\t' << "Area:" << area() << endl; } }; class cylinder: public circle { int h; public: cylinder(int radius = 0, int height = 0) : circle(radius), h(height) {} void seth(int height) { h = height; } int geth() { return h; } double volume() { return area() * h; } void showcylinder() { cout << "Radius:" << getr() << '\t' << "Height:" << h << '\t' << "Volume:" << volume() << endl; } }; int main() { circle c1(2); cylinder c2(2, 3); c1.showcircle(); c2.showcylinder(); return 0; }结果是
时间: 2024-04-04 17:34:52 浏览: 19
运行这段代码会输出以下结果:
Radius:2 Area:12.56
Radius:2 Height:3 Volume:37.68
这段代码定义了两个类,一个是圆形类 `circle`,另一个是圆柱体类 `cylinder`,其中圆柱体类 `cylinder` 继承了圆形类 `circle`。在 `main` 函数中创建了一个半径为 2 的圆形对象 `c1` 和一个半径为 2、高度为 3 的圆柱体对象 `c2`,并分别调用它们的成员函数 `showcircle()` 和 `showcylinder()` 输出它们的半径、面积(对于圆形对象)或体积(对于圆柱体对象)等信息。
相关问题
#include<iostream> using namespace std; #define PI 3.14 class circle { int r; public: circle(int radius=0){r=radius;} void setr(int radius){r=radius;} int getr(){return r;} double area(){return PI*r*r;} void showcircle(){ cout<<"Radius:"<<r<<'\t'<<"Area:"<<area()<<endl;} }; class cylinder: 1 { 2 }; int main() { circle c1(2); cylinder c2(2,3); c1.showcircle(); c2.showcylinder(); return 0; },请补充完整这段代码
下面是完整的程序代码:
```cpp
#include<iostream>
using namespace std;
#define PI 3.14
class circle {
int r;
public:
circle(int radius = 0) { r = radius; }
void setr(int radius) { r = radius; }
int getr() { return r; }
double area() { return PI*r*r; }
void showcircle() {
cout << "Radius:" << r << '\t' << "Area:" << area() << endl;
}
};
class cylinder: public circle {
int h;
public:
cylinder(int radius = 0, int height = 0) : circle(radius), h(height) {}
void seth(int height) { h = height; }
int geth() { return h; }
double volume() { return area() * h; }
void showcylinder() {
cout << "Radius:" << getr() << '\t' << "Height:" << h << '\t' << "Volume:" << volume() << endl;
}
};
int main() {
circle c1(2);
cylinder c2(2, 3);
c1.showcircle();
c2.showcylinder();
return 0;
}
```
在 `main` 函数中,我们先创建了一个圆形对象 `c1` 和一个圆柱体对象 `c2`,并调用它们各自的成员函数 `showcircle` 和 `showcylinder` 分别输出它们的半径、面积和高度、体积等信息。
定义一个圆形类circle为基类,派生出一个圆柱体类cylinder(增加高度属性)。主函数完成对圆柱体类cylinder的成员函数测试。 1)程序代码如下,请补充圆柱体类定义的代码: #include<iostream> using namespace std; #define PI 3.14 class circle { int r; public: circle(int radius=0){r=radius;} void setr(int radius){r=radius;} int getr(){return r;} double area(){return PI*r*r;} void showcircle(){ cout<<"Radius:"<<r<<'\t'<<"Area:"<<area()<<endl;} }; class cylinder: 1 { 2 }; int main() { circle c1(2); cylinder c2(2,3); c1.showcircle(); c2.showcylinder(); return 0; }
以下是圆柱体类cylinder的定义代码:
```
class cylinder: public circle {
int h;
public:
cylinder(int radius=0, int height=0): circle(radius), h(height) {}
void seth(int height) {h = height;}
int geth() {return h;}
double volume() {return area() * h;}
void showcylinder() {
cout << "Radius:" << getr() << "\tHeight:" << h << "\tVolume:" << volume() << endl;
}
};
```
在圆柱体类中,我们使用public继承了圆形类,因此圆柱体类可以访问圆形类的公有成员函数和数据成员。在构造函数中,我们使用圆形类的构造函数初始化半径,同时初始化高度h。新增了高度属性h,以及计算圆柱体体积的函数volume()和显示圆柱体信息的函数showcylinder()。
相关推荐
![text/plain](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)