时间限制1s 内存限制128MB
题目描述:
定义一个CPoint点类,包含数据成员x,y(坐标点)。
以CPoint为基类,派生出一个圆形类CCircle,增加数据成员r(半径)和一个计算圆面积的成员函数。
再以CCircle做为直接基类,派生出一个圆柱体类CCylinder,增加数据成员h(高)和一个计算体积的成员函数。
生成圆和圆柱体对象,调用成员函数计算面积或体积并输出结果。
输入:
输入圆的圆心位置、半径
输入圆柱体圆心位置、半径、高
输出:
输出圆的圆心位置 半径
输出圆面积
输出圆柱体的圆心位置 半径 高
输出圆柱体体积
输入样例:
0 0 1
1 1 2 3
输出样例:
Circle:(0,0),1
Area:3.14
Cylinder:(1,1),2,3
Volume:37.68
#include <iostream>
using namespace std;
class CPoint {
protected:
float x;
float y;
public:
void setpoint() {
cin >> x >> y;
}
};
class CCircle :public CPoint {
protected:
float r;
public:
float carea() {
return 3.14 * r * r;
}
void setr() {
cin >> r;
}
void printcircle() {
cout << "Circle:(" << x << "," << y << ")," << r <<endl;
cout << "Area:"<<carea() << endl;
}
};
class CCylinder :public CCircle{
float h;
public:
float cylinder() {
return carea() * h;
}
void seth() {
cin >> h;
}
void printcylinder() {
cout << "Cylinder:(" << x << "," << y << ")," << r << "," << h << endl;
cout << "Volume:" << cylinder() << endl;
}
};
int main(void)
{
CCircle cc;
CCylinder ccy;
cc.setpoint();
cc.setr();
ccy.setpoint();
ccy.setr();
ccy.seth();
cc.printcircle();
ccy.printcylinder();
}