第十一周项目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; }
运行结果:
很多地方都做得不好 总是觉得忙被时间推着走
相关文章推荐
- 第十一周【项目4 - 类族的设计】(2)
- 第十一周 项目四--类族的设计(3)
- 第十一周项目4-类族的设计
- 第十一周项目1-点-圆-圆柱类族的设计(3)
- 第十一周项目1-点-圆-圆柱类族的设计(2)
- 第十一周上机实践项目4——类族的设计(1)
- 第十一周上机实践项目 项目1-点-圆-圆柱类的设计(2)
- 2015年第十一周项目四:类族的设计
- 2015年第十一周项目四:类组的设计(3)
- 第十一周项目4-类族的设计
- 第十一周【项目1 - 点-圆-圆柱类族的设计2】
- 第十一周 项目四 类族的设计】
- 第十一周 项目4-类族设计(1)
- 第十一周实践项目1.2-点-圆-圆柱类族的设计
- 第十一周项目1-点-圆-圆柱类族的设计(2)
- 第十一周项目4 - 类族的设计
- 2015年第十一周项目四:类族的设计(2)
- 第十一周项目四--类族的设计(子任务)
- 第十一周项目4-2:类族的设计-圆类
- 第十一周项目实践1————点-圆-圆柱类族设计