• 实验十 继承


    A. 圆和圆柱体计算(继承)

    定义一个CPoint点类,包含数据成员x,y(坐标点)。

    以CPoint为基类,派生出一个圆形类CCircle,增加数据成员r(半径)和一个计算圆面积的成员函数。

    再以CCircle做为直接基类,派生出一个圆柱体类CCylinder,增加数据成员h(高)和一个计算体积的成员函数。

    生成圆和圆柱体对象,调用成员函数计算面积或体积并输出结果。

    输入

    输入圆的圆心位置、半径

    输入圆柱体圆心位置、半径、高

    输出

    输出圆的圆心位置 半径

    输出圆面积

    输出圆柱体的圆心位置 半径 高

    输出圆柱体体积

    输入样例1

    0 0 1
    1 1 2 3

    输出样例1

    Circle:(0,0),1
    Area:3.14
    Cylinder:(1,1),2,3
    Volume:37.68
     

    1. #include<iostream>
    2. #include <cstring>
    3. using namespace std;
    4. class Cpoint
    5. {
    6. protected:
    7. int x, y;
    8. public:
    9. Cpoint(int xx, int yy) :x(xx), y(yy)
    10. {
    11. }
    12. };
    13. class Ccircle :public Cpoint
    14. {
    15. protected:
    16. int r;
    17. public:
    18. float Area()
    19. {
    20. return 3.14 * r * r;
    21. }
    22. //注意子类构造语法
    23. Ccircle(int xx, int yy, int rr) :Cpoint(xx, yy), r(rr)
    24. {
    25. }
    26. void print()
    27. {
    28. cout << "Circle:(" << x << "," << y << ")," << r << endl;
    29. cout << "Area:" << Area() << endl;
    30. }
    31. };
    32. class CCylinder :public Ccircle
    33. {
    34. protected:
    35. int h;
    36. public:
    37. float volume()
    38. {
    39. return 3.14 * r * r * h;
    40. }
    41. CCylinder(int xx, int yy, int rr, int hh) :Ccircle(xx, yy, rr), h(hh)
    42. {
    43. }
    44. void print() {
    45. cout << "Cylinder:(" << x << "," << y << ")," << r << "," << h << endl;
    46. cout << "Volume:" << volume() << endl;
    47. }
    48. };
    49. int main()
    50. {
    51. int x, y, r, h;
    52. cin >> x >> y >> r;
    53. Ccircle c1(x, y, r);
    54. c1.print();
    55. cin >> x >> y >> r >> h;
    56. CCylinder c2(x, y, r, h);
    57. c2.print();
    58. }

    B. 三维空间的点(继承)

    题目描述

    定义一个平面上的点C2D类,它含有一个getDistance()的成员函数,计算该点到原点的距离;从C2D类派生出三维空间的点C3D类,它的getDistance()成员函数计算该点到原点的距离。试分别生成一个C2D和C3D的对象,计算它们到原点的距离。
    三维空间的两点(x, y, z)和(x1, y1, z1)的距离公式如下:
    [(x-x1)^2+(y-y1)^2+(z-z1)^2]^(1/2) 

    输入

    第一行二维坐标点位置

    第二行三维坐标点位置1

    第三行三维坐标点位置2

    输出

    第一行二维坐标点位置到原点的距离

    第二行三维坐标点位置1到原点的距离

    第三行三维坐标点位置2到原点的距离

    第四行三维坐标点位置2赋值给二维坐标点变量后,二维坐标点到原点的距离

    输入样例1 

    3 4
    3 4 5
    6 8 8

    输出样例1

    5
    7.07107
    12.8062
    10

    1. #include<iostream>
    2. #include <iomanip>
    3. #include<cmath>
    4. using namespace std;
    5. class C2d
    6. {
    7. protected:
    8. int x, y;
    9. public:
    10. float getdis()
    11. {
    12. return sqrt(x * x + y * y);
    13. }
    14. C2d(int xx, int yy) :x(xx), y(yy)
    15. {
    16. }
    17. };
    18. class C3d :public C2d
    19. {
    20. protected:
    21. int z;
    22. public:
    23. float getdis()
    24. {
    25. return sqrt(x * x + y * y + z * z);
    26. }
    27. C3d(int xx, int yy, int zz) :C2d(xx, yy), z(zz)
    28. {
    29. }
    30. };
    31. int main()
    32. {
    33. int x, y, z;
    34. cin >> x >> y;
    35. C2d c2(x, y);
    36. cin >> x >> y >> z;
    37. C3d c3_1(x, y, z);
    38. cin >> x >> y >> z;
    39. C3d c3_2(x, y, z);
    40. C2d c2_2(x, y);
    41. //返回的是值 函数需要cout 若为空返回,在函数里面cout
    42. cout<<c2.getdis()<<endl;
    43. cout<<c3_1.getdis()<<endl;
    44. cout<<c3_2.getdis()<<endl;
    45. cout<<c2_2.getdis()<<endl;
    46. }

    C. 时钟模拟(继承)

    题目描述

    定义计数器类,包含保护数据成员value,公有函数increment计数加1。

    定义循环计算器继承计数器类,增加私有数据成员:最小值minValue,maxValue,

    重写公有函数increment,使得value在minValue~maxValue区间内循环+1。

    定义时钟类,数据成员是私有循环计数器对象小时hour、分钟minute、秒second,公有函数time(int s)计算当前时间经过s秒之后的时间,即hour,minute,second的新value值。

    定义时钟类对象,输入当前时间和经过的秒数,调用time函数计算新时间。

    根据题目要求,增加必要的构造函数、析构函数和其他所需函数。

    因为clock和time是系统内置函数,为了避免重名,请不要使用clock或者time作为类名或者函数名

    输入

    第一行测试次数n

    2行一组,第一行为当前时间(小时 分钟 秒),第二行为经过的秒数。

    输出

    输出n行

    每行对应每组当前时间和经过秒数后计算得到的新时间(小时:分钟:秒)。

    输入样例1

    2
    8 19 20
    20
    23 30 0
    1801

    输出样例1

    8:19:40
    0:0:1
     

    1. #include <iostream>
    2. using namespace std;
    3. class jishu
    4. {
    5. protected:
    6. int value;
    7. public:
    8. void increment()
    9. {
    10. value++;
    11. }
    12. jishu(int v):value(v)
    13. {
    14. }
    15. int getvalue()
    16. {
    17. return value;
    18. }
    19. };
    20. class xunhuan :public jishu
    21. {
    22. protected:
    23. int minvalue, maxvalue;
    24. public:
    25. void increment()
    26. {
    27. value++;
    28. if (value == maxvalue)
    29. {
    30. value = minvalue;
    31. }
    32. }
    33. xunhuan(int v,int min,int max):jishu(v),minvalue(min),maxvalue(max)
    34. {
    35. }
    36. };
    37. class shizhong
    38. {
    39. protected:
    40. xunhuan hour, minute, second;
    41. public:
    42. shizhong(int h,int m,int s):hour(h,0,24),minute(m,0,60),second(s,0,60)
    43. {
    44. }
    45. void time(int s)
    46. {
    47. for (int i = 0; i < s; i++) //经过s秒
    48. {
    49. second.increment();
    50. if (second.getvalue() == 0)
    51. {
    52. minute.increment();
    53. if (minute.getvalue() == 0)
    54. {
    55. hour.increment();
    56. }
    57. }
    58. }
    59. }
    60. void print()
    61. {
    62. cout << hour.getvalue() << ":" << minute.getvalue() << ":" << second.getvalue() << endl;
    63. }
    64. };
    65. int main()
    66. {
    67. int n;
    68. cin >> n;
    69. int hour, minute,second,s;
    70. while (n--)
    71. {
    72. cin >> hour >> minute >> second;
    73. cin >> s;
    74. shizhong s1(hour, minute, second);
    75. s1.time(s);
    76. s1.print();
    77. }
    78. }

    E. 学生成绩计算(继承)

    题目描述

    定义Person类具有姓名、年龄等属性,具有输出基本信息的display函数。

    选修《面向对象程序设计》课程的学生在Person类的基础上,派生出子类:免听生和非免听生。子类继承父类成员,新增其他成员、改写display函数。

    非免听生具有平时成绩、考试成绩和总评成绩三个属性,总评成绩根据(平时成绩*40%+考试成绩*60%)计算的结果,85分(包含)以上为A,75分(包含)-85分(不包含)为B,65分(包含)-75分(不包含)为C,60分(包含)-65分(不包含)为D,60分(不包含)以下为F。

    免听生只有考试成绩和总评成绩两个属性,总评成绩100%根据考试成绩对应上述等级制成绩。

    定义上述类并编写主函数,输入类型符号,若输入R,根据学生基本信息、平时成绩和考试成绩,建立非免听生对象,若输入S,根据学生基本信息、考试成绩,建立免听生对象。计算学生的总评成绩,并输出。

    输入

    测试次数t

    随后每行输入学生类型 相关信息,姓名的最大字符长度为20

    输出

    每个学生基本信息和总评成绩

    输入样例1 

    2
    R cindy 18 100 100
    S sandy 28 59

    输出样例1

    cindy 18 A
    sandy 28 F

    1. #include<iostream>
    2. #include<cmath>
    3. #include<cstring>
    4. using namespace std;
    5. class person
    6. {
    7. protected:
    8. string name;
    9. int age;
    10. char level;
    11. public:
    12. void display()
    13. {
    14. cout << name << " "<<age;
    15. }
    16. person(string na, int a) :name(na),age(a)
    17. {
    18. }
    19. };
    20. class r :public person
    21. {
    22. protected:
    23. int pingshi, kaoshi, zong;
    24. public:
    25. r(string na, int a, int p, int k):person(na,a),pingshi(p),kaoshi(k)
    26. {
    27. }
    28. void display()
    29. {
    30. zong = pingshi * 0.4 + kaoshi * 0.6;
    31. if (zong < 60) level = 'F';
    32. if (zong >= 60) level = 'D';
    33. if (zong >= 65) level = 'C';
    34. if (zong >= 75) level = 'B';
    35. if (zong >= 85) level = 'A';
    36. cout << name << " " << age << " " << level << endl;
    37. }
    38. };
    39. class s :public person
    40. {
    41. protected:
    42. int kaoshi, zong;
    43. public:
    44. s(string na, int a, int k) :person(na,a),kaoshi(k)
    45. {
    46. }
    47. void display()
    48. {
    49. zong = kaoshi;
    50. if (zong < 60) level = 'F';
    51. if (zong >= 60) level = 'D';
    52. if (zong >= 65) level = 'C';
    53. if (zong >= 75) level = 'B';
    54. if (zong >= 85) level = 'A';
    55. cout << name << " " << age << " " << level << endl;
    56. }
    57. };
    58. int main()
    59. {
    60. int t;
    61. cin >> t;
    62. char x;
    63. string name;
    64. int age, pingshi, kaoshi;
    65. while (t--)
    66. {
    67. cin >> x;
    68. if (x == 'R')
    69. {
    70. cin >> name >> age >> pingshi >> kaoshi;
    71. r r1(name, age, pingshi, kaoshi);
    72. r1.display();
    73. }
    74. if (x == 'S')
    75. {
    76. cin >> name >> age >> kaoshi;
    77. s s1(name, age, kaoshi);
    78. s1.display();
    79. }
    80. }
    81. }

  • 相关阅读:
    法国乐天下单支付流程,自养号测评技术环境揭秘。
    01【C语言 & 趣味算法】百钱百鸡问题(问题简单,非初学者请忽略叭)。请注意算法的设计(程序的框架),程序流程图的绘制,算法的优化。
    创建UI账号密码登录界面
    【Vuex】vue状态机详解
    【vue.js】使用高德地图选择省市区后,再点击确认当前选择的位置
    深入理解ngx_http_upstream_vnswrr_module负载均衡模块
    NPM 设置淘宝cnpm 镜像
    【八股】synchronized
    CentOS 7 安装新版本的Nginx
    前端工程化精讲第二十课 流程优化:部署流程中的构建流程策略优化
  • 原文地址:https://blog.csdn.net/fangyuxuan123/article/details/127674098