点,圆,圆柱体(继承,派生)

按以下的提示,由基类的设计和测试开始,逐渐地完成各个类的设计,求出圆格柱体的表面积、体积并输出并且完成要求的计算任务:
(1)先建立一个Point(点)类,包含数据成员x,y(坐标点),实现需要的成员函数,并设计main函数完成测试;
(2)以Point为基类,派生出一个Circle(圆)类,增加数据成员r(半径),以及求面积的成员函数area,实现其他需要的成员函数,设计main函数完成测试;
(3)再以Circle类为直接基类,派生出一个Cylinder(圆柱体)类,再增加数据成员h(高),,以及求圆柱表面积的成员函数area和求圆柱体积的成员函数volume,实现需要的成员函数,并设计main函数完成测试。
要求编写程序,设计出各类中“需要的成员函数”,包括构造函数、析构函数、修改数据成员和获取数据成员的公共接口、用于输出的重载运算符“<<”函数等。

#include<iostream>
using namespace std;
#define PI 3.14159

class Point
{
public:
	Point(double x=0,double y=0);
	~Point();
	double getx();
	double gety();
	void setpoint(double,double);
protected:
	double x;
	double y;
};

Point::Point(double a,double b)
{
	this->x=a;
	this->y=b;
	cout<<"Point构造函数"<<endl;
}
Point::~Point()
{
	cout<<"Point析构函数"<<endl;
}
double Point::getx()
{
	return x;
}
double Point::gety()
{
	return y;
}
void Point::setpoint(double a,double b)
{
	x=a;
	y=b;
}
class Circle :public Point
{
public:
	Circle(double a=0,double b=0,double c=0);
	~Circle();
	double aera();
	double getRadius();
	void setRadius(double);
//	void show();
protected:
	double radius; 
};
Circle::Circle(double a,double b,double c)
{
	x=a;
	y=b;
	radius=c;
	cout<<"Cilrcle构造函数"<<endl;
}
double Circle::getRadius()
{
	return radius;
}
double Circle::aera()
{
	return PI*radius*radius;
}
void Circle::setRadius(double c)
{
	radius=c;
}
Circle::~Circle()
{
	cout<<"Cilrcle析构函数"<<endl;
}
class Cylinder :public Circle
{
public:
	Cylinder(double a=0,double b=0,double c=0,double h=0);
	~Cylinder();
	void setHeight(double);
	double getHeight();
	double CorArea();
	double volume();
	void show();
protected:
	double height;
};
Cylinder::Cylinder(double a,double b,double c,double h)
{
	x=a;
	y=b;
	radius=c;
	height=h;
	cout<<"Cylinder构造函数"<<endl;
}
Cylinder::~Cylinder()
{
	cout<<"Cylinder析构函数"<<endl;
}
double Cylinder::getHeight()
{
	return height;
}
void Cylinder::setHeight(double h)
{
	height=h;
}
double Cylinder::CorArea()
{
	return 2*PI*radius*height;
}
double Cylinder::volume()
{
	return PI*radius*radius*height;
}
void Cylinder::show()
{
	cout<<"坐标:"<<x<<","<<y<<endl;
	cout<<"底面圆的半径:"<<radius<<endl;
	cout<<"底面圆的面积:"<<Circle::aera()<<endl;
	cout<<"圆柱体的高:"<<Cylinder::getHeight()<<endl;
	cout<<"圆柱体的表面积:"<<Cylinder::CorArea()<<endl;
	cout<<"圆柱体的体积:"<<Cylinder::volume()<<endl;
}
int main()
{
	Cylinder c(1,2,1,4);
	c.show();
	c.setpoint(3,4);
	c.setRadius(2);
	c.setHeight(5);
	c.show();
	return 0;
}

运行结果:


  • 7
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是求球体和圆柱体的体积和表面积的c++代码: ```c++ #include <iostream> using namespace std; const double PI = 3.1415926535; // 基 Circle class Circle { protected: double radius; // 半径 public: Circle(double r): radius(r) {} virtual ~Circle() {} virtual void showInfo() { cout << "半径:" << radius << endl; } virtual double getArea() { return PI * radius * radius; } }; // 派生 Sphere class Sphere: public Circle { public: Sphere(double r): Circle(r) {} ~Sphere() {} void showInfo() { cout << "---球体信息---" << endl; Circle::showInfo(); } double getArea() { return 4 * PI * radius * radius; } double getVolume() { return 4.0 / 3 * PI * radius * radius * radius; } }; // 派生 Column class Column: public Circle { private: double height; // 高 public: Column(double r, double h): Circle(r), height(h) {} ~Column() {} void showInfo() { cout << "---圆柱体信息---" << endl; Circle::showInfo(); cout << "高:" << height << endl; } double getArea() { return 2 * PI * radius * height + 2 * PI * radius * radius; } double getVolume() { return PI * radius * radius * height; } }; int main() { Sphere s(2.0); Column c(1.0, 3.0); s.showInfo(); cout << "表面积:" << s.getArea() << endl; cout << "体积:" << s.getVolume() << endl; c.showInfo(); cout << "表面积:" << c.getArea() << endl; cout << "体积:" << c.getVolume() << endl; return 0; } ``` 输出结果如下: ``` ---球体信息--- 半径:2 表面积:50.2655 体积:33.5103 ---圆柱体信息--- 半径:1 高:3 表面积:18.8496 体积:3.14159 ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值