01./* (程序头部注释开始)
02.* 程序的版权和版本声明部分
03.* Copyright (c) 2011, 烟台大学计算机学院学生
04.* All rights reserved.
05.* 文件名称:renwu.cpp
06.* 作者: 刘文英
07.* 完成日期:2012 年 5 月 14 日
08.* 版 本 号:13.4
09.* 对任务及求解方法的描述部分
10.* 输入描述:略
11.* 问题描述:略
12.* 程序输出:略
13.* 程序头部的注释结束
14.*/
15.#include<iostream>
16.#include<Cmath>
17.using namespace std;
18.const double pi=3.1415926;
19.
20.class CSolid //定义抽象基类
21.{
22.public:
23. virtual double superficial_areas()=0;//表面积
24. virtual double volume()=0; //体积
25.};
26.
27.class CCube: public CSolid //利用抽象基类定义正方体类
28.{
29.public:
30. CCube(double CCube_long);//构造函数
31. ~CCube(){}
32. double superficial_areas();
33. double volume();
34.private:
35. double CCube_long;
36.};
37.
38.class CBall: public CSolid //利用抽象基类定义球体类
39.{
40.public:
41. CBall(double CBall_long);//构造函数
42. ~CBall(){}
43. double superficial_areas();
44. double volume();
45.private:
46. double CBall_long;
47.};
48.
49.class CCylinder: public CSolid //利用抽象基类定义圆柱体类
50.{
51.public:
52. CCylinder(double CCylinder_bottom,double CCylinder_high); //构造函数
53. ~CCylinder(){}
54. double superficial_areas();
55. double volume();
56.private:
57. double CCylinder_bottom;
58. double CCylinder_high;
59.};
60.
61.CCube::CCube(double CCube_long)//构造函数
62.{
63. this->CCube_long=CCube_long;
64.}
65.
66.double CCube::superficial_areas()
67.{
68. return (6*CCube_long*CCube_long);
69.}
70.
71.double CCube::volume()
72.{
73. return (CCube_long*CCube_long*CCube_long);
74.}
75.
76.CBall::CBall(double CBall_long)
77.{
78. this->CBall_long=CBall_long;
79.}
80.
81.double CBall::superficial_areas()
82.{
83. return (4*pi*CBall_long*CBall_long);
84.}
85.
86.double CBall::volume()
87.{
88. return (4*pi*CBall_long*CBall_long*CBall_long/3);
89.}
90.
91.CCylinder::CCylinder(double CCylinder_bottom,double CCylinder_high)
92.{
93. this->CCylinder_bottom=CCylinder_bottom;
94. this->CCylinder_high=CCylinder_high;
95.}
96.
97.double CCylinder::superficial_areas()
98.{
99. return (2*pi*CCylinder_bottom*CCylinder_bottom+2*pi*CCylinder_bottom*CCylinder_high);
100.}
101.
102.double CCylinder::volume()
103.{
104. return (pi*CCylinder_bottom*CCylinder_bottom*CCylinder_high);
105.}
106.
107.int main()
108.{
109. CCube CCube1(12.6); //建立CCube类对象c1,参数为正方体边长
110. CBall CBall1(4.5); //建立CBall1类对象CBall1,参数为球的半径
111. CCylinder CCylinder1(4.5,8.4); //建立CCylinder类对象CCylinder1,参数为圆柱体的半径和高
112. CSolid *p;
113. p=&CCube1;
114. cout<<"totol of CCube areas="<<p->superficial_areas()<<endl; //输出表面积
115. cout<<"totol of CCube volume="<<p->volume()<<endl; //输出体积
116. p=&CBall1;
117. cout<<"totol of CBall areas="<<p->superficial_areas()<<endl; //输出表面积
118. cout<<"totol of CBall volume="<<p->volume()<<endl; //输出体积
119. p=&CCylinder1;
120. cout<<"totol of CCylinder areas="<<p->superficial_areas()<<endl; //输出表面积
121. cout<<"totol of CCylinder volume="<<p->volume()<<endl; //输出体积
122. system("pause");
123. return 0;
124.}
运行结果:
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
请按任意键继续. . .