C++程序设计实验报告(七十二)---第十三周任务四

来源:互联网 发布:win10 ubuntu子系统 编辑:程序博客网 时间:2024/04/28 06:56

/* (程序头部注释开始)
* 程序的版权和版本声明部分
* Copyright (c) 2012, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称:抽象类CSolid

* 作 者: 刘镇
* 完成日期: 2012 年 05 月 13 日
* 版 本 号: 1.068
* 对任务及求解方法的描述部分
* 输入描述: ......

* 问题描述: ......

* 程序输出: ......

* 程序头部的注释结束
*/

 

 

#include<iostream>using namespace std;const double PI = 3.14159;class CSolid{public:virtual double area() const =0;virtual double volume() const =0;};class CCube: public CSolid{public:CCube(double length);virtual double area() const;virtual double volume() const;void show_area();void show_volume();protected:double length;};class CBall: public CSolid{public:CBall(double radius);virtual double area() const;virtual double volume() const;protected:double radius;};class CCylinder: public CSolid{public:CCylinder(double height, double radius);virtual double area() const;virtual double volume() const;void show_area();void show_volume();protected:double height;double radius;};CCube::CCube(double length){this->length = length;}double CCube::area() const{return (this->length * this->length * 6);}double CCube::volume() const{return (this->length * this->length * this->length);}CBall::CBall(double radius){this->radius = radius;}double CBall::area() const{return (4 * PI * this->radius * this->radius);}double CBall::volume() const{return (PI * this->radius * this->radius * this->radius * 3 / 4);}CCylinder::CCylinder(double height, double radius){this->height = height;this->radius = radius;}double CCylinder::area() const{return (2 * (PI * this->radius * this->radius) + this->height * (2 * PI * this->radius));}double CCylinder::volume() const{return (PI * this->radius * this->radius * this->height);}int main(){CSolid *p;CCube ccube(5);CBall cball(4);CCylinder ccylinder(10, 2);p = &ccube;cout << "正方体的表面积是:" << p->area() << endl;cout << "正方体的体积是:" << p->volume() << endl;p = &cball;cout << "球体的表面积是:" << p->area() << endl;cout << "球体的体积是:" << p->volume() << endl;p = &ccylinder;cout << "圆柱体的表面积是:" << p->area() << endl;cout << "圆柱体的体积是:" << p->volume() << endl;system("pause");return 0;}


运行结果:

 

 

 

 

 

 

 

原创粉丝点击