2-2 Time类的定义

来源:互联网 发布:sql select 常量表 编辑:程序博客网 时间:2024/04/24 19:10

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;}

0 0
原创粉丝点击