简答题 C++
题号 #2254749

使用VC++6.0打开考生文件夹下的源程序文件1.cpp,该程序运行时有错,请改正程序中的错误,使程序输出的结果为
150
200
注意:错误的语句在//******error******的下面,修改该语句即可。
提示:定义Numl为一个整型数据位长的变量。
试题程序:
#include<iostream.h>
//******error******
struct
{
//******error******
intNuml:1;
intNum2;
}
MyStruct;
voidmain()
{
MyStructmstr;
mstr.Num1=150;
mstr.Num2=15;
//******error******
int*ptr=&mstr.Numl;
cout<<*ptr<<endl;
*ptr=200;
cout<<*ptr<<endl;
}

进入在线模考

参考答案

暂无

答案解析

(1)“typedefstruct”。
(2)“intNuml:32;”或“intNuml;”。
(3)“int*ptr=(int*)&mstr;”。
【解析】第1处是定义结构体,结构体定义的一般形式为
struct(结构体名>
{
<成员列表>
}
(变量名列表);
程序当中定义结构体名放在后面,MyStruct为结构体变量类型,相当于typedef为已有的类型定义一个新的类型,第1处应改为“typedefstruct”。第2处是定义Numl为一个整型数据变量,整型数据位数为32(4字节),因此第2处应改成“intNuml:32;”或“intNuml;”。ptr为整型数据指针,mstr为MyStruct类型结构变量,两者类型不一致应该做强制类型转换,第3处应改成“int*ptr=(int*)&mstr;”,表示将mstr的地址给ptr,ptr指向MyStruct的第一个域Numl,Numl也是整型,类型一致,故可通过ptr指针来改变Numl的值。

你可能感兴趣的试题

2 使用VC++6.0打开考生文件夹下的源程序文件3.cpp,其中定义了用于表示个人基本信息的类PInfo,但类PInfo的定义并不完整。请按要求完成下列操作,将类Pin-fo的定义补充完成:
(1)定义私有数据成员bloodType用于表示血型,血型为char型的数据。请在注释1之后添加适当的语句。
(2)完成构造函数的定义,要求具有默认值,默认值为
身高175,体重70,血型A。请在注释2之后添加适当的语句。
(3)完成类PInfo外成员函数SetInfo的定义。请在注
释3之后添加适当的语句。
(4)在主函数main中调用成员函数Setlnfo,把对象d2的3个私有数据成员分别设定为身高170,体重64,血型B。
请在注释4之后添加适当的语句。
注意:除在指定位置添加语句之外,请不要改动程序中的其他内容。
试题程序:
#include<iostream.h>
classPlnfo
{
private:
intheight;
intweight;
//********1********
public:
//********2********
:height(ht),weight(wt),bloodType(bt){};
Plnfo(Plnfo&h1):height(h1.height),
weight(h1.weight),bloodType(h1.bloodType){}
intGetHeight()
{
returnheight;
}
intGetWeight()
{
returnweight;
}
intGetBloodType()
{
returnbloodType;
}
voidSetlnfo(intht,intwt,charbt);
voidDisplay();
};
//********3********
{
height=ht;
weight=wt;
bloodType=bt;
}
voidPlnfo::Display()
{
cout<<"Humanlnf0:";
cout<<height<<"cm,"<<weight<<"Kg,Blood-
Type"<<bloodType<<endl;
}
voidmain()
{
PInfoh1(169,61,'A');
Plnfoh2;
Plnfoh3(h1);
Plnfoh4(h2);
//********4********
h1.Display();
h2.Display();
h3.Display():
h4.Display();
}
找试题 · 找答案 · 找资料 在线模考