第13周项目4-立体类族共有的抽象类
2016-05-31 16:40
489 查看
#include "iostream" using namespace std; const double pai=3.1415926; // 抽象立体图形基类 class CSolid { public: virtual double SurfaceArea() const=0; virtual double Volume() const=0; }; // 立方体类 class CCube : public CSolid { public: CCube(double len=0); double SurfaceArea() const; // 求表面积 double Volume() const; // 求体积 private: double length; }; // 立方体类构造函数 CCube::CCube(double len) { length=len; } // 求立方体表面积 double CCube::SurfaceArea() const { double c; c=6*length*length; return c; } // 求立方体体积 double CCube::Volume() const { double c; c=length*length*length; return c; } // 球体类 class CBall : public CSolid { private: double radius; // 圆周率 public: CBall(double r=0); double SurfaceArea() const; // 求表面积 double Volume() const; // 求体积; }; // 球体类构造函数 CBall::CBall(double r) { radius=r; } // 求球体表面积 double CBall::SurfaceArea() const { double c; c=4*pai*radius*radius; return c; } // 求球体体积 double CBall::Volume() const { double c; c=pai*radius*radius*radius*4/3; return c; } // 圆柱体类 class CCylinder : public CSolid { private: double radius; double height; public: CCylinder(double r=0,double high=0); double SurfaceArea() const; // 求表面积 double Volume() const; // 求体积 }; // 圆柱体类构造函数 CCylinder::CCylinder(double r,double high) { radius=r; height=high; } // 求圆柱体表面积 double CCylinder::SurfaceArea() const { double c; c=2*pai*radius*radius+2*pai*radius*height; return c; } // 求圆柱体体积 double CCylinder::Volume() const { double c; c=pai*radius*radius*height; return c; } int main( ) { CSolid *p; double s,v; CCube x(30); cout<<"立方体边长为 30 "<<endl; p=&x; s=p->SurfaceArea( ); v=p->Volume( ); cout<<"表面积:"<<s<<endl; cout<<"体积:"<<v<<endl; cout<<endl; CBall y(4.5); cout<<"球体半径为 4.5 "<<endl; p=&y; s=p->SurfaceArea( ); v=p->Volume( ); cout<<"表面积:"<<s<<endl; cout<<"体积:"<<v<<endl; cout<<endl; CCylinder z(10,20); cout<<"圆柱体底面半径、高分别为 10, 20"<<endl; p=&z; s=p->SurfaceArea( ); v=p->Volume( ); cout<<"表面积:"<<s<<endl; cout<<"体积:"<<v<<endl; cout<<endl; return 0; }运行结果:
相关文章推荐
- 奇偶分割数组
- jQuery scroll事件实现监控滚动条分页示例
- Mysql中文乱码问题完美解决方案
- 构建之法阅读笔记05
- Win10(GWX)升级应用 提供一次额外确认已安排升级时间选项
- LeetCode 58. Length of Last Word
- Middle-题目69:330. Patching Array
- C++中指针指向以为二维数组
- Win10怎么关闭欢迎界面?Win10开机欢迎界面关闭教程
- 分数运算符重载(4)
- mysql出现问题
- appcan页面间的通讯
- MongoDB的学习(三)-MongoDB操作符
- 蓝桥杯 基础练习 数列排序
- Delphi iOS
- ubuntu下MySQL无法启动Couldn't find MySQL server (/usr/bin/mysqld_safe)”
- Hadoop之MapReduce分布式计算
- pull方法解析Xml文档
- scala的交互式图表工具wisp
- Middle-题目68:106. Construct Binary Tree from Inorder and Postorder Traversal