一、题目描述
定义一个CPoint点类,包含数据成员x,y(坐标点)。
以CPoint为基类,派生出一个圆形类CCircle,增加数据成员r(半径)和一个计算圆面积的成员函数。
再以CCircle做为直接基类,派生出一个圆柱体类CCylinder,增加数据成员h(高)和一个计算体积的成员函数。
生成圆和圆柱体对象,调用成员函数计算面积或体积并输出结果。
二、输入与输出
1.输入
输入圆的圆心位置、半径
输入圆柱体圆心位置、半径、高
0 0 1
1 1 2 3
2.输出
输出圆的圆心位置 半径
输出圆面积
输出圆柱体的圆心位置 半径 高
输出圆柱体体积
Circle:(0,0),1
Area:3.14
Cylinder:(1,1),2,3
Volume:37.68
三、参考代码
#include <iostream>
#include <string>
#include <cmath>
#include <iomanip>
using namespace std;
class Cpoint
{
double x;
double y;
public:
Cpoint(double x1,double y1):x(x1),y(y1){};
double getx()
{
return x;
}
double gety()
{
return y;
}
void pri1()
{
cout<<"Circle:("<<getx()<<","<<gety()<<"),";
}
};
class Ccir:public Cpoint
{
double r;
public:
Ccir(double x1,double y1,double r1):Cpoint(x1,y1),r(r1)
{};
void pri2()
{
cout<<r<<endl;
cout<<"Area:"<<getarea()<<endl;
}
double getarea()
{
return 3.14*r*r;
}
double getr()
{
return r;
}
};
class Ccy:public Ccir
{
double h;
public:
Ccy(double x1,double y1,double r1,double h1):Ccir(x1,y1,r1),h(h1){};
void pri3()
{
cout<<"Cylinder:("<<getx()<<","<<gety()<<"),"<<getr()<<","<<h<<endl;
cout<<"Volume:"<<getv()<<endl;
}
double getv()
{
return getarea()*h;
}
};
int main()
{
double x1,y1,r1;
double x2,y2,r2,h2;
cin>>x1>>y1>>r1;
Cpoint p1(x1,y1);
p1.pri1();
Ccir p2(x1,y1,r1);
p2.pri2();
cin>>x2>>y2>>r2>>h2;
Ccy p3(x2,y2,r2,h2);
p3.pri3();
return 0;
}