点 圆 圆柱的继承实现

我觉得值得注意的是他用了输入输出的运算符重载:

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

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值