点 圆 圆柱的继承实现

我觉得值得注意的是他用了输入输出的运算符重载:
#include<iostream>
using namespace std;
class Point
{
protected:
	double x,y;
public:
	Point(double x=0,double y=0);
	void setPoint(double x,double y);
	double getx(){return x;}
	double gety(){return y;}
	friend ostream & operator<<(ostream &,const Point &);

};
class Cricle:public Point
{
protected:
	double radius;
public:
	Cricle(double r=0,double x=0,double y=0);
	void setCricle(double r);
	double getr(){return radius;}
	double getarea(){return 3.14*radius*radius;}
	friend ostream & operator<<(ostream &,const Cricle &);
};
class Crylinder:public Cricle
{
protected:
	double height;
public:
	Crylinder(double h=0,double r=0,double x=0,double y=0);
	void setCrylinder(double h);
	double geth(){return height;};
	double getarea(){return 2*Cricle::getarea()+2*3.14*radius*height;}
	double getvolume(){return height*Cricle::getarea();}
	friend ostream &operator<<(ostream &,const Crylinder &);
};
Point::Point(double x,double y)
{setPoint(x,y);}
void Point::setPoint(double x,double y)
{this->x=x;this->y=y;}
Cricle::Cricle(double r,double x,double y):Point(x,y){radius=r;}
void Cricle::setCricle(double r){radius=r;}
Crylinder::Crylinder(double h,double r,double x,double y):Cricle(r,x,y){height=h;}
void Crylinder::setCrylinder(double h){height=h;}
ostream &operator<<(ostream & output,const Point& p)
{
	output<<"Point\n"<<"("<<p.x<<","<<p.y<<")"<<endl;
	return output;
}
ostream &operator<<(ostream & output,const Cricle &c)
{
	output<<"Cricle\n"<<"the radius is:"<<c.radius<<"\nthe center is the point:"<<"("<<c.x<<","<<c.y<<");\n";
	return output;
}
ostream & operator<<(ostream & output,const Crylinder &cr)
{
	output<<"Crylinder\n"<<"height:"<<cr.height<<"\tradius:"<<cr.radius<<"\t center point:"<<"("<<cr.x<<","<<cr.y<<");\n";
	
return output;}
int main()
{
	Point p1(1,2);
	p1.setPoint(2,3);
	cout<<p1;
	Cricle c1(3,4,5);
	c1.setCricle(4);
	cout<<c1;
	cout<<"area: "<<c1.getarea()<<endl;
	Crylinder cr(7,8,9,10);
	cr.setCrylinder(10);
	cout<<cr;
	cout<<"area: "<<cr.getarea()<<"\t"<<"volume: "<<cr.getvolume()<<endl;
	return 0;
}


猜你喜欢

转载自blog.csdn.net/weixin_41499217/article/details/80871171