第十三周任务(四)

来源:互联网 发布:网络舆论发展的因素 编辑:程序博客网 时间:2024/06/05 17:27
#include <iostream>using namespace std;const double pi = 3.1415926;class CSolid{public:virtual double area() const = 0;virtual double volume() const = 0;};class CCube:public CSolid{public:CCube(double l):len(l){}~CCube();double area() const;double volume() const;private:double len;};class CBall:public CSolid{public:CBall(double r):rang(r){}~CBall();double area() const;double volume() const;private:double rang;};class CCylinder:public CSolid{public:CCylinder(double R,double h):Rang(R),high(h){}~CCylinder();double area() const;double volume() const;private:double Rang;double high;};CCube::~CCube(){}double CCube::area() const{return 6*len*len;}double CCube::volume() const{return len*len*len;}CBall::~CBall(){}double CBall::area() const{return 4*pi*rang*rang;}double CBall::volume() const{return 1.33*pi*rang*rang*rang;}CCylinder::~CCylinder(){}double CCylinder::area() const{return 2*pi*Rang*high+2*pi*Rang*Rang;}double CCylinder::volume() const{return pi*Rang*Rang*high;}int main( )  {      CSolid *p;      double s,v;      CCube x(30);      cout<<"立方体边长为30"<<endl;      p=&x;      s=p->area( );      v=p->volume( );      cout<<"表面积:"<<s<<endl;      cout<<"体积:"<<v<<endl;      cout<<endl;      CBall y(4.5);      cout<<"球体半径为4.5"<<endl;      p=&y;      s=p->area( );      v=p->volume( );      cout<<"表面积:"<<s<<endl;      cout<<"体积:"<<v<<endl;      cout<<endl;      CCylinder z(10,20);      cout<<"圆柱体底面半径、高分别为10, 20"<<endl;      p=&z;      s=p->area( );      v=p->volume( );      cout<<"表面积:"<<s<<endl;      cout<<"体积:"<<v<<endl;      cout<<endl;      system("pause");      return 0;  }  /*int main(){CCube c1(1);CBall c2(2);CCylinder c3(3,4);CSolid *p;p = &c1;cout << "正方体的表面积是:" << p->area() << "体积是:" << p->volume() <<endl;p = &c2;cout << "球体的表面积是:" << p->area() << "体积是:" << p->volume() << endl;p = &c3;cout << "圆柱体的表面积是:" << p->area() << "体积是:" << p->volume() << endl;system("pause");return 0;}*/

感悟:这个任务其实就是任务三的改变而已,没有什么,就是主函数那的问题了,定义指针类的问题,搞定这一切万事大吉,找到书了,所以OK了,但还是用了老师的主函数,看着比我的好多了,我的就陪衬吧。

原创粉丝点击