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