问答题 请使用VC6或使用[答题]菜单打开考生文件夹proj1下的工程proj1,此工程中含有一个源程序文件proj1.cpp。其中位于每个注释“//ERROR****found****”之后的一行语句存在错误。请改正这些错误,使程序的输出结果为: Constructor called of 10 The value is 10 Destructor called of 10 注意:只修改注释“//ERROR****found****”的下一行语句,不要改动程序中的其他内容。 // proj1.cpp #include <iostream> using namespace std; class MyClass { public: MyClass(int i) { value = i; cout << "Constructor called of"<< value << endl; } // ERROR **********found********** void Print() { cout << "The value is" << value <<endl; } // ERROR **********found********** void ~MyClass() { cout << "Destructor called of" << value << endl; } private: // ERROR **********found********** int value = 0; }; int main() { const MyClass obj (10); obj.Print(); return 0; }
【正确答案】(1)void Print()const (2)~MyClass() (3)int value;
【答案解析】[考点] 本题考查MyClass类,其中涉及构造函数、const函数和析构函数。 [解析] (1)主要考查考生对const函数的掌握,主函数中obj的定义为:const MyClass obj(10);,要使obj能调用Print函数,必须使Print函数为const类型。 (2)析构函数不需要返回类型,应将void去掉。 (3)主要考查考生对私有成员的掌握,私有成员只能声明不能初始化。