问答题 请使用VC6或使用[答题]菜单打开考生文件夹proj1下的工程proj1。程序中位于每个“//ERROR ****found****”下一行的语句有错误,请加以改正。改正后程序的输出结果应为: Thp value is 5 The value is 10 There are 2 objects. There are 1 objects. 注意:只修改每个“//ERROR ****found****”下的那一行,不要改动程序中的其他内容。 #include <iostream> using namespace std; class MyClass { public: MyClass(int value) { // ERROR ********** found********** this.value = value; count ++; } // ERROR ********** found********** void ~MyClass() { count--; } static int getCount () { return count; } int getValue() { return value; } private: int value; static int count; }; // ERROR ********** found********** static int MyClass::count = 0; int main() { MyClass* p = new MyClass(5); MyClass* q = newMyClass(10); cout << "The value is" << p ->getValue() << endl; cout << "The value is" << q ->getValue() << endl; cout << "There are" << MyClass::getCount() << " objects." << endl; delete p; cout << "There are" <, MyClass::getCount() << "objects." << endl; return 0; }
【正确答案】(1)this->value=value; (2)~MyClass() (3)int MyClass::count=0;
【答案解析】[考点] 本题考查MyClass类,其中涉及构造函数、析构函数和静态成员函数。 [解析] (1)主要考查考生对this指针的掌握,this是一个指针变量,调用成员时应使用标识符“->”。 (2)主要考查考生对析构函数的掌握,定义析构函数时不能使用任何返回类型。 (3)主要考查考生对静态成员的掌握,静态成员赋值时不用添加static,但声明时要使用。