点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;
}

原文地址:https://www.cnblogs.com/IT-hexiang/p/4084634.html