点Point 圆Circle 圆柱Cylinder的继承与派生

#include <iostream.h>
//using namespace std;
class Point
{
private:
	double x;
	double y;
public:
	Point(double x1,double y1)
	{
		x=x1;
		y=y1;
	}
	~Point()//析构函数
	{ cout<<"The constructor of Point was called"<<endl; }


};
class Circle:public Point
{
public:
	double r;
	Circle(double x1,double y1,double r1):Point(x1,y1)//派生类构造函数
	{
		r=r1;//新增量
	}
	~Circle()
	{
		cout<<"The constructor of Circle was called"<<endl;
	}
};

class Cylinder:public Circle
{
private:
	double h;
public:
	Cylinder(double x1,double y1,double r1,double h1):Circle(x1,y1,r1)
	{
		h=h1;//新增量
	}
	~Cylinder()
	{
		cout<<"The constructor of Cylinder was called"<<endl;
	}
	double set_volume();//求体积
	double set_area();//求面积
	friend ostream &operator<<(ostream &output,Cylinder &);//重载输出流
	//void show();
};

double Cylinder::set_area()
{
	return(2*3.14*r*(h+r));
}
double Cylinder::set_volume()
{
	return(3.14*r*r*h);
}

ostream &operator<<(ostream &output,Cylinder &c)
{
	output<<"The area of the Cylinder is "<<c.set_area()<<endl;
	output<<"The volume of the Cylinder is "<<c.set_volume()<<endl;
	return output;
}
int main()
{
	Cylinder c1(3.2,3,2,5);
	c1.set_area();
	c1.set_volume();
	cout<<c1;
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值