问答题 使用VC6打开考生文件夹下的源程序文件modi3.cpp。类Person完成对人的姓名和年龄的绑定。函数fun()获得年龄比较大的姓名。然后输出这个姓名到屏幕。 其中定义的类并不完整,按要求完成下列操作, 将类的定义补充完整。 (1)完成类的构造函数,请在注释∥********1********后添加适当的语句。 (2)完成构造函数对姓名的赋值,请在注释∥********2********后添加适当的语句。 (3)定义类的友元函数fun(),请在注释∥********3********后添加适当的语句。 (4)补充函数fun()的年龄比较,请在注释∥********4********后添加适当的语句。 注意:增加代码,或者修改代码的位置已经用符号表示出来。请不要修改其他的程序代码。 #include C1as s Person { public: ∥********1******** { int i; for(i=0;sUserrName[i]!=0; i++1 { m UserName[i]=sUserrName[i]; } ∥********2******** mold=nold; } private: char m_UserName[32]; int m_old; ∥********3******** }; void fun(char*s,Person&personl, Person&person2) { ∥********4******** if() { for(int i=0;personl. m UserName[i] !=0;i++) { s[i] =personl.m UserName[i]; s[i+1]=0; } } else { for(int i=0;person2.m UserName[i]!=0;i++) { s[i] =person2.m UserName[i]; S[i+1]=0; } } } void main() { char S[32]; Person p1(“abc”,20); Person p2(“def”,3 0); fun(s,p1,p2); cout<
【正确答案】正确答案:(1) 添加语句 :Pe rson(char*suserrName,int n01d) (2)添加语句:m UserName[i]=0 ; (3)添加语句:friend void fun(char*s,Person&personl,Person&person2); (4)将“if()”补充完整为:if(personl.m—Old>person2.m—Old)
【答案解析】解析:在VC环境下打开程序,根据题干给出的几条功能要求,对程序中给出注释下的内容逐个补全或修改。从已定源程序的main主函数开始入手,函数fun是输出年龄较大的姓名,类Person记录了姓名和年龄。