Overloading << and >> in the CTime class

Source: Internet
Author: User

Program code:

#include <iostream>using namespace Std;class ctime//time class {private:unsigned short int hour;  When unsigned short int minute;  Sub unsigned short int second; Seconds public:ctime (int h=0,int m=0,int s=0);//constructor void settime (int h,int m,int s);//Set time//overload >> implement input time friend    istream& operator >> (istream& input, ctime& c);    Overload << achieve output time friend ostream& operator << (ostream& output, ctime& c); overloaded bool operator > (CTime &t), bool operator < (CTime &t), bool operator >= (CTime &t); bool    Operator <= (CTime &t); bool operator = = (CTime &t); bool operator! = (CTime &t); The overloaded CTime operator+ (CTime &c) of the two-mesh operator returns the time, minutes, and seconds specified by C. Example T1 (8,20,25), T2 (11,20,50), T1+t2:41:15ctime operator-(CTime &c);//contrast + understanding CTime operator+ (int s);//return s seconds after CTime operator-(int s);//returns the time before//the overload of the unary operator CTime operator++ (int);//post + +, next second CTime operator++ ();//front + +, next second, The front and back return values are not the same ctime operator--(int);//post--, the previous second CTime Operator--();//Front--。 The previous second///assignment operator overload CTime operator+= (CTime &c); CTime operator-= (CTime &c); CTime operator+= (int s); CTime operator-= (int s);    };//initialization Time Ctime::ctime (int h, int m, int s) {hour = h;    minute = m; Second = s;}    Initialization time void ctime::settime (int h,int m,int s) {hour = h;    minute = m; Second = s;}       Overload >> implement input time istream& operator >> (istream& input, ctime& c) {char ch1, ch2;//save and colon between minutes, minutes, and seconds    do {input>>c.hour>>ch1>>c.minute>>ch2>>c.second;    }while (! (': ' = = ch1 && ': ' = = CH2)); return input;} Overload << implement output time ostream& operator << (ostream& output, ctime& c) {output<<c.hour<< ': '     <<c.minute<< ': ' <<c.second<<endl; return output;}    The overloaded bool Ctime::operator > (CTime &t) {//calculation time is the number of seconds int sec1 = hour * 3600 + minute * + second for the comparison operator (second mesh)    int sec2 = t.hour * 3600 + t.minute * + T.second; if (Sec1 > SEC2) {return true;    } else {return false;    }}bool Ctime::operator < (CTime &t) {//Calculation time is how many seconds int sec1 = hour * 3600 + minute * + second;    int sec2 = t.hour * 3600 + t.minute * + T.second;    if (Sec1 < SEC2) {return true;    } else {return false;    }}bool ctime::operator >= (CTime &t) {//Calculation time is how many seconds int sec1 = hour * 3600 + minute * + second;    int sec2 = t.hour * 3600 + t.minute * + T.second;    if (sec1 >= sec2) {return true;    } else {return false;    }}bool ctime::operator <= (CTime &t) {//Calculation time is how many seconds int sec1 = hour * 3600 + minute * + second;    int sec2 = t.hour * 3600 + t.minute * + T.second;    if (sec1 <= sec2) {return true;    } else {return false;    }}bool Ctime::operator = = (CTime &t) {//calculation time is the number of seconds int sec1 = hour * 3600 + minute * + second; int sec2 = t.hour * 3600 + t.minute * + T.second;   if (sec1 = = sec2) {return true;    } else {return false;    }}bool ctime::operator! = (CTime &t) {//Calculation time is how many seconds int sec1 = hour * 3600 + minute * + second;    int sec2 = t.hour * 3600 + t.minute * + T.second;    if (sec1! = sec2) {return true;    } else {return false; }}//the overload of the two mesh operator CTime ctime::operator+ (CTime &c)//Returns the time, minutes, and seconds specified in C.    Example T1 (8,20,25), T2 (11,20,50), t1+t2:41:15{//time into seconds int sec1 = hour * 3600 + minute * + second;    int sec2 = c.hour * 3600 + c.minute * + C.second;    Two time add int sec3 = SEC1 + sec2;    CTime T1; T1.hour = sec3/3600;//calculation time (time) T1.minute = sec3% 3600/60;//calculation time (min) t1.second = sec3% 3600 60;//calculation time (seconds) ret Urn T1;}    CTime ctime::operator-(CTime &c)//contrast + Understanding {//will time into seconds int sec1 = hour * 3600 + minute * + second;    int sec2 = c.hour * 3600 + c.minute * + C.second;    Subtract two times int sec3 = SEC1-SEC2;    CTime T1; T1.hour = sec3/3600;//calculation time (time)   T1.minute = sec3% 3600/60;//calculation time (min) t1.second = sec3% 3600 60;//calculation time (seconds) return T1;}        CTime ctime::operator+ (int s)//returns the time after s seconds {CTime T;    Time into seconds int sec1 = hour * 3600 + minute * + second;    Calculates the time after adding s seconds int sec2 = SEC1 + S;  T.hour = sec2/3600;//calculation time (time) T.minute = sec2% 3600/60;//calculation time (min) t.second = sec2% 3600 60;//calculation time (seconds) return t;}        CTime ctime::operator-(int s)//return s seconds before time {CTime T;    Time into seconds int sec1 = hour * 3600 + minute * + second;    Computes the time after which the S-second is reduced int sec2 = sec1-s;  T.hour = sec2/3600;//calculation time (time) T.minute = sec2% 3600/60;//calculation time (min) t.second = sec2% 3600 60;//calculation time (seconds) return t;} The overload of the CTime operator is ctime::operator++ (int)//post + +.    such as i++{CTime t = *this;    *this = *this + 1; return t;} CTime ctime::operator++ ()//front + +, such as ++i;    {*this = *this + 1; return *this;}    CTime ctime::operator--(int)//post--, such as i--{CTime t = *this;    *this = *this + 1; return t;} CTime ctime::operator--()//front--, such as I.{*this = *this-1; return *this;}    Add two times (this is a pointer to the time Class) CTime ctime::operator+= (CTime &c) {*this = *this + C; return *this;}    Subtract two times (this is a pointer to the time Class) CTime ctime::operator-= (CTime &c) {*this = *this-c; return *this;}        Add s second CTime ctime::operator+= (int s) {//time into seconds int sec1 = hour * 3600 + minute * + second;    Calculates the time after adding s seconds int sec2 = SEC1 + S;    CTime T1; T1.hour = sec2/3600;//calculation time (time) T1.minute = sec2% 3600/60;//calculation time (min) t1.second = sec2% 3600 60;//calculation time (seconds) ret Urn T1;}        Decrease S-second CTime ctime::operator-= (int s) {//time into seconds int sec1 = hour * 3600 + minute * + second;    Calculates the time after adding s seconds int sec2 = sec1-s;    CTime T1; T1.hour = sec2/3600;//calculation time (time) T1.minute = sec2% 3600/60;//calculation time (min) t1.second = sec2% 3600 60;//calculation time (seconds) ret Urn T1;}    void Main () {//define three time objects CTime T1, T2, T;    cout<< "Please enter the first time:";    cin>>t1;    cout<< "Please enter a second time:";    cin>>t2; Show first time cout<< "T1= ";cout<<t1;    Show second time cout<< "t2 =";cout<<t2; cout<< "\ n" is two times the size: \ n "; if (t1>t2) cout<<" T1>t2 "<<endl;if (t1<t2) cout<<" T1<t2 "<<endl;if (T1==T2) cout<<" T1=t2 "<<endl; if (t1!=t2) cout<< "T1≠t2" <<endl;if (t1>=t2) cout<< "T1≥t2" <<endl;if (T1&LT;=T2) cout<    < "T1≤t2" <<endl;cout<<endl;    Two time add cout<< "T1 + t2 =";    t = t1 + t2;        cout<<t;    Two time subtraction cout<< "t1-t2 =";    t = t1-t2;    cout<<t;    Calculate T1 add 300 seconds after the time cout<< "T1 + 300 seconds =";    t = t1 + 300;    cout<<t;    Calculate T1 to decrease the time after 300 seconds cout<< "t1-300 seconds =";    t = t1-300;    cout<<t;    cout<<endl;    cout<< "T1 =";     cout<<t1;    Calculate t1++ cout<< "t1++ =";    t = t1++;    cout<<t;     cout<<endl;    cout<< "T1 =";     cout<<t1;    Calculate ++t1 cout<< "++t1 =";    t = ++t1;  cout<<t;  cout<<endl;    cout<< "T1 =";     cout<<t1;    Calculate t1--cout<< "t1--=";    t = t1--;    cout<<t;     cout<<endl;    cout<< "T1 =";     cout<<t1;    Calculate--t1 cout<< "--t1 =";    t =--t1;    cout<<t;    cout<<endl;    cout<< "t2 =";    cout<<t2;    cout<< "T =";    cout<<t;    T2 + = t;    cout<< "Run t2 + = T1 after t2 =";    cout<<t2;    cout<<endl;    cout<< "t2 =";    cout<<t2;    cout<< "T =";    cout<<t;     T2-= t;    cout<< "Run t2-= T1 after t2 =";    cout<<t2;    cout<<endl; System ("Pause");}


Operation Result:


Overloading &lt;&lt; and &gt;&gt; in the CTime class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.