Nineth Week on-machine practice project operator overloading in the 2--time class (cont.)

Source: Internet
Author: User

On the basis of operator overloading in the time class
(1) define the self-increment and decrement operators for time objects

    //一目运算符的重载    operator++(int);//后置++,下一秒    operator++();//前置++,下一秒,前置与后置返回值不一样    operatorint);//后置--,前一秒    operator--();//前置--,前一秒

(2) define the << and >> operator overloads in the time class, realize the input and output of the times, and transform the display mode of the operation results in the original program, making the program read more naturally.

Code

/* Copyright (c) 2015, Yantai University School of Computer * All rights reserved. * File Name: Test.cpp * Author: Simbin * Completion Date: May 8, 2015 * version number: v1 .0 * /#include <iostream>using namespace STD;classctime{Private:unsigned  Short intHour//Time    unsigned  Short intMinute//min    unsigned  Short intSecond//Sec Public: CTime (intH=0,intm=0,ints=0);voidSetTime (intHintMints);//Two purpose comparison operator overloading    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 overload of the unary operatorCTimeoperator++(int);//Post + +, next secondCTimeoperator++();//Front + +, next second, front and back return value not the sameCTimeoperator--(int);//back--, one secondCTimeoperator--();//Front--, one second    //Returns the time, minutes, and seconds specified by T    //Example T1 (8,20,25), T2 (11,20,50), t1+t2 for 19:41:15CTimeoperator+ (CTime &t); CTimeoperator-(CTime &t);//contrast + understandingCTimeoperator+(ints);//Returns the time after S secondsCTimeoperator-(ints);//Returns the time before S seconds    overload of the//two-mesh assignment operatorCTimeoperator+ = (CTime &c); CTimeoperator-= (CTime &c); CTimeoperator+=(ints);//Returns the time after S secondsCTimeoperator-=(ints);//Returns the time before S seconds    friendostream&operator<< (Ostream&,Constctime&);friendistream&operator>> (istream&,ctime&);}; Ctime::ctime (intHintMints) {hour=h;    Minute=m; Second=s;}voidCtime::settime (intHintMints) {hour=h;    Minute=m; Second=s;}BOOLCTime::operator> (CTime &t) {if(Hour>t.hour)return true;Else if(Hour<t.hour)return false;Else if(Minute>t.hour)return true;Else if(Minute<t.minute)return false;Else if(Second>t.hour)return true;Else if(Second<t.hour)return false;Else return false;}BOOLCTime::operator< (CTime &t) {if(Hour<t.hour)return true;Else if(Hour>t.hour)return false;Else if(Minute<t.hour)return true;Else if(Minute>t.minute)return false;Else if(Second<t.hour)return true;Else if(Second>t.hour)return false;Else return false;}BOOLCTime::operator>= (CTime &t) {return! (* This&LT;T);}BOOLCTime::operator<= (CTime &t) {return! (* This&GT;T);}BOOLCTime::operator= = (CTime &t) {if(Hour==t.hour&&minute==t.minute&&second==t.second)return true;Else        return false;}BOOLCTime::operator! = (CTime &t) {return! (* This==t);} CTime CTime::operator+ (CTime &t) {intH=hour+t.hour;intM=minute+t.minute;intS=second+t.second;if(s>= -) {s-= -;    m++; }if(m>= -) {m-= -;    h++; }if(h>= -) {h-= -; } CTime C (h,m,s);returnC;} CTime CTime::operator-(CTime &t) {intH,m,s;    S=second-t.second;    M=minute-t.minute; H=hour-t.hour;if(s<0) {s+= -;    m--; }if(m<0) {m+= -;    h--; }if(h<0) h+= -; CTime t0 (h,m,s);returnt0;} CTime CTime::operator+(ints) {intss=s% -;intMm= (s/ -)% -;inthh=s/3600; CTime T (HH,MM,SS);return* This+t;} CTime CTime::operator-(ints) {intss=s% -;intMm= (s/ -)% -;inthh=s/3600; CTime T (HH,MM,SS);return* This-T;} CTime CTime::operator+ = (CTime &t) {* This=* This+t;return* This;} CTime CTime::operator-= (CTime &t) {* This=* This-T;return* This;} CTime CTime::operator+=(ints) {* This=* This+s;return* This;} CTime CTime::operator-=(ints) {* This=* This-S;return* This;} CTime CTime::operator++(int) {CTime t (* This); * This+=1;returnt;} CTime CTime::operator++(){    * This+=1;return* This;} CTime CTime::operator--(int) {CTime t (* This); * This-=1;returnt;} CTime CTime::operator--(){    * This-=1;return* This;} ostream&operator<< (ostream& out,Constctime& c) {out<<c.hour<<":"<<c.minute<<":"<<c.second;returnOut;} istream&operator>> (istream& in,ctime& c) {cout<<"input, minutes, seconds:"; in>>c.hour>>c.minute>>c.second;returnIn;}intMain () {CTime T1 (8, -, -), T2 ( One, -, -), T;cout<<"T1 as:"<<t1<<endl;cout<<"T2 as:"<<t2<<endl;cout<<"Compare two time sizes below: \ n";if(T1&GT;T2)cout<<"T1>t2"<<endl;if(T1&LT;T2)cout<<"T1<t2"<<endl;if(T1==T2)cout<<"T1=t2"<<endl;if(T1!=T2)cout<<"T1≠t2"<<endl;if(T1&GT;=T2)cout<<"T1≥t2"<<endl;if(T1&LT;=T2)cout<<"T1≤t2"<<endl;cout<<endl; T=t1+t2;cout<<"t1+t2="<<t<<endl; T=t1-t2;cout<<"t1-t2="<<t<<endl; t=t1+ -;cout<<"t1+2000="<<t<<endl; T=t1- the;cout<<"t1-5000="<<t<<endl; T1+=t2;cout<<"t1+=t2="<<t1<<endl; T1-=t2;cout<<"t1-=t2="<<t1<<endl; t1+= -;cout<<"t1+=2000="<<t1<<endl; t1-= the;cout<<"t1-=5000="<<t1<<endl;cout<<"t1++="<<t1++<<endl;cout<<"++t1="<<++t1<<endl;cout<<"t2--="<<t2--<<endl;cout<<"--t2="<<--t2<<endl;return 0;}

Run results

Nineth Week on-machine practice project operator overloading in the 2--time class (cont.)

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.