2-13-3 立体类族共有的抽象类
来源:互联网 发布:禅道 linux 安装 编辑:程序博客网 时间:2024/06/05 19:11
问题及代码:
#include <iostream>using namespace std;class CSolid{public: virtual double Area()=0; virtual double Volume()=0;};class CCube:public CSolid{public: CCube(double s=1):Side(s){}; double Area(); double Volume();protected: double Side;};class CBall:public CSolid{public: CBall(double R=1):r(R){}; double Area(); double Volume();protected: double r;};class CCylinder:public CSolid{public: CCylinder(double R=1,double h=1):r(R),height(h){}; double Area(); double Volume();protected: double r,height;};double CCube::Area(){ return Side*Side*6;}double CCube::Volume(){ return Side*Side*Side;}double CBall::Area(){ return 4*3.14159*r*r;}double CBall::Volume(){ return (4*3.14159*r*r*r)/3;}double CCylinder::Area(){ return 3.14159*r*r*2+2*3.14159*r*height;}double CCylinder::Volume(){ return 3.14159*r*r*height;}int main(){ CSolid *p; CCube cc0,cc1(3); p=&cc0; cout<<"The area of cc0(Cube) is "<<p->Area()<<endl; cout<<"The volume of cc0(Cube) is "<<p->Volume()<<endl; p=&cc1; cout<<"The area of cc1(Cube) is "<<p->Area()<<endl; cout<<"The volume of cc1(Cube) is "<<p->Volume()<<endl; //以上为用基类指针指向派生类CCube的对象 cout<<endl; CBall cb0,cb1(2); p=&cb0; cout<<"The area of cb0(CBall) is "<<p->Area()<<endl; cout<<"The volume of cb0(CBall) is "<<p->Volume()<<endl; p=&cb1; cout<<"The area of cb1(CBall) is "<<p->Area()<<endl; cout<<"The volume of cb1(CBall) is "<<p->Volume()<<endl; //以上为用基类指针指向派生类CBall的对象 cout<<endl; CCylinder cy0,cy1(4,5); p=&cy0; cout<<"The area of cy0(CCylinder) is "<<p->Area()<<endl; cout<<"The volume of cy0(CCylinder) is "<<p->Volume()<<endl; p=&cy1; cout<<"The area of cy1(CCylinder) is "<<p->Area()<<endl; cout<<"The volume of cy1(CCylinder) is "<<p->Volume()<<endl; //以上为用基类指针指向派生类CCylinder的对象 return 0;}
运行结果:
学习小结:
额,第一次在机房完成一周的项目啊,这周可以干点别的了。
这个项目完整的体验了一遍抽象类及其派生类的设计流程,挺顺利的
加油吧
0 0
- 2-13-3 立体类族共有的抽象类
- 立体类族共有的抽象类
- 立体类族共有的抽象类
- 立体类族共有的抽象类
- 立体类族共有的抽象类
- 立体类族共有的抽象类
- 立体类族共有的抽象类
- 立体类族共有的抽象类
- 立体类族共有的抽象类
- 立体类族共有的抽象类
- 立体类族共有的抽象类
- 立体类族共有的抽象类
- 立体类族共有的抽象类
- 立体类族共有的抽象类
- 立体类族共有的抽象类
- 立体类族共有的抽象类
- 立体类族共有的抽象类
- 立体类族共有的抽象类
- C++之容器
- [Leetcode 217, Easy] Contains Duplicate
- 二叉树节点间最长距离
- Android 实现记住用户名和密码的功能
- 可以看的东西
- 2-13-3 立体类族共有的抽象类
- 只顾自己说,不顾他人感受
- GitHub上史上最全的Android开源项目分类汇总
- 修改UINavigationController的UINavigationItem的颜色
- 自定义日历控件
- DS-Lite,推动网络向IPv6演进
- powerDesigner生成的oracle 建表sql hibernate 无法映射
- set集合容器
- 第十三周项目3立体类族中共有的抽象类