第十周 任务三

╰+哭是因爲堅強的太久メ 2022-06-13 13:52 327阅读 0赞
  1. /* (程序头部注释开始)
  2. * 程序的版权和版本声明部分
  3. * Copyright (c) 2012, 烟台大学计算机学院学生
  4. * All rights reserved.
  5. * 文件名称:继承和派生
  6. * 作 者: 薛广晨
  7. * 完成日期: 2012 年 4 月 21 日
  8. * 版 本 号: x1.0
  9. * 对任务及求解方法的描述部分
  10. * 输入描述: 继承和派生
  11. * 问题描述: 【任务3】
  12. (1)先建立一个Point(点)类,包含数据成员x,y(坐标点);
  13. (2)以Point为基类,派生出一个Circle(圆)类,增加数据成员 (半径);
  14. (3)再以Circle类为直接基类,派生出一个Cylinder(圆柱体)类,再增加数据成员h(高)。
  15. 要求编写程序,设计出各类中基本的成员函数(包括构造函数、析构函数、修改数据成员和获取数据成员的公共接口、用于输出的重载运算符“<<”函数等),使之能用于处理以上类对象,最后求出圆格柱体的表面积、体积并输出。
  16. (提示:此任务可以分为三个子任务分成若干步骤进行。先声明基类,再声明派生类,逐级进行,分步调试。——这种方法适用于做任何的项目)
  17. (1)第1个程序: 基类Point类及用于测试的main()函数
  18. (2)第2个程序:声明Point类的派生类Circle及其测试的main()函数
  19. (3)第3个程序:声明Circle的派生类Cylinder及测试的main()函数
  20. * 程序输出: ......
  21. * 程序头部的注释结束
  22. */
  23. #include<iostream>
  24. #include<Cmath>
  25. #include<iomanip>
  26. #define PI 3.1415926
  27. using namespace std;
  28. class Point //定义坐标点类
  29. {
  30. public:
  31. Point(){x = 0; y = 0;}
  32. Point(double x0, double y0) {x = x0; y = y0;}
  33. ~Point(){}
  34. double getx(){return x;}
  35. double gety(){return y;}
  36. friend ostream &operator << (ostream & input, Point & c);
  37. protected:
  38. double x, y; //点的横坐标和纵坐标
  39. };
  40. ostream &operator << (ostream & output, Point & c)
  41. {
  42. output << "Point:(" << c.x << ", " << c.y << ")";
  43. return output;
  44. }
  45. class Circle : public Point
  46. {
  47. public:
  48. Circle(){r = 0;}
  49. Circle(double x0, double y0, double r); //构造函数
  50. ~Circle(){};
  51. double getr(){return r;}
  52. friend ostream &operator << (ostream & out, Circle & c);
  53. double perimeter0();
  54. double area0();
  55. protected:
  56. double r;
  57. };
  58. Circle :: Circle(double x0, double y0, double r1) : Point(x0, y0), r(r1){} //构造函数
  59. ostream &operator << (ostream & output, Circle & c)
  60. {
  61. output << "以" << "(" << c.getx() << "," << c.gety() << ")" << "为圆心, " << "以" << c.r << "为半径的圆";
  62. return output;
  63. }
  64. double Circle :: perimeter0()
  65. {
  66. return PI * 2 * r;
  67. }
  68. double Circle :: area0()
  69. {
  70. return PI * r * r;
  71. }
  72. class Cylinder : public Circle
  73. {
  74. public:
  75. Cylinder(){height = 0;}
  76. Cylinder(double x1,double y1, double r1, double h);
  77. ~Cylinder(){}
  78. double area1();
  79. double volume();
  80. friend ostream &operator << (ostream & output, Cylinder & c);
  81. protected:
  82. double height;
  83. };
  84. Cylinder :: Cylinder(double x1,double y1, double r1, double h) : Circle(x1, y1, r1), height(h){}
  85. ostream &operator << (ostream & output, Cylinder & c)
  86. {
  87. output << "以" << "(" << c.getx() << "," << c.gety() << ")" << "为圆心, 以" << c.r << "为半径, 以" << c.height << "为高的圆柱体";
  88. return output;
  89. }
  90. double Cylinder :: area1()
  91. {
  92. return (area0() * 2 + perimeter0() * height);
  93. }
  94. double Cylinder :: volume()
  95. {
  96. return area0() * height;
  97. }
  98. int main()
  99. {
  100. Cylinder cy(3, 3, 5, 6);
  101. cout << cy << endl;
  102. cout << setiosflags(ios::fixed) << setprecision(3);
  103. cout << "表面积是:" << cy.area1() << endl;
  104. cout << "体积是:" << cy.volume() << endl;
  105. system("pause");
  106. return 0;
  107. }

1334976397_1860.jpg

发表评论

表情:
评论列表 (有 0 条评论,327人围观)

还没有评论,来说两句吧...

相关阅读

    相关 任务

    要求:请在原类基础上,增加下列成员函数,要求前三个设计成内置函数,在main()数中增加适当的调用以展示扩充类定义后的功能(最好能一次运行)。 add_a_s