• 福州大学 2022~2023 学年第 1 学期考试 A 卷压轴题参考答案


     题目:

    定义一个抽象类Structure(含有纯虚函数type函数,用以显示当前结构的类型; 含有show函数),
    在此基础上派生出Building类, 用来存储一座楼房的层数、房间数以及它的总平方米数。 建立派生
    类House,继承Building类,并存储下面的内容:卧室与浴室的数量。另外,建立派生类Office,继承
    Building类,并存储灭火器与电话的个数。要求定义各个类的数据成员,成员函数(包括构造函数、
    析构函数、显示各数据成员的show()函数,type()函数)。
    (1)在完成上述类的基础上,在main函数中定义相应的对象,并测试所有的函数功能。
    (2)在main()中,用Office类定义一个对象,请分析此时调用构造函数的顺序和析构函数的顺序。
    (3)实现Structure类族中show()函数的动态多态性,要求要在每个层级都实现,并调用测试。
    (4)在Office类中重载运算符“<<”,以便通过它来直接对Office类对象内容直接输出。
    (5)调用“<<”运算符将Office对象的信息输出到文本文件“d:\\office.txt”中。

    程序:

    1. #include
    2. #include//(5)文件读写操作需要用到的头文件
    3. using namespace std;
    4. class Structure {//抽象类
    5. public:
    6. virtual void type() = 0;
    7. virtual void show() = 0;//(3)纯虚函数,Structure类族动态多态性
    8. };
    9. class Building :public Structure {
    10. protected:
    11. int floor;//记录层数
    12. int room;//记录房间数
    13. int square;//记录平方米数
    14. public:
    15. Building(int f = 1, int r = 0, int s = 0) {
    16. this->floor = f;
    17. this->room = r;
    18. this->square = s;
    19. cout << "the constructor function of Building has been called" << endl;//验证(2)构造函数调用顺序
    20. return;
    21. }
    22. ~Building() {
    23. cout << "the destructor function of Building has been called" << endl;//验证(2)析构函数调用顺序
    24. }
    25. public:
    26. virtual void type() {
    27. cout << "The type is Building" << endl;
    28. return;
    29. }
    30. virtual void show() {//(3)虚函数,Structure类族动态多态性
    31. cout << "floor:" << floor << endl;
    32. cout << "room:" << room << endl;
    33. cout << "square:" << square << "m^2" << endl;
    34. return;
    35. }
    36. };
    37. class House :public Building {
    38. protected:
    39. int bedroom;//记录卧室数
    40. int bathroom;//记录浴室数量
    41. public:
    42. House(int floor = 1, int room = 0, int square = 0, int bedroom = 0, int bathroom = 0)
    43. :Building(floor, room, square) {//重载构造函数(参数表法)
    44. this->bedroom = bedroom;
    45. this->bathroom = bathroom;
    46. return;
    47. }
    48. ~House() { }
    49. public:
    50. void type() {
    51. cout << "The type is House" << endl;
    52. }
    53. void show() {
    54. cout << "floor:" << floor << endl;
    55. cout << "room:" << room << endl;
    56. cout << "bedroom:" << bedroom << endl;
    57. cout << "bathroom" << bathroom << endl;
    58. cout << "square:" << square << "m^2" << endl;
    59. return;
    60. }
    61. };
    62. class Office :public Building {
    63. protected:
    64. int hydrant;//记录灭火器数
    65. int telephone;//记录电话数
    66. public:
    67. Office(int floor = 1, int room = 0, int square = 0, int hydrant = 0, int telephone = 0)
    68. :Building(floor, room, square) {//重载构造函数(参数表法)
    69. this->telephone = telephone;
    70. this->hydrant = hydrant;
    71. cout << "the constructor function of Office has been called" << endl;//验证(2)构造函数调用顺序
    72. return;
    73. }
    74. ~Office() {
    75. cout << "The destructor function of Office has been called" << endl;//验证(2)析构函数调用顺序
    76. };
    77. public:
    78. friend void operator<<(ostream&, Office&);//(4)运算符重载函数声明,流插入运算符只能重载为友元函数
    79. public:
    80. void type() {
    81. cout << "The type is Office" << endl;
    82. }
    83. void show() {
    84. cout << "floor:" << floor << endl;
    85. cout << "room:" << room << endl;
    86. cout << "hydrant:" << hydrant << endl;
    87. cout << "telephone" << telephone << endl;
    88. cout << "square:" << square << "m^2" << endl;
    89. return;
    90. }
    91. public:
    92. void document() {//(5)文件读写操作
    93. ofstream outfile("d:\\office.txt", ios::out);
    94. if (!outfile) {
    95. cerr << "OPEN ERROR!" << endl;
    96. exit(1);
    97. }
    98. outfile << "floor:" << floor << endl;
    99. outfile << "room:" << room << endl;
    100. outfile << "hydrant:" << hydrant << endl;
    101. outfile << "telephone" << telephone << endl;
    102. outfile << "square:" << square << "m^2" << endl;
    103. return;
    104. }
    105. };
    106. void operator <<(ostream& output, Office& of) {//(4)重载运算符,用于直接输出office类
    107. cout << "floor:" << of.floor << endl;
    108. cout << "room:" << of.room << endl;
    109. cout << "hydrant:" << of.hydrant << endl;
    110. cout << "telephone" << of.telephone << endl;
    111. cout << "square:" << of.square << "m^2" << endl;
    112. return;
    113. }
    114. int main() {
    115. Building bu(2, 3, 100);
    116. House ho(2, 4, 100, 3, 2);
    117. Office of(3, 10, 300, 5, 4);
    118. //(1)测试部分程序
    119. bu.show();
    120. bu.type();
    121. cout << endl;
    122. ho.show();
    123. ho.type();
    124. cout << endl;
    125. of.show();
    126. of.type();
    127. cout << endl;
    128. //(4)测试部分程序
    129. cout << of;//直接输出office类成员
    130. cout << endl;
    131. //(5)测试部分程序
    132. of.document();
    133. return 0;
    134. }

    结果:

  • 相关阅读:
    K8s 集群高可用master节点ETCD挂掉如何恢复?
    ISO三体系认证需要什么材料,办理流程
    【2022】前端面试之vue汇总
    Redisson—分布式对象
    如何将一个字符串转换为驼峰命名法(camel case)?
    App自动化测试持续集成效率提高50%
    Redis中的Lua脚本(六)
    @Requestbody注解
    Word设置封面无页码,摘要罗马数字页码,正文数字页码
    可用于嵌入各种功能的STSPIN820、STSPIN830、STSPIN240、STSPIN233电机驱动器 功率 MOSFET
  • 原文地址:https://blog.csdn.net/weixin_45080800/article/details/139886160