Home > Back-end >  Save the children!
Save the children!

Time:11-02

Define a clock class attributes include, minutes and seconds, operations include setting time, access time, add the default constructor, and the constructor and copy constructor; Test the above function respectively,

CodePudding user response:

 
//code, complete set and read, if the 24 60 minutes 60 seconds, when they completed whether value effectively,
The class cTime
{
Public:
CTime ()
{
M_Hour=0;
M_Minu=0;
M_Seco=0;
}
~ cTime () {}
CTime (int h, int m, int s)
{
M_Hour=h;
M_Minu=m;
M_Seco=s;
}
CTime (const cTime& T)
{
M_Hour=t.m _Hour;
M_Minu=t.m _Minu;
M_Seco=t.m _Seco;
}

Private:
Int m_Hour;
Int m_Minu;
Int m_Seco;
};

CodePudding user response:

I this simplified version, some more general, you can refer to:

Const int HOUR_MINUTES=60;//how many minutes an hour
Const int HOUR_SECONDS=3600;//an hour how many seconds
Const int MINUTE_SECONDS=60;//how many seconds in a minute

The class CTime
{
Private:
Int m_time;//time, unit: second

Public:
CTime () : m_time ()
{
}

CTime (int and int minute, int second)
{
SetTime (hour, minute, second);
}

Void SetTime (int and int minute, int second)
{
M_time=HOUR_SECONDS + minute hour * * MINUTE_SECONDS + second;
}

Int Hour ()
{
Return m_time/HOUR_SECONDS;
}

Int Minute ()
{
Return (m_time/MINUTE_SECONDS) % HOUR_MINUTES;
}

Int Seconds ()
{
Return m_time % MINUTE_SECONDS;
}

};

CodePudding user response:

 const int HOUR_MINUTES=60;//how many minutes an hour 
Const int HOUR_SECONDS=3600;//an hour how many seconds
Const int MINUTE_SECONDS=60;//how many seconds in a minute

The class CTime
{
Private:
Int m_time;

Public:
CTime () : m_time ()
{
}

CTime (int and int minute, int second)
{
SetTime (hour, minute, second);
}

Void SetTime (int and int minute, int second)
{
M_time=HOUR_SECONDS + minute hour * * MINUTE_SECONDS + second;
}

Int Hour ()
{
Return m_time/HOUR_SECONDS;
}

Int Minute ()
{
Return (m_time/MINUTE_SECONDS) % HOUR_MINUTES;
}

Int Seconds ()
{
Return m_time % MINUTE_SECONDS;
}

};
  • Related