使用VC6打开考生文件夹下的源程序文件modi3.cpp。请完成以下部分,实现在屏幕上输出为: TestClass3 TestClass2 这个程序需要修改的部分,请按照以下部分实现。 (1)类TestClass0不能被实例

admin2018-10-23  28

问题 使用VC6打开考生文件夹下的源程序文件modi3.cpp。请完成以下部分,实现在屏幕上输出为:
    TestClass3
    TestClass2
    这个程序需要修改的部分,请按照以下部分实现。
    (1)类TestClass0不能被实例化,请定义一个纯虚函数print,在注释//********1********后添加适当的语句。
    (2)类TcstClass1私有虚继承类TestClass0,请在注释//********2********后添加适当的语句。
    (3)类TestClass2公有继承类TestClass0,请在注释//********3********后添加适当的语句。
    (4)类TestClass3公有继承类TestClass2与TestClass1,请在注释//********4********后添加适当的语句。
    注意:仅在函数指定位置添加语句,请勿改动主函数main与其他函数中的任何内容。
1  #include<iostream.h>
2  class TestClass0
4  {
4   //********1********
5
6  };
7   //********2********
8  class TestClass1:
9  {
10  public:
11  void print()
12  {
13  cout<<’’TestClass1’’<<end1;
14   }
15  };
16  //********3********
17  class TestClass2:
18  {
19  public:
20  void print()
21   {
22   cout<<’’TestClaSs2’’<<end1;
23   }
24  };
25  //********4********
26  class TeStClass3:
27  {
28  public:
29   void print()
30   {
31   cout<<’’TestClass3’’<<end1;
32  }
33  };
34  void main()
35  {
36   TestClass3 c3;
37   TestClass2 c2;
38  c3.print();
39  c2.print();
40   return;
41  }

选项

答案(1)添加语句:virtual void print()=0; (2)将“class TeStClass1:”补充完整为:class TestClass1:virtual private TestClass0 (3)将“class TestClass2:”补充完整为:class TestClass2:public TestClass0 (4)将“class TestClass3:”补充完整为:class TestClass3:public TestClass2,public TestClass1

解析 在VC环境下打开程序,根据题干给出的几条功能要求,对程序中给出注释下的内容逐个补全或修改。本题从题干要求入手,依次处理各个类,完成各个类的定义。
    (1)题目1要求“请定义一个纯虚函数print”。在C抖中,虚函数在基类中用virtual声明成员函数为虚函数。纯虚函数是在声明虚函数时被“初始化”为0的函数,即“virtual voidprint0=0;”。
    (2)题目2要求“类TestClass1私有虚继承类TestClass0”。在C++中声明一个派生类时将基类的继承方式指定为privme的,称为私有继承。同(1),声明TestClass1虚继承类“:virtual TestClass0”,题目要求为私有,即“class TestClass1:virtual private TestClass0”。
    (3)题目3要求“类TestClass2公有继承类TestClass0。”同(2),公有继承是在定义一个派生类时将基类的继承方式指定为public的,称为公用继承。所以这里补全“class TestClass2:public TestClass0”。
    (4)题目4要求“类TestClass3公有继承类TestClass2与TestClass1。”同(3),所以这里补全“class TestClass3:public TestClass2,public TestClass1”。
转载请注明原文地址:https://kaotiyun.com/show/85Ap777K
0

随机试题
最新回复(0)