2015年大一下第8周项目3-类族的设计
2015-05-14 16:00
225 查看
/* *Copyright (c) 2014,烟台大学计算机学院 *All rights reserved. *文件名称:Annpion.cpp *作者:王耀鹏 *完成日期:2015年5月14日 *版本号:v1.0 * *问题描述:由基类的设计和测试开始,逐渐地完成各个类的设计,求出圆格柱体的表面积、体积并输出并且完成要求的计算任务。 *输入描述:无。 *输出描述:圆柱体的表面积,体积等信息。 */ #include<iostream> using namespace std; class Point { public: Point(double a,double b):x(a),y(b) {}; double getx(); double gety(); void setXY(double a,double b); friend ostream &operator <<(ostream &out,Point &p); protected: double x,y; }; double Point::getx() { return x; } double Point::gety() { return y; } void Point::setXY(double a,double b) { x=a; y=b; } ostream &operator <<(ostream &out,Point &p) { cout<<"("<<p.x<<","<<p.y<<")"<<endl; return out; } class Circle:public Point { public: Circle(double a,double b,double c):Point(a,b),r(c) {}; double area(); double getR(); void setR(double a); friend ostream &operator <<(ostream & out ,Circle &c); protected: double r; }; double Circle::area() { return 3.1415926*r*r; } double Circle::getR() { return r; } void Circle::setR(double a) { r=a; } ostream &operator <<(ostream & out ,Circle &c) { cout<<"该圆是以"<<"("<<c.x<<","<<c.y<<")为圆心,"<<c.r<<"为半径的圆。该圆的面积为:"<<c.area()<<endl; return out; } class Cylinder:public Circle { public: Cylinder(double a,double b,double c,double d):Circle(a,b,c),h(d) {}; double Sarea(); double volume(); double getH(); void setH(double a); friend ostream &operator <<(ostream &out ,Cylinder &c); private: double h; }; double Cylinder::Sarea() { return 3.1415926*2*r*h+3.1415926*r*r*2; } double Cylinder::volume() { return 3.1415926*r*r*h; } double Cylinder::getH() { return h; } void Cylinder::setH(double a) { h=a; } ostream &operator<<(ostream &out ,Cylinder &c) { cout<<"该圆柱是以"<<"("<<c.x<<","<<c.y<<")为中心,"<<c.r<<"为半径,"<<c.h<<"为高的圆柱体。该圆柱体的表面积为:"<<c.Sarea()<<",体积为:"<<c.volume()<<endl; return out; } int main( ) { Cylinder cy1(3.5,6.4,5.2,10); cout<<"\noriginal cylinder:\nx="<<cy1.getx( )<<", y="<<cy1.gety( )<<", r=" <<cy1.getR( )<<", h="<<cy1.getH( )<<"\narea="<<cy1.Sarea() <<",volume="<<cy1.volume()<<endl; cy1.setH(15); cy1.setR(7.5); cy1.setXY(5,5); cout<<"\nnew cylinder:\n"<<cy1; return 0; }
运行结果:
相关文章推荐
- 2015年大一下第8周项目1-存储班长信息的学生类(1)访问权限为public
- 2015年大一下第8周项目1-存储班长信息的学生类(2)访问权限为private
- 2015年大一下第8周项目4-点类派生直线类
- 2015年大一下第3周项目1-三角形的构造函数(2)设计默认构造函数
- 2015年大一下第12周项目2-形状类族的中的纯虚函数
- 2015年大一下第8周项目2-职员有薪水了(1)string类型
- 2015年大一下第8周项目2-职员有薪水了(2)char* 类型
- 2015年大一下第12周项目3--立体类族共有的抽象类
- 2015年第十一周项目四:类族的设计
- 2015年第十一周项目四:类族的设计(2)
- 2015年大一下第8周项目0-是春哥啊
- 2015年大一下第13周项目1-小玩文件
- 第十一周【项目1 - 点-圆-圆柱类族的设计2】
- 第十一周上机实践项目4——类族的设计(2)
- 第十一周 项目四(3):类族的设计
- 第十一周项目训练8.3 圆,圆柱类族的设计
- 2015年大一下第3周项目2-分数类的雏形
- 2015年大一下第11周项目4-点、圆的关系(1)重载输入输出函数
- 第十一周项目4-1:类族的设计-基类-点类
- 第10、11周项目1-点-圆-圆柱类族的设计(2)