请打开考生文件夹下的解决方案文件proj1,程序中位于每个“//ERROR****found****”之后的一行语句有错误,请加以改正。改正后程序的输出结果应为: value=63 number=1 注意:只修改每个“//ERROR****found***

admin2018-07-06  19

问题 请打开考生文件夹下的解决方案文件proj1,程序中位于每个“//ERROR****found****”之后的一行语句有错误,请加以改正。改正后程序的输出结果应为:
value=63
number=1
注意:只修改每个“//ERROR****found****”下的那一行,不要改动程序中的其他内容。
#include<iostream>
using namespace std;
class MyClass{
int*p;
const int N;
public:
//ERROR*******found*******
MyClass(int val):N=1
{
p=new int;
*p=val;
}
//ERROR*******found*******
~MyClasS( ){delete*p;}
friend void print(MyClass&obj);
};
//ERROR*******found*******
void MyClass::print(MyClass&obj)
{
cout<<"value="<<*(obj.p)<<endl;
cout<<"number="<<obj.N<<endl;
}
int msin( )
{
MyClass obj(63);
print(obj);
return0;
}

选项

答案(1)MyClass(int val):N(1) (2)~MyClass( ){delete[ ]p;} (3)void print(MyClass&obj)

解析 (1)主要考查考生对构造函数的掌握,在这里不能使用赋值语句。
(2)主要考查考生对析构函数的掌握,析构函数的delete语句要使用标识符“[ ]”,即delete[ ]p;。
(3)主要考查考生对友元函数的掌握,友元函数并不属于类,因此定义时前面不用加类名和作用域符号。
转载请注明原文地址:https://kaotiyun.com/show/jAAp777K
0

随机试题
最新回复(0)