请打开考生文件夹下的解决方案文件proj1,该工程中包含程序文件main.cpp,其中有类Clock(“时钟”)的定义和主函数main的定义。程序中位于每个“//ERROR **** found ****”之后的一行语句有错误,请加以改正。改正后程序的输出

admin2020-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
0

最新回复(0)