首页
外语
计算机
考研
公务员
职业资格
财经
工程
司法
医学
专升本
自考
实用职业技能
登录
计算机
使用VC6打开考生文件夹下的源程序文件modi1.cpp,但该程序运行时有错,请改正main()函数中的错误,使程序的输出结果如下: Constructor. Default constructor Area is12 Area is0 Area is12
使用VC6打开考生文件夹下的源程序文件modi1.cpp,但该程序运行时有错,请改正main()函数中的错误,使程序的输出结果如下: Constructor. Default constructor Area is12 Area is0 Area is12
admin
2019-05-14
50
问题
使用VC6打开考生文件夹下的源程序文件modi1.cpp,但该程序运行时有错,请改正main()函数中的错误,使程序的输出结果如下:
Constructor.
Default constructor
Area is12
Area is0
Area is12
注意:错误的语句在//******error******的下面,修改该语句即可。
#include<iostream.h>
class CRectangle
{
private:
double length,width;
public:
CRectangle()
{
cout<<"Defaultconstructor.\n";
}
CRectangle(double1,double w)
{
length=1;width=w;
cout<<"Constructor.\n";
}
void Set(double1,double w)
{
this->length=1;
this->width=w;
}
void GetArea()
{
cout<<"Area is"<<length*width<<endl;
}
};
void main()
{
CRectangle Rectl(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(Rect1); (3)Rect2.Set(0,0);
解析
(1)构造函数CRectangle()不带有参数,CRectangle(double1,double w)带有2个参数,在创建对象时会自动调用构造函数,但是参数必须匹配,第1个标识下定义Rect2对象时,有一个参数,而类CRectangleoe()中并没有重载一个参数的构造函数,编译后出错,因此第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://kaotiyun.com/show/5E8p777K
本试题收录于:
二级C题库NCRE全国计算机二级分类
0
二级C
NCRE全国计算机二级
相关试题推荐
执行语句:{cout<<setfill(’*’)<<setw(10)<<seffill(’#’)<<left<<123<<"0K"<<end1;return0;}后将输出()。
内部格式控制操作函数是在头文件()中定义的。
在下面的4个关键字中用来说明虚函数的是()。
设有以下定义和程序:#include<iostream.h>classTestClass1{public:voidshow1(){cout<<"TestClass1"<<end1;}
在C++语言中函数返回值的类型是由()决定的
下面关于数据库三级模式结构的叙述中,正确的是()。
在数据库系统的组织结构中,下列()映射把用户数据库与概念数据库联系了起来。
有如下程序:#include<iostream>usingnamespacestd;classBase{public;intm,n;};classDerived1:______Base{};
随机试题
需求拉动的通货膨胀
咳嗽与咳痰疾病中,最常见的疾病是
下列哪种癌容易见到角化珠()
应严格控制现浇板上荷时间,待混凝土达到终凝条件时,即可上人放线、调运模板钢筋等材料。()
《建设工程安全生产管理条例》规定,出租的机械设备和施工工具及配件,在签订租赁协议时,应当出具()。
中国古典园林的特色有()。
基因是遗传的物质基础,它可以通过复制把遗传信息传递给下一代,但不能使遗传信息得到表达。()
Atwork,asinlife,attractivewomengetalotofgoodlucks.Studieshaveshownthattheyaremorelikelytobe【B1】______than
Parentsandstudentsarenowrelyinglessontakingoutloansandmoreongrants,scholarshipsandtheirownincomeandsavings,
Lookatthenotebelow.Youwillhearacallingabouttimeofarrival.Flight:【C5】______Getinat【C6】______Thefemalehadto【
最新回复
(
0
)