您的位置:首页 > 其它

第十一周项目4类族的设计

2015-05-20 09:27 176 查看
/*
*Copyright(c)2014,烟台大学计算机学院
*All rights reserved.
*文件名称:test.cpp
*作者:曾晓
*完成日期:2015年 5月 20日
*版本号:v1.0
*/
#include <iostream>
using namespace std;
class Point
{
public:
Point(double x=0,double y=0);
void setPoint(double,double);
double getX( ) const {return x;}
double getY( ) const {return y;}
friend ostream & operator<<(ostream &,const Point &);
protected:
double x,y;
};
Point::Point(double a,double b)
{
x=a;
y=b;
}
void Point::setPoint(double a,double b)
{
x=a;
y=b;
}

ostream & operator<<(ostream &output,const Point &p)
{
output<<"["<<p.x<<","<<p.y<<"]"<<endl;
return output;
}

class Circle:public Point
{
public:
Circle(double x=0,double y=0,double r=0);
void setRadius(double);
double getRadius( ) const;
double area ( ) const;
friend ostream &operator<<(ostream &,const Circle &);
protected:
double radius;
};

Circle::Circle(double a,double b,double r):Point(a,b),radius(r){ }

void Circle::setRadius(double r)
{
radius=r;
}

double Circle::getRadius( ) const
{
return radius;
}
double Circle::area( ) const
{
return 3.14159*radius*radius;
}
ostream &operator<<(ostream &output,const Circle &c)
{
output<<"("<<c.x<<", "<<c.y<<"), r="<<c.radius<<", area="<<c.area( )<<endl;
return output;
}

class Cylinder:public Circle
{
public:
Cylinder (double x=0,double y=0,double r=0,double h=0);
void setHeight(double);
double getHeight( ) const;
double area( ) const;
double volume( ) const;
friend ostream& operator<<(ostream&,const Cylinder&);
protected:
double height;
};

Cylinder::Cylinder(double a,double b,double r,double h) :Circle(a,b,r),height(h){}
void Cylinder::setHeight(double h)
{
height=h;
}
double Cylinder::getHeight( ) const
{
return height;
}
double Cylinder::area( ) const
{
return 2*Circle::area( )+2*3.14159*radius*height;
}

double Cylinder::volume() const
{
return Circle::area()*height;
}

ostream &operator<<(ostream &output,const Cylinder& u)
{
output<<"("<<u.x<<","<<u.y<<"), r="<<u.radius<<", h="<<u.height
<<"area="<<u.area( )<<", volume="<<u.volume( )<<endl;
return output;
}

int main( )
{
Cylinder cy(3.5,6.4,5.2,10);
cout<<"x="<<cy.getX( )<<", y="<<cy.getY( )<<", r="
<<cy.getRadius( )<<", h="<<cy.getHeight( )<<"area="<<cy.area()
<<",volume="<<cy.volume()<<endl;

return 0;
}


运行结果:



很多地方都做得不好    总是觉得忙被时间推着走 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: