2-8-2 Time类中的运算符重载
2015-05-13 21:29
155 查看
问题及代码:
运行结果:
学习小结:
早上写了一节课还没写完,汗
一开始写重载加号的思路错了,写成了+=的了,运行时发现+完本身的时间也变了才找到这隐藏的错误。
写减号重载时也是差不多的错误,参考了一下贺老的代码。
#include <iostream> using namespace std; class CTime { private: unsigned short int hour; // 时 unsigned short int minute; // 分 unsigned short int second; // 秒 unsigned short int SEC; public: CTime(int h=0,int m=0,int s=0); void setTime(int h,int m,int s); void display(); //二目的比较运算符重载 bool operator > (CTime &t); bool operator < (CTime &t); bool operator >= (CTime &t); bool operator <= (CTime &t); bool operator == (CTime &t); bool operator != (CTime &t); //二目的加减运算符的重载 //返回t规定的时、分、秒后的时间 //例t1(8,20,25),t2(11,20,50),t1+t2为19:41:15 CTime operator+(CTime &t); CTime operator-(CTime &t);//对照+理解 CTime operator+(int s);//返回s秒后的时间 CTime operator-(int s);//返回s秒前的时间 //二目赋值运算符的重载 CTime operator+=(CTime &c); CTime operator-=(CTime &c); CTime operator+=(int s);//返回s秒后的时间 CTime operator-=(int s);//返回s秒前的时间 }; CTime::CTime(int h,int m,int s) { hour=h; minute=m; second=s; SEC=hour*3600+minute*60+second; } void CTime::setTime(int h,int m,int s) { hour=h; minute=m; second=s; SEC=hour*3600+minute*60+second; } void CTime::display() { cout<<hour<<':'<<minute<<':'<<second<<endl; } bool CTime::operator>(CTime &t) { return SEC>t.SEC; } bool CTime::operator<(CTime &t) { return SEC<t.SEC; } bool CTime::operator<=(CTime &t) { return !(SEC>t.SEC); } bool CTime::operator>=(CTime &t) { return !(SEC<t.SEC); } bool CTime::operator==(CTime &t) { return SEC==t.SEC; } bool CTime::operator!=(CTime &t) { return !(SEC==t.SEC); } CTime CTime::operator+(CTime &t) { int h,m,s; s=second+t.second; m=minute+t.minute; h=hour+t.hour; if(s>=60) { ++m; s-=60; } if(m>=60) { ++h; m-=60; } if(h>=24) h-=24; return CTime(h,m,s); } CTime CTime::operator-(CTime &t) { int h,m,s; s=second-t.second; m=minute-t.minute; h=hour-t.hour; if(s<0) { m--; s+=60; } if(m<0) { h--; m+=60; } if(h<0) h+=24; return CTime(h,m,s); } CTime CTime::operator+(int s) { if((second+=s)>=60) { minute+=second/60; second%=60; } if(minute>=60) { hour+=minute/60; minute%=60; } if(hour>=24) hour%=24; return *this; } CTime CTime::operator-(int s) { int hh,mm,ss; ss=s%60; mm=(s/60)%60; hh=(s/3600)%24; 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+=(int s) { *this=*this+s; return *this; } CTime CTime::operator-=(int s) { *this=*this-s; return *this; } int main() { CTime t1(12,0,0),t2; //测试构造函数 t2.setTime(11,59,59); //测试settime() t1.display(); //测试display() t2.display(); cout<<(t1>t2?"t1>t2":"t1<t2")<<endl; //测试operator>() CTime t3(22,1,10),t4(23,50,10); t3.display(); t4.display(); cout<<(t3>=t4?"t3>=t4":"t3<t4")<<endl; //测试operator>=() cout<<(t2<=t1?"t2<=t1":"t2>t1")<<endl; //测试operator<=() cout<<(t3==t4?"t3==t4":"t3!=t4")<<endl; //测试operator==() cout<<(t1!=t2?"t1!=t2":"t1==t2")<<endl; //测试operator!=() (t1+t2).display(); //测试operator+() (t3+t4).display(); (t1-t2).display();//测试operator-() (t3-t4).display(); (t4+600).display(); (t1-20).display(); (t1+=t2).display(); (t1-=t2).display(); (t1+=600).display(); (t1-=300).display(); return 0; }
运行结果:
学习小结:
早上写了一节课还没写完,汗
一开始写重载加号的思路错了,写成了+=的了,运行时发现+完本身的时间也变了才找到这隐藏的错误。
写减号重载时也是差不多的错误,参考了一下贺老的代码。
相关文章推荐
- Time类中的运算符重载
- Time类中的运算符重载
- 第8周项目2-Time类中的运算符重载
- 第9周项目2-Time类中的运算符重载(续)
- 第九周项目2-Time类中的运算符重载(续)(1)定义自加++自减--一目运算符
- 第十二周实践项目2-Time类中的运算符重载
- 第12周项目2—Time类中的运算符重载
- 项目2-Time类中的运算符重载
- Time类中的运算符重载
- Time类中运算符重载的改造
- 第九周项目二 Time类中的运算符重载(续)-(2)定义Time类的>>和<<
- 第十周上机项目2-Time类中的运算符重载
- 第九周项目二 Time类中的运算符重载(续)(2)定义Time类的>>和<<
- 第十二周项目2—Time类中的运算符重载
- 第12周项目2-Time类中的运算符重载
- 第八周【项目2-Time类中的运算符重载】
- 第8周-项目2-Time类中的运算符重载(续)-++、--、>>、<<
- Time类中的运算符重载
- 第九周上机实践项目2——Time类中的运算符重载(续)
- 第九周 项目2-Time类中的运算符重载(1)