第十三周实验报告(4)

来源:互联网 发布:ocr身份证识别 源码 编辑:程序博客网 时间:2024/06/08 10:07
/* 程序头部注释开始* 程序的版权和版本声明部分* Copyright (c) 2012, 烟台大学计算机学院学生 * Copyright (c) 2012, 烟台大学计算机学院学生 * All rights reserved.* 文件名称:                         * 作    者:        李瑞                   * 完成日期:  2012 年 5月 16日* 版 本 号:        v1.0* 对任务及求解方法的描述部分* 输入描述:…… * 问题描述:…… * 程序输出:……* 程序头部的注释结束*/
/*【任务4】设计一个抽象类CSolid,含有两个求表面积及体积的纯虚函数。设计三个派生类CCube、CBall、           CCylinder,分别表示正方体、球体及圆柱体。在main()函数中,定义基类的指针p(CSolid *p;),   利用p指针,输出正方体、球体及圆柱体对象的表面积及体积。*/#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 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;double length;length = 10 ;CCube ccube(length);
double radius;radius = 10;CBall cball(radius);
double c_height, c_radius;c_height = 1 ;c_radius = 10 ;CCylinder ccylinder(c_height, c_radius);p = &ccube;cout << "棱长为 " <<  length  <<" 的正方体的表面积是:" << p->area() << endl;cout << "体积是:" << p->volume() << endl << endl;p = &cball;cout << "半径为 " <<  radius  << " 的球体的表面积是:" << p->area() << endl;cout << "体积是:" << p->volume() << endl << endl;p = &ccylinder;cout << "高为 " << c_height << " ,底面半径为 " << c_radius << "  的圆柱体的表面积是:" << p->area() << endl;cout << "体积是:" << p->volume() << endl;system("pause");return 0;}


 

结果:

棱长为 10 的正方体的表面积是:600
体积是:1000

半径为 10 的球体的表面积是:1256.64
体积是:2356.19

高为 1 ,底面半径为 10  的圆柱体的表面积是:691.15
体积是:314.159
请按任意键继续. . .

 

 

原创粉丝点击