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<T);}BOOLCTime::operator<= (CTime &t) {return! (* This>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>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<=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.)