第十三周实验报告4

* (程序头部注释开始)

 * 程序的版权和版本声明部分

 * Copyright (c) 2011, 烟台大学计算机学院学生 

 * All rights reserved.

 * 文件名称:利用虚函数实现多态性 

* 作    者:        任小宁                

 * 完成日期:     2012    年 5    月21

 * 版 本 号:      201158504431

 * 对任务及求解方法的描述部分

#include<iostream>   

#include<Cmath>   

using namespace std;

const double pi=3.1415926;  

class CSolid //定义抽象基类

{

public:

       virtual double superficial_areas()=0;//表面积

    virtual double volume()=0;    //体积   

};

  

class CCube: public CSolid  //利用抽象基类定义正方体类   

{  

public:  

    CCube(double CCube_long);//构造函数   

    ~CCube(){} 

       double superficial_areas();

       double volume();

private:  

    double CCube_long;  

};

 

class CBall: public CSolid    //利用抽象基类定义球体类     

{  

public:  

    CBall(double CBall_long);//构造函数   

       ~CBall(){}  

       double superficial_areas();

       double volume();  

private:  

    double CBall_long;  

}; 

 

class CCylinder: public CSolid  //利用抽象基类定义圆柱体类   

{  

public:  

    CCylinder(double CCylinder_bottom,double CCylinder_high); //构造函数   

       ~CCylinder(){}  

    double superficial_areas();

       double volume(); 

private:  

    double CCylinder_bottom;

       double CCylinder_high;  

};

CCube::CCube(double CCube_long)//构造函数

{

       this->CCube_long=CCube_long;

}

double CCube::superficial_areas()

{

return (6*CCube_long*CCube_long);

}

double CCube::volume()

{

       return (CCube_long*CCube_long*CCube_long);

}

CBall::CBall(double CBall_long)

{

       this->CBall_long=CBall_long;

}

double CBall::superficial_areas()

{

       return (4*pi*CBall_long*CBall_long);

}

double CBall::volume()

{

       return (4*pi*CBall_long*CBall_long*CBall_long/3);

}

CCylinder::CCylinder(double CCylinder_bottom,double CCylinder_high)

{

       this->CCylinder_bottom=CCylinder_bottom;

       this->CCylinder_high=CCylinder_high;

}

double CCylinder::superficial_areas() 

{

       return (2*pi*CCylinder_bottom*CCylinder_bottom+2*pi*CCylinder_bottom*CCylinder_high);

}

double CCylinder::volume()

{

       return (pi*CCylinder_bottom*CCylinder_bottom*CCylinder_high);

}

int main()

{

       CCube CCube1(12.6);    //建立CCube类对象c1,参数为正方体边长

       CBall CBall1(4.5);       //建立CBall1类对象CBall1,参数为球的半径

       CCylinder CCylinder1(4.5,8.4);    //建立CCylinder类对象CCylinder1,参数为圆柱体的半径和高

       CSolid *p;

       p=&CCube1;

       cout<<"totol of CCube areas="<<p->superficial_areas()<<endl;   //输出表面积

       cout<<"totol of CCube volume="<<p->volume()<<endl;   //输出体积

       p=&CBall1;

       cout<<"totol of CBall areas="<<p->superficial_areas()<<endl;   //输出表面积

       cout<<"totol of CBall volume="<<p->volume()<<endl;   //输出体积

       p=&CCylinder1;

       cout<<"totol of CCylinder areas="<<p->superficial_areas()<<endl;   //输出表面积

       cout<<"totol of CCylinder volume="<<p->volume()<<endl;   //输出体积

       system("pause");

       return 0;

}

输出结果

totol of CCube areas=952.56

totol of CCube volume=2000.38

totol of CBall areas=254.469

totol of CBall volume=381.704

totol of CCylinder areas=364.739

totol of CCylinder volume=534.385

请按任意键继续. . .


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值