填空题

使用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();

【参考答案】


(A)错误:缺少友元函数的声明
正确:添加友元函数的声明friend void setvalue(an......

(↓↓↓ 点击下方‘点击查看答案’看完整答案、解析 ↓↓↓)
热门 试题

问答题
使用VC6打开考生文件夹下的工程test19_3,此工程包含一个源程序文件test19_3.cpp,其中定义了用于表示向量的类 vector,但类vector的定义并不完整。请按要求完成下列操作,将类vector的定义补充完整。 (1)补充类vector的构造函数,该函数有参数x和y,它们都是int型的数据,默认值都为0。请使用参数列表的形式分别将类的数据成员a和b分别初始化为参数x和y的值。请在注释“ **1**”之后添加适当的语句。 (2)完成类vector的成员函数input(int x,int y)的定义,将int型的参数x和y分别赋值给数据成员a和b,请在注释“ **2**”之后添加适当的语句。 (3)完成类vector的友元函数friend double Multiply (vector &x,vector &y)的定义,先定义double型的临时变量c,然后将参数对象x和对象y的数据成员a与b分别相乘再相加后赋值给c,最后返回c的值,将请在注释“ **3**”之后添加适当的语句。输出结果如下: (10,20) (2, 3) 80 注意:除在指定位置添加语句之外,请不要改动程序中的其他内容。 源程序文件test19_3.cpp清单如下: #include <iostream.h> class vector int a; int b; public: **1** void input(int x, int y) **2** void output () cout<<’(’<<a<<’,’<<b<< ) <<end1; friend double Multiply(vector &x,vector &y); double Multiply (vector &x,vector &y) **3** c=x.a*y.a+x.b*y.b; return c; void main() vector x(10,20),y; double d; y.input (2,3); d=Multiply (x,y); x. output ( ); y. output ( ); cout<<d<<end1;