使用VC6打开考生文件夹下的源程序文件modi3.cpp,其中定义了用于表示矩形的CRect类,但类CRect的定义并不完整。请按要求完成下列操作,将类CRect的定义补充完成。 (1)定义私有数据成员leftPoint、topPoint、rightP

admin2015-07-20  34

问题 使用VC6打开考生文件夹下的源程序文件modi3.cpp,其中定义了用于表示矩形的CRect类,但类CRect的定义并不完整。请按要求完成下列操作,将类CRect的定义补充完成。
  (1)定义私有数据成员leftPoint、topPoint、rightPoint、bottomPoint,分别用于表示矩形左上角及右下角的点的坐标,它们都是double型的数据。请在注释∥********1********之后添加适当的语句。
(2)完成默认构造函数CRect的定义,指定缺省实参为0,都是double型的数据。请在注释∥********2********之后添加适当的语句。
(3)定义函数体为空的析构函数。请在注释∥********3********之后添加适当的语句。
(4)在main()函数中定义CRect类的实例reet2,并把rectl的值赋给rect2。请在注释∥********4********之后添加适当的语句。
注意:除在指定位置添加语句之外,请不要改动程序中的其他内容。#include
Class CRect
{
private:
    ∥********1********
public:
    ∥********2********
    ∥********3********
    void SetPoints(double,
double,double,double);
    void SetLeftPoint(double m)
(1eftPoint=m;)
    void SetRightPoint(double m)
{rightPoint=m;)
    void SetToppoint(double m)
{topPoint=m;)
    void SetBottomPoint
(double m){bottomPoint=m;)
    void Display();
);
CRect::CRect(double 1,double
t,double r,double b)
{
    leftPoint=1;topPoint=t ;
    rightPoint=r;bottomPoint
=b ;
}
void CRect::SetPointS(double
1,double t,double r,double b)
{
    leftPoint=1 ; topPoint=t;
    rightPoint=r;bottompoint
=b ;
}
void CRect::Di splay()
{
    cout(<”left—top point iS
(”<”)”<<‘\n’;
    cout<<“right-bottom point it
(“<nt<<“)”<<‘\n’,
}
void main(){
    CRect rect0 ;
    rect0.Display();
    rect0.SetPoints(20,2 0.6,3 0,
4 0);
    rect 0.Display();
    CRect rectl(0,0,15 0,15 0);
    rectl.SetToppoint(10.5);
    rectl.SetLeftPoint(10.5);
    ∥********4********
    rect2.Display();
}

选项

答案(1)添加语句:double leftPoint,topPoint,rightPoint,bottomPoint; (2)添加语句:CRect(double leftPoint=0,double topPoint=0,double rightPoint=0,double bottomPoint=0); (3)添加语句:~CRect(){} (4)添加语句:CRect rect2(rectl)

解析 (1)第1个标识下完成私有数据成员leflPoint、topPoint、rightPoint、bottomPoint的定义,均为double型的变量,故第1个标识下应添加“double leflPoint,topPoint,rightPoint,bottomPoint;”。
(2)构造函数完成成员变量的初始化,类CRect的默认构造函数并初始化double型私有数据成员leflPoint、topPoint、rightPoint、bottomPoint为0,故第2个标识下应添加“CRect(double leftPoint=0,double topPoint=0,doublerightPoint=0,double bottomPoint=0);”。
(3)析构函数名和类名一致,并在前面加“~”以和构造函数区别,该析构函数体为空,故第3个标识下应添加“~CRect0{};”,虽然该函数体为空,但是“{}”必须保留。
(4)主函数中类CRect的对象rect2是通过复制构造函数将rectl的值赋值给它实现初始化的,而rect1的初始化直接调用自定义构造函数,第4个标识下应添加“CRectrect2(reet 1);”。
转载请注明原文地址:https://jikaoti.com/ti/DDE0FFFM
0

最新回复(0)