问题 问答题

使用VC6打开考生文件夹下的工程MyProj12。此工程包含一个源程序文件MyMain12.cpp。程序中定义了两个类Base和Derived,但类的定义并不完整。 请按要求完成下列操作,将类的定义补充完成: ①类Derived是基类Base公有派生来的。请在注释“//* *1* *”之后添加适当的语句。 ②完成构造函数Derived(int x)定义,采用初始化列表的方式使基类Base私有成员a初始化为x,类Derived的私有成员b初始化为x+1。请在注释“//* *2* *”之后添加适当的语句。 ③完成类Derived的成员函数show()的类体外的定义。函数show()中要输出基类数据成员a的值,然后要输出私有成员b的值。请在注释“//* *3* *之后添加适当的语句。 注意;除在指定位置添加语句之外,请不要改动程序中的其他内容。 源程序文件MyMain12.cpp清单如下: //MyMain12.cpp #include<iostream> using namespace std; class Base { public: int a; Base(int i) {a=i; } }; //* * * 1 * * * { private: int b; public: //* * * 2 * * * void show(); }; void Derived::show() { //* * * 3 * * * } int main() { Derived d(1); d.show(); return 0; }

答案

参考答案:

解析:①class Derived:public Base ②Derived(int x):Base(x),b(x+1){} ③cout<<Base: :a;cout<<b<<end1; 程序中定义了两个类Base和Derived,类Derived是类Base的公有派生类。

单项选择题
选择题