请使用VC6或使用【答题】菜单打开考生文件夹proj1下的工程pmj1。其中有线段类Line的定义。程序中位于每个“//ERROR****found****”之后的一行语句有错误,请加以改正。改正后程序的输出结果应该是: Endpoint1:(1,8),E

admin2021-02-22  12

问题 请使用VC6或使用【答题】菜单打开考生文件夹proj1下的工程pmj1。其中有线段类Line的定义。程序中位于每个“//ERROR****found****”之后的一行语句有错误,请加以改正。改正后程序的输出结果应该是:
Endpoint1:(1,8),Endpoint2=(5,2),length=7.2111。
注意:只修改每个“//ERROR****found****”下的那一行,不要改动程序中的其他内容。
#include
#include
usingnamespacestd;
classLine;
doublelength(Line);
classLine{//线段类
doublex1,y1;//线段端点1
doublex2,y2;//线段端点2
public:
//ERROR**********found**********
Line(doublex1,doubley1,double
x2,doubley2)const{
this->x1=x1;
this->y1=y1;
this->x2=x2;
this->y2=y2;
}
doublegetX1()const{returnx1;}
doublegetY1()const{returny1;}
doublegetX2()const{returnx2;}
doublegetY2()const{returny2;}
voidshow()const{
cout《"Endpoint1=("<cout<<"),Endpoint2=("<//ERROR**********found**********
cout<<"),length="<  }
};
doublelength(Line1){
//ERROR**********found**********
returnsqrt((1.x1-1.x2)*(1.x1-1.x2)+(1.y1-1.y2)*(1.y1-1.y2));
}
intmain(){
Liner1(1.0,8.0,5.0,2.0);
r1.show();
return0;
}

选项

答案(1)Line(doublex1,doubley1,doublex2,doubley2){ (2)cout<<"),length="<
解析 (1)主要考查考生对构造函数的掌握,构造函数要给私有成员赋初始值,因此不能使用const来限制。
(2)主要考查考生对this指针的掌握,由函数length的声明doublelength(Line);可知,length函数的形参是Line类,在voidshow()const函数里,this指针指向的是当前:Line类,因此可以用*this表示当前Line类。
(3)主要考查考生对成员函数的掌握,length函数是类外函数,不能直接调用类的私有成员,因此要通过成员函数取得对应的值。
转载请注明原文地址:https://jikaoti.com/ti/nTh0FFFM
0

最新回复(0)