您的位置:首页 > 其它

2015年第十一周项目四:类组的设计(3)

2015-05-20 16:44 225 查看
#include <iostream>

using namespace std;

class Point
{
protected:
double x;
double y;
public:
Point(double,double);
Point(){}
~Point(){}
friend ostream & operator<<(ostream&,const Point &);

};
ostream& operator<<(ostream&output,const Point &p)
{
output<<"("<<p.x<<","<<p.y<<")";
return output;
}
Point::Point(double m,double n)
{
x=m;
y=n;
}
class Circle:public Point
{
protected:
double r;
public:
double area();
Circle(double m,double n,double q):Point(m,n),r(q){}
void setr(double q)
{
r=q;
}
friend ostream& operator <<(ostream&,const Circle&c);
};
ostream& operator <<(ostream&output,const Circle&c)
{
output<<"("<<c.x<<","<<c.y<<","<<c.r<<")";
return output;
}
double Circle::area()
{
return 3.14*r*r;
}

class Cylinder:public   Circle
{
private:
double h;
public:
Cylinder(double m,double n,double q,double a):Circle(m,n,q),h(a){}
double areas();
double volume();
~Cylinder(){}
friend ostream& operator<<(ostream& ,const Cylinder &);
};
double Cylinder::areas()
{
return (2*area()+2*3.14*r*h);
}
double Cylinder::volume()
{
return (area()*h);
}
ostream& operator<<(ostream&output ,const Cylinder &c)
{
output<<"("<<c.x<<","<<c.y<<","<<c.r<<","<<c.h<<")";
return output;
}
int main()
{
Cylinder c(5,6,9,10);
cout<<c<<endl;
cout<<"the area of the cylinder:";
cout<<c.areas()<<endl;
cout<<"the volume of the cylinder:";
cout<<c.volume();
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: