请使用VC6或使用【答题】菜单打开考生文件夹pmjl下的工程pmjl。此工程定义了StopWatch(秒表)类,用于表示时、分、秒信息,有构造函数StopWatch()、设置时间函数reset(),并且重载了前置和后置++运算符,实现增加1秒的功能。 程序

admin2015-06-27  29

问题 请使用VC6或使用【答题】菜单打开考生文件夹pmjl下的工程pmjl。此工程定义了StopWatch(秒表)类,用于表示时、分、秒信息,有构造函数StopWatch()、设置时间函数reset(),并且重载了前置和后置++运算符,实现增加1秒的功能。
程序中位于每个“//ERROR****found****”之后的一行语句有错误,请加以改正。改正后程序的输出结果应该是:
00:00:00
00:01:00
注意:只修改每个“//ERROR****found****”下的那一行,不要改动程序中的其他内容。
#include
#include
usingnamespacestd;
classStopWatch//"秒表"类
{
inthours;//小时
intminutes;//分钟
intseconds;//秒
public:
StopWatch():hours(0),minutes(0),
seconds(0){}
voidreset()fhours=minutes=seconds=0;}
StopWatchoperator++(int)//后置++
{
StopWatchold=*this;
++(*this);
returnold;
}
//前进一秒
StopWatch&operator++()//前置++
{
//ERROR*********found*********
if(seconds++=60)
{
seconds=0;minutes++;
if(minutes=60)
{
minutes=0;
hours++;
  }
}
//ERROR*********found*********
returnthis;
}
friendvoidshow(StopWatch);
};
voidshow(StopWatchwatch)
{
cout<cout<<<end1;
}
intmain()
{
StopWatchsw;
show(SW),
for(inti=0;i<59;i++)SW++;
//ERROR*********found*********
show(sw++);
return0;
}

选项

答案(1)if ((++seconds) == 60) (2)return *this; (3)show(++sw);

解析 (1)主要考查考生对’++’运算符的掌握,结合程序可知,应先使seconds加1,再判断是否需要进位,因此为++second。
(2)主要考查考生对this指针的掌握,应返回this指针指向的类。
(3)主要考查考生对’++’运算符的掌握,判断sw是要先取值再自加1还是先自加1再取值。
转载请注明原文地址:https://kaotiyun.com/show/RxNp777K
0

最新回复(0)