增加一个成员函数,计算加一秒的时间

发布时间 2023-04-20 23:35:50作者: PeitongShi

设置Cmytime类。

具有三个成员函数

Show()

int Set(int h,int m,int s)

对于Set函数的要求,

     1、对于非法赋值不给予执行,三个参数合法范围是:0<=h<=23,  0<=m,s<=59。 如何参数非法,本次Set函数不改变原有值。

    2、赋值成功,返回1,否则返回0。

void  AddOneSecond();

实现在原时间的基础上加1秒的时间值。

输入  23  25 38

输出:  

23:25:38

0

23:25:38

23:25:39

#include<iostream>
using namespace std;
class Cmytime
{
private://内部属性 
    int hour,minute,second; 
public://对外接口 
    int Set(int h,int m,int s)
    {
        if(((h>=0)&&(h<=23))&&(m>=0)&&(s<=59))
        {
            hour=h;
            minute=m;
            second=s;
            return 1; 
        }
        else
        {
            return 0;
        }
    }
    void AddOneSecond()
    {
        second+=1;
        second%=60;
    }
    void Show()
    {
        cout << hour << ":" << minute << ":" << second;
    }
};
//StudybarCommentBegin
int main(void) {
    int h,m,s;
   cin>>h>>m>>s;

   Cmytime t1;
   t1.Set(h,m,s);
   t1.Show();
   cout<<endl<<t1.Set(24,0,0)<<"\n";
   t1.Show();

   t1.AddOneSecond();
   cout<<endl;
   t1.Show();
  
    return 0;
}

//StudybarCommentEnd

-END