首页
外语
计算机
考研
公务员
职业资格
财经
工程
司法
医学
专升本
自考
实用职业技能
登录
计算机
请打开考生文件夹下的解决方案文件proj1,该工程中包含程序文件main.cpp,其中有类Clock(“时钟”)的定义和主函数main的定义。程序中位于每个“//ERROR **** found ****”之后的一行语句有错误,请加以改正。改正后程序的输出
请打开考生文件夹下的解决方案文件proj1,该工程中包含程序文件main.cpp,其中有类Clock(“时钟”)的定义和主函数main的定义。程序中位于每个“//ERROR **** found ****”之后的一行语句有错误,请加以改正。改正后程序的输出
admin
2020-04-07
21
问题
请打开考生文件夹下的解决方案文件proj1,该工程中包含程序文件main.cpp,其中有类Clock(“时钟”)的定义和主函数main的定义。程序中位于每个“//ERROR **** found ****”之后的一行语句有错误,请加以改正。改正后程序的输出结果应为:
Initial times are
0 d:0 h:0 m:59 s
After one second times are
0 d:0 h:1 m:0 s
注意:只修改每个“//ERROR **** found ****木”下的那一行,不要改动程序中的其他内容。
#include<iostream>
using namespace std;
c1ass CloCk
{
public:
Clock(unsigned long i=0);
void set(unsigned long i=0);
void print()const;
void tick(); //时间前进一秒
Clock operator++();
private:
unsigned long total_sec,seconds,minutes,hours,days;
};
Clock::Clock(unsigned long i)
:total_sec(i),seconds(i%60),minutes((i/60)%60),hours((i/3600)%24),days(i/8 6400){}
void Clock::set(unsigned long i)
{
total_sec=i;
seconds=i%60;
minutes=(i/60)%60;
hours:(i/3600)%60;
days=i/86400;
}
//ERROR ******found******
void Clock::print()
{
cout <<days << "d: " <<hours<<"h:"
<<mLnutes << "m : " <<seconds<<"s"<<endl;
}
void Clock::tick()
{
//ERROR *******found*******
set(total sec++);
}
Clock Clock::operator++()
{
tick();
//ERROR *******found*******
return this;
}
int main()
{
Clock ck(59);
cout<< "Initial times are"<<endl;
ck.print();
++ck;
cout << "After one second times are"<<endl;
ck.print();
return 0;
}
选项
答案
(1)void Clock::print()cons! (2)set(++total_sec); (3)return *this;
解析
(1)主要考查考生对成员函数的掌握,由Clock类中对函数print的声明void print()const;可知,在定义print函数时少了const。
(2)主要考查考生对++操作的掌握,根据函数要求,时间要先前进一秒,再调用函数set,因此total_sec++应改为++total_sec。
(3)主要考查考生对this指针的掌握,函数要求返回值Clock,即返回一个类,而不是指针,因此使用*this。
转载请注明原文地址:https://jikaoti.com/ti/lPA0FFFM
本试题收录于:
二级C题库NCRE全国计算机二级分类
0
二级C
NCRE全国计算机二级
相关试题推荐
结构化程序设计的3种结构是
假定A为一个类,则语句A(A&a);为该类______函数的原型说明。
下列关于getline()函数的描述中,不正确的是()。
程序设计语言的工程特性之一为()。
有如下类声明:classXA{intx;public:XA(intU)}x=n;}};classXB:publicXA{inty;public:XB(in
从工程管理的角度来看,软件设计分两步来完成。首先是概要设计,然后是【】。
在面向对象的程序设计中,用来请求对象执行某一处理或回答某些信息的要求称为【】。
以下C++语言中用于单行注释的是
下列描述中,错误的是()。A)公有继承时基类中的公有成员在派生类中仍是公有成员B)公有继承时基类中的保护成员在派生类中仍是保护成员C)保护继承时基类中的公有成员在派生类中仍是公有成员D)保护继承时基类中的保护成员在派生类中仍是保护成
随机试题
允许外国公使进驻北京的条约是()
流行性出血热病毒是
油性软膏基质中加入羊毛脂的作用是( )。
某男甲与某女乙2000年登记结婚,婚后两人的感情非常冷淡,无法一起共同生活。2004年1月,乙以感情破裂为由向法院请求离婚,法院支持其主张的理由可以包括:
十进制数140转换为十六进制数为()。
按投资作用不同,工程建设项目可分为生产性建设项目和非生产性建设项目两大类。下列项目中属于非生产性建设项目的是( )。
据某市交警部门公布的数据,在该市全市上个月发生的13000多起汽车交通事故中,只有10%是因为司机酒后驾驶造成的。由此可见,酒后驾驶的危险性并不像某些交通宣传中所说的那么大。“酒后驾驶,等于送死”只是危言耸听的夸张宣传而已。假定以上数据无误,那么
2008年以来,索马里附近海域先后发生累计逾120起的海盗劫船事件。海盗为何如此猖獗?甲乙丙丁四人有如下断定:甲:海盗猖獗的主因是被劫船只的船主通常都愿支付高额赎金。乙:如果海盗猖獗的主因是被劫船主愿支付高额赎金,那么肯定会助长海盗的气
Theearthiswitnessinganurbanrevolution,aspeopleworldwidecrowdintotownsandcities.In1800onlyfivepercentofthew
QueenMary’sReignI.HistoriceventsA.KingHenryVIIIplannedMary,his【T1】_______,tomarryhissonEdward.B.Hermothe
最新回复
(
0
)