使用VC6打开考生文件夹下的工程test19_1,此工程包含一个源程序文件test19_1.cpp,但该程序运行有问题,请改正程序中的错误,使程序的输出结果如下: 1: weight:5 age:0 2: weight:7 age:9 源程序文件test19_1.cpp 清单如下: #include <iostream.h> class animal { public: /**************** found *******************/ friend void setvalue(animal&,int); /**************** found *******************/ void print() protected: int itsweight; int itsage; }; void animal::print() { cout<<"weight:"<<itsweight<<end1; cout<<"age:"<<itsage<<end1; } void setvalue(animal &ta,int tw) { ta.itsweight=tw; ta.ihsage=0; } void setvalue(animal &ta,int tw, int tn) { ta.itsweight=tw; ta.itsage=tn; } void main() { /**************** found *******************/ animal peppy setvalue(peppy,5); cout<<"1:"<<end1; peppy.print(); setvalue(peppy,7,9); cout<<"2:"<<end1; peppy.print(); }
参考答案:
解析:(1)错误:缺少友元函数的声明 正确:添加友元函数的声明friend void setvalue(animal &,int,int);(2)错误:viod print(); 正确:void print();(3)错误:animal peppy 正确:animal peppy;(1)主要考查考生对于成员函数定义规则的掌握,成员函数必须先声明再使用,即使是友元函数也不例外;(2)主要考查考生对于关键字的掌握,空类型的关键字应用"void";(3)主要考查考生对于变量定义的掌握,该处缺少“;”。