【id:68】【20分】A. 圆和圆柱体计算(继承)


一、题目描述

定义一个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;
}



好的,我可以帮你设计一个计算圆柱体的程序,并使用继承来实现代码重用。 首先,我们可以创建一个名为`Circle`的类来计算的面积和周长: ```python class Circle: def __init__(self, radius): self.radius = radius def area(self): return 3.14 * self.radius ** 2 def perimeter(self): return 2 * 3.14 * self.radius ``` 接下来,我们可以创建一个名为`Cylinder`的子类,该子类继承了`Circle`类,并添加了一个高度属性来表示圆柱体的高度。我们还可以定义一个新的方法来计算圆柱体的表面积和体积: ```python class Cylinder(Circle): def __init__(self, radius, height): super().__init__(radius) self.height = height def surface_area(self): return 2 * self.area() + 2 * 3.14 * self.radius * self.height def volume(self): return self.area() * self.height ``` 现在,我们可以创建一个和一个圆柱体对象,并调用它们的方法来计算它们的面积、周长、表面积和体积: ```python # 创建一个对象 circle = Circle(5) print("的面积:", circle.area()) print("的周长:", circle.perimeter()) # 创建一个圆柱体对象 cylinder = Cylinder(5, 10) print("圆柱体的表面积:", cylinder.surface_area()) print("圆柱体的体积:", cylinder.volume()) ``` 输出: ``` 的面积: 78.5 的周长: 31.400000000000002 圆柱体的表面积: 471.0 圆柱体的体积: 785.0 ``` 这样,我们就成功地使用继承来实现了圆柱体计算
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Z1Jxxx

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值