类的继承与派生

定义平面二维点类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>
using namespace std;
class CPoint {
public:
	CPoint(double x,double y) {
		this->x = x;
		this->y = y;
	}
	CPoint(CPoint& c)
		:x(c.x),y(c.y)
	{}
	virtual double GetArea() { ; }
	virtual double GetVolume() { ; }
	void print() {
		cout << "Center:(" << x << "," << y << ")" << endl;
	}
private:
	double x, y;
};

class Cirle :public CPoint{
public:
	Cirle(double x,double y,double radius):CPoint(x,y){
		this->radius = radius;
	}
	Cirle(Cirle& c):CPoint(c), radius(c.radius){}
	double GetRadius() { return radius; }
	double GetArea()
	{
		return   3.1415926 * radius * radius * 2;
	}
	double virtual GetVolume()
	{
		return 3 * 3.1415926 * radius * radius * radius / 4.0;
	}
	void print() {
		cout << "radius=" << Cirle::GetRadius() << endl;
	}
	double radius;
};

class Ccylinder:public Cirle {
public:
	Ccylinder(double x,double y,double radius,double height):Cirle(x,y,radius){
		this-> height = height;
	}
	Ccylinder(Ccylinder& cc):Cirle(cc),height(cc.height){}
	double GetArea() {
		return 3.1415926 * radius * radius;
	}
	double GetHeight() { return height; }
	double GetVolume() {
		return 3.1415926 * radius * radius * height;
	}
	void print() {
		cout << "height:" << GetHeight() << endl;
		cout << "BasalArea:" << GetArea() << endl;
		cout << "SupfaceArea:" << 2 * GetArea() + 2 * 3.1415926 * Cirle::GetRadius() * height << endl;
		cout << "Volume:" << GetVolume() << endl;
	}
	double height;
 };
int main() {
	double x, y, radius, height;
	cin >> x >> y >> radius >> height;
	CPoint c(x, y);
	Cirle cc(x, y, radius);
	Ccylinder ccc(x, y, radius, height);
	c.print();
	cc.print();
	ccc.print();
	return 0;
}

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值