使用VC++6.0打开考生文件夹下的源程序文件3.cpp,其中定义了用于表示矩形的CRect类,但类CRect的定义并不完整。请按要求完成下列操作,将类CRect的定义补充完整。 (1)定义私有数据成员leftPoint、topPoint、rightPoint、bottomPoint,分别用于表示矩形左上角及右下角的点的坐标,它们都是double型的数据。请在注释1之后添加适当的语句。 (2)完成默认构造函数CRect的定义,指定默认实参为0,它们都是double型的数据。请在注释2之后添加适当的语句。 (3)定义函数体为空的析构函数。请在注释3之后添加适当的语句。 (4)在main函数中定义GReet类的实例rect2,并把re- ct1的值赋给rect2。请在注释4之后添加适当的语句。 注意:除在指定位置添加语句之外,不要改动程序中的其他内容。 试题程序: #include classCRect { private: //********1******** public: //********2******** //********3******** voidSetPoints(double,double,double,double); voidSetLeftPoint(doublem){leftPoint=m;} voidSetRightPoint(doublem){rightPoint=m;} voidSetTopPoint(doublem){topPoint=m;} voidSetBottomPoint(doublem){bottomPoint=m;} voidDisplay(); }; CReet::CRect(double1,doublet,doubler,doubleb) { leftPoint=1;topPoint=t; rightPoint=r;bottomPoint=b; } voidCRect::Setpoints(double1,doublet,doubler,doubleb) { leftPoint=1;topPoint=t; rightPoint=n;bottomPoint=b; } voidCRect::Display() { cout<<"left-toppointis("< Point<<")"<<’\n’; cout<<"right-bottompointis("< < } voidmain() { CRectrect0; rect0.Display(); rect0.SetPoints(20,20.6,30,40); rect0.Display(); CRectrectl(0,0,150,150); rect1.SetTopPoint(10.5); rect1.SetLeftPoint(10.5); //********4******** rect2.Display( );}
参考答案:(1)应添加“doubleleftPoint,topPoint,rightPoint,hot-tomPoint;”。 (2)应添加“CReet(doubleleftPoint=0,doubletopPoint=0,doublerightPoint=0,doublebottomPoint=O);”。 (3)应添加“~CReet(){}”。 (4)应添加“CReetrect2(rect1);”。
解析:【解析】本题在第1处完成私有数据成员leftPoint、top-Point、rightPoint、bottomPoint的定义,均为double型的变量,故第1处应添加“doubleleftPoint,topPoint,rightPoint,bottomPoint;”。构造函数完成成员变量的初始化,类CRect的默认构造函数初始化double型私有数据成员leftPoint、topPoint、rightPoint、bottomPoint为0。故第2处应添加“CRect(doubleleftPoint=0,doubletopPoint=0,doublerightPoint=0,doublebottomPoint=0);”。析构函数名和类名一致,并在前面加“~”以和构造函数区别,该析构幽数体为空,故第3处应添加“~CRect(){}”,虽然该函数体为空,但“{}”必须保留。主函数中类CRect的对象rect2是通过复制构造函数将rect1的值赋值给它实现初始化的.而reet1的初始化直接调用自定义构造函数,第4处应添加“CReetrect2(rect1);”。