问答题
使用VC6打开考生文件夹下的源程序文件modi1.cpp,该程序运行时有错,请改正其中的错误,使程序正确运行,并且使程序输出的结果为:CMyObject,in the construtor~CMyObject,in the destrutor注意:错误的语句在//******error******的下面,修改该语句即可。#include<lOStream.n>class CMyObject{//******error******//******error******CMyObject{cout<<"CMyObject,in the construtor"<<endl;}~CMyObject(){cout<<"~CMyObject,in the destrutor"<<endl;}};void main(){CMyObjectobj1;}
【正确答案】正确答案:(1)添加语句:public: (2)CMyObject(){cout<<"CMyObject,in the construtor"<<endl;}
【答案解析】解析:(1)构造函数和析构函数均为公有函数,而在C++中默认的访问属性为私有(private),因此构造函数和析构函数必须说明为public,即第1个标识下应添加“public:”。 (2)函数定义中在函数名后均有一对括号表示函数声明,因此第2个标识下应改为“CMyObject(){cout<<"CMyObject,in the construtor"<<endl;)”。