在线等,挺急的!!用C++设计一个类Time(时间),具有成员私有变量hour,minute,second 10

1.设计一个类Time(时间),具有成员私有变量hour、minute、second。2.设计构造函数,可以给Time类赋初始值。3.设计一个printTime函数,实现... 1. 设计一个类Time(时间),具有成员私有变量hour、minute、second。
2. 设计构造函数,可以给Time类赋初始值。
3. 设计一个printTime函数,实现Time的打印输出。
4. 设计公共接口函数,使得外部程序只能通过接口函数进行访问私有成员变量。
5. 实现秒加1操作。
6. 对++和--操作符进行重载,实现类Time对象的秒加1和减1操作。
7. 设计一个main函数进行上述功能的测试。
展开
 我来答
isaaccwoo
2015-11-17 · TA获得超过152个赞
知道小有建树答主
回答量:76
采纳率:0%
帮助的人:103万
展开全部
#include <cstdio>
using namespace std;

class Time
{
private:
    int hour, minute, second;   //1
public:
    Time(int h, int m, int s) :hour(h), minute(m), second(s) {} //2
    void printTime() { printf("%02d:%02d:%02d\n", hour, minute, second); } //3
    int getHour()   const { return hour;   }
    int getMinute() const { return minute; }
    int getSecond() const { return second; }
    void setHour(  const int &h) { hour   = h;  }
    void setMinute(const int &m) { minute = m;  }
    void setSecond(const int &s) { second = s;  }
    void increase() // 5
    {
        second ++;
        if (second == 60) { second = 0; minute ++; }
        if (minute == 60) { minute = 0; hour ++;   }
        if (hour == 24)   { hour = 0;              }
    }
    void decrease()
    {
        second --;
        if (second == -1) { second = 59; minute --; }
        if (minute == -1) { minute = 59; hour --;   }
        if (hour == -1)   { hour = 23;              }
    }
    Time &operator ++()   { increase(); return *this;  }
    Time operator ++(int) { increase(); return *this;  }
    Time &operator --()   { decrease(); return *this;  }
    Time operator --(int) { decrease(); return *this;  }
};

int main()
{
    // 赋初值,输出
    Time myTime(12, 34, 56);
    myTime.printTime();
    // 公共接口
    printf("hour=%d, minute=%d, second=%d\n", myTime.getHour(),
        myTime.getMinute(), myTime.getSecond());
    // +1
    myTime.increase(); 
    myTime.printTime();
    // ++ --
    myTime.setHour(23);
    myTime.setMinute(59);
    myTime.setSecond(59);
    myTime.printTime();
    myTime ++;
    myTime.printTime();
    myTime --;
    myTime.printTime();
    ++myTime; 
    myTime.printTime();
    --myTime; 
    myTime.printTime();
    return 0;
}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式