问答题

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

【参考答案】

正确答案:(1)CRectangle Rect2; (2)CRectangle Rect3(Rectl); (3)Rec......

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

问答题
使用VC6打开考生文件夹下的源程序文件modi3.cpp。其中定义的类并不完整,按要求完成下列操作,将类的定义补充完整。完成以下功能: 每卖出一个瓜,则计算瓜的重量,还要计算所有卖出瓜的总重量以及总个数,同时允许退货,请按照以下的操作,把类补充完整 (1)定义类Cmelon的私有静态数据成员float型变量totalweight和int型变量totalNo,请在注释//********1********后添加适当的语句。 (2)完成类Cmelon的带一个float型变量w的构造函数,并把这个w加到totalweight中,并且totalNo自加。请在注释//********2********后添加适当的语句。 (3)在析构函数中,在totalweight中减去weight,然后totalNo自减,请在注释//********3********后添加适当的语句。 (4)完成静态成员变量的初始化为0,请在注释//********4********后添加适当的语句。 注意:增加代码,或者修改代码的位置已经用符号表示出来。请不要修改其他的程序代码。#include<iostream.h>Class Cmelon{private: float weight; //********1******** static int totalNo;public: Cmelon(fleat w) { //********2******** totalweight+=w; totalNo++; } ~Cmelon() { //********3******** totalweight-=weight; } void display() { cout<< Sell a melonwith <<weight<<”kg”<<endl; cout << Total Sellnumber: <<totalNo<<endl; cout << Total sellweight: <<totalweight<< kg <<endl<<endl; }};//********4********float Cmelon::totalweight=0.0;int main(){ Cmelon melon1(1.2); melon1.display(); Cmelon melon2(2.3); melon2.display(); return 0;}