使用VC6打开考生文件夹下的源程序文件modil.cpp,但该程序运行时有错,请改正main()函数中的错误,使程序的输出结果如下: Constructor. Default constructor. Area is 12

admin2017-11-27  12

问题 使用VC6打开考生文件夹下的源程序文件modil.cpp,但该程序运行时有错,请改正main()函数中的错误,使程序的输出结果如下:
    Constructor.
    Default constructor.
    Area is 12
    Area is 0
    Area is 12
    注意:错误的语句在//*****error******的下面,修改该语句即可。
#include
class  CRectangle
{
private:
double length,width;
public:
    CRectangle()
    {
    cout<<"Default
constructor.\n";
    }
    CRectangle(double 1,double w)
    {
    length=1;width=w;
    cout<<"Constructor  .\n";
    }
    void Set(double 1,double w)
    {
    this->length=1;
    this->width=w;
    }
    void GetArea()
    {
    cout<<"Area is"<width<    }
    }
    void main()
    {
    CRectangle Rect1(3.0,4.0);
    //*****error******
    CRectangle  Rect2(1);
    //*****error******
    Rect1.GetArea();
    //*****error******
    Rect2.Set(0);
    Rect2.GetArea();
    Rect3.GetArea();
    }

选项

答案(1)CRectangle Rect2; (2)CRectangle Rect3(Rect1); (3)Rect2.Set(0,0);

解析 (1)构造函数CRectangle()不带有参数,CRectangle(double1,double w)带有2个参数,在创建对象时会自动调用构造函数,但是参数必须匹配,第1个标识下定义Rect2对象时,有一个参数,而类CRectangle()并没有重载一个参数的构造函数,编译后出错,因此第1标识下应改为“CRectangle Rect2;”。
(2)Rect3.GetArea()输出的结果和Rect1.GetArea()输出结果一致,因此对象Rect3和对象Rect1两者具有相同的length和width值,除定义Rect3对象之外没有改变Rect31的length和width值,因此Rect3成员变量的初始化是通过拷贝构造函数类实现的,即用Rect1对象去初始化Rect3,因此第2个标识下应改为“CRectangle Rect3(Rect1);”。
(3)Rect2.GetArea()的输出结果为0,说明Rect2的成员变量length和width值至少一个为0,而Set()函数必须有两个参数,这里Rect2.Set(0)参数不匹配,应改为“Rect2.Set(0,0);”或者“Rect2.Set(0,5)”,两个参数当中至少一个为0即可。
转载请注明原文地址:https://jikaoti.com/ti/Eit0FFFM
0

随机试题
最新回复(0)