[2669]2-2 Time类的定义



2-2 Time类的定义

Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^

题目描述

通过本题目的练习可以掌握类与对象的定义;

设计一个时间类Time,私有数据成员有hour()minute()second()

公有成员函数有:setHour(int)设置数据成员hour的值,非法的输入默认为12setMinue(int)设置数据成员minute的值,非法输入默认为0setSecond(int)设置数据成员second的值,非法输入默认为0setTime(intintint)设置时、分、秒三个数据成员的值; showTime()显示时间对象的值。

在主函数main()中调用相应成员函数,使得时间对象的值能从键盘接收,并正确显示。

输入

 

输入3个整数,用一个空格间隔

输出

 

输出 时、分、秒的值,中间用“:”间隔

示例输入

10 11 12

示例输出

10:11:12

提示

输入

58 23 85

输出

12:23:00

#include <iostream>
using namespace std;
class time
{
private:
    int h,m,s;
public:
    void settime()
    {
        cin>>h>>m>>s;
    }
    void seth()
    {
        if(h<0||h>12)
            h=12;
    }
    void setm()
    {
        if(m<0||m>60)
            m=0;
    }
    void sets()
    {
        if(s<0||s>60)
            s=0;
    }
    void showtime()
    {
        if(h<10)
            cout<<"0"<<h<<":";
        else
            cout<<h<<":";
        if(m<10)
            cout<<"0"<<m<<":";
        else
            cout<<m<<":";
        if(s<10)
            cout<<"0"<<s<<endl;
        else
            cout<<s<<endl;
    }
};
int main()
{
    class time t;
    t.settime();
    t.seth();
    t.setm();
    t.sets();
    t.showtime();
    return 0;
}

posted @ 2014-09-12 11:32  jiangyy  阅读(195)  评论(0编辑  收藏  举报