首页
外语
计算机
考研
公务员
职业资格
财经
工程
司法
医学
专升本
自考
实用职业技能
登录
计算机
使用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
62
问题
使用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://www.kaotiyun.com/show/5E8p777K
本试题收录于:
二级C题库NCRE全国计算机二级分类
0
二级C
NCRE全国计算机二级
相关试题推荐
非成员函数应该声明为类______函数才能访问该类的私有成员。
在下面的类定义中,横线处应填入的内容是()。classFred{public:voidprint(){cout<<data<<end1;}voidsetData(doubled}{data
关于函数重载,下列叙述中错误的是()
有如下程序:#include<iostream>usingnamespacestd;classAA{intn;public:AA(intk):n(k){}intget(){retur
在下面的4个关键字中用来说明虚函数的是()。
下面程序的运行结果是()。#include<iostream.h>intx=5;intfan(inta){intc;c=x*a;returnc;}voidmain(
有如下程序:#include<iostream>usingnamespacestd;voidfun(int&x,inty){intt=x;x=y;y=t;}intmain(){inta[2]={
下面关于数据库三级模式结构的叙述中,正确的是()。
程序中有如下语句:for(inti=0;i<5;i++)cout<<*(p+i)<<",";能够依次输出int型一维数组DATA的前5个元素。由此可知,变量p的定义及初始化语句是______。
随机试题
骺板软骨()
简析小说《拜堂》的艺术特点。
LastnightIdidnot:getmyhomework______untilmidnight.
正常人体液中各部分渗透压的关系是
为保证稳定土层表面不受破坏,严禁压路机在已完成的或正在碾压的地段上()。
红葡萄酒,1.25L瓶装
【2016年济南市济阳县】原始社会不存在专门的教育机构。()
××学校一个二级学院要召开全院教师大会布置学期工作,所用的公文文种是()。
Oh!Ihavejusthadsuchalovelydream!andthenIwoke,andallthedreamwentoutlikekettle-steamorchimney-smoke.Mydrea
A、Harmtosingersdonebysmokyatmospheres.B、Sideeffectsofsomecommondrugs.C、Voiceproblemsamongpopsingers.D、Hardship
最新回复
(
0
)