7-3 类的继承与派生

定义平面二维点类CPoint,有数据成员x坐标,y坐标,函数成员(构造函数复制构造函数、虚函数求面积GetArea,虚函数求体积函数GetVolume、输出点信息函数print。由CPoint类派生出圆类Cirle类(新增数据成员半径radius),函数成员(构造函数、复制构造函数、求面积GetArea,虚函数求体积函数GetVolume、输出圆信息函数print。再由Ccirle类派生出圆柱体Ccylinder类(新增数据成员高度height),函数成员(构造函数、复制构造函数、求表面积GetArea,求体积函数GetVolume、输出圆柱体信息函数print。在主函数测试这个这三个类。

输入格式:

0 0 例如:第一行读入圆心坐标。
1 2 第二行读入半径与高度。

输出格式:

分四行输出,分别代表圆心、底面积、表面积、体积。

输入样例:

在这里给出一组输入。例如:

0 0
1 2

输出样例:

在这里给出相应的输出。例如:

Center:(0,0)
radius=1
height:2
BasalArea:3.14159
SupfaceArea:18.8496
Volume:6.28319

答案代码如下: 

#include<iostream>
#include<iomanip>
#define PI 3.1415926
using namespace std;

class CPoint{
	protected:
	double x,y;
	public:
	   CPoint(double X, double Y):x(X), y(Y){}
	   CPoint(CPoint &cp):x(cp.x), y(cp.y){}
	   virtual double GetArea()=0;
	   virtual double GetVolume()=0;
	   virtual double GetlowArea()=0;
	   void print();
};

class Ccirle:public CPoint{
	protected:
	double radius;
	public:
		Ccirle(double X, double Y, double r):CPoint(X,Y),radius(r){
		}
		Ccirle(Ccirle &cc):CPoint(cc.x,cc.y),radius(cc.radius){}
		double GetArea();
	    double GetVolume();
};
double Ccirle::GetArea(){
	return PI*radius*radius;
}

double Ccirle::GetVolume(){
	return 4/3*radius*radius*radius;
}

class Ccylinder:public Ccirle{
	double height;
	public:
		Ccylinder(double X, double Y, double r, double h):Ccirle(X,Y,r), height(h){
		       cout<< "Center:(" << x << "," << Y << ")" << endl;
		       cout<< "radius=" << radius <<endl;
		       cout<< "height:" << height <<endl;
		       cout<<"BasalArea:"<< GetlowArea() <<endl;
		       cout<<"SupfaceArea:"<< GetArea() <<endl;
		       cout<<"Volume:"<< GetVolume() <<endl;
		}
		Ccylinder(Ccylinder &Cc):Ccirle(Cc.x,Cc.y,Cc.radius),height(Cc.height){}
		double GetArea();
	    double GetVolume();
	    double GetlowArea();
};

double Ccylinder::GetArea(){
	return 2*PI*radius*radius + 2*PI*radius*height;
}

double Ccylinder::GetVolume(){
	return PI*radius*radius*height;
}

double Ccylinder::GetlowArea(){
	return PI*radius*radius;
}


int main(){
	double X, Y, r, h;
	cin >> X >> Y;
    cin >> r >> h;
    Ccylinder(X, Y, r, h);
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值