C++实践Time类中的运算符重载参考方法
【项目-Time类中的运算符重载】
实现Time类中的运算符重载。
classCTime { private: unsignedshortinthour;//时 unsignedshortintminute;//分 unsignedshortintsecond;//秒 public: CTime(inth=0,intm=0,ints=0); voidsetTime(inth,intm,ints); voiddisplay(); //二目的比较运算符重载 booloperator>(CTime&t); booloperator<(CTime&t); booloperator>=(CTime&t); booloperator<=(CTime&t); booloperator==(CTime&t); booloperator!=(CTime&t); //二目的加减运算符的重载 //返回t规定的时、分、秒后的时间 //例t1(8,20,25),t2(11,20,50),t1+t2为19:41:15 CTimeoperator+(CTime&t); CTimeoperator-(CTime&t);//对照+理解 CTimeoperator+(ints);//返回s秒后的时间 CTimeoperator-(ints);//返回s秒前的时间 //二目赋值运算符的重载 CTime&operator+=(CTime&c); CTime&operator-=(CTime&c); CTime&operator+=(ints);//返回s秒后的时间 CTime&operator-=(ints);//返回s秒前的时间 //一目运算符的重载 CTimeoperator++(int);//后置++,下一秒 CTime&operator++();//前置++,下一秒 CTimeoperator--(int);//后置--,前一秒 CTime&operator--();//前置--,前一秒 };
提示1:并不是所有比较运算重载函数都很复杂
//比较运算返回的是比较结果,是bool型的true或false //可以直接使用已经重载了的运算实现新运算,例如果已经实现了>,则实现<=就可以很方便了…… boolCTime::operator<=(CTime&t)//判断时间t1<=t2 { if(*this>t)returnfalse; returntrue; }
甚至可以如下面的代码般简练:
boolCTime::operator<=(CTime&t){return!(*this>t)}
提示2:并不是所有复合赋值运算重载函数都需要很复杂
//可以直接使用已经重载了的加减运算实现 //这种赋值,例如t1+=20,直接改变当前对象的值,所以在运算完成后,将*this作为返回值 CTime&CTime::operator+=(CTime&c) { *this=*this+c; return*this; }
提示3:请自行编制用于测试的main()函数,有些结果不必依赖display()函数,提倡用单步执行查看结果
[参考解答]
#includeusingnamespacestd; classCTime { private: unsignedshortinthour;//时 unsignedshortintminute;//分 unsignedshortintsecond;//秒 public: CTime(inth=0,intm=0,ints=0); voidsetTime(inth,intm,ints); //输入输出运算的重载 friendistream&operator>>(istream&in,CTime&t); friendostream&operator<<(ostream&out,CTimet); //比较运算符(二目)的重载 booloperator>(CTime&t); booloperator<(CTime&t); booloperator>=(CTime&t); booloperator<=(CTime&t); booloperator==(CTime&t); booloperator!=(CTime&t); //二目运算符的重载 CTimeoperator+(CTime&c);//返回c所规定的时、分、秒后的时间,例t1(8,20,25),t2(11,20,50),t1+t2为:41:15 CTimeoperator-(CTime&c);//对照+理解 CTimeoperator+(ints);//返回s秒后的时间 CTimeoperator-(ints);//返回s秒前的时间 //一目运算符的重载 CTimeoperator++(int);//后置++,下一秒 CTime&operator++();//前置++,下一秒 CTimeoperator--(int);//后置--,前一秒 CTime&operator--();//前置--,前一秒 //赋值运算符的重载 CTime&operator+=(CTime&c); CTime&operator-=(CTime&c); CTime&operator+=(ints);//返回s秒后的时间 CTime&operator-=(ints);//返回s秒前的时间 }; //构造函数 CTime::CTime(inth,intm,ints) { hour=h; minute=m; second=s; } //设置时间 voidCTime::setTime(inth,intm,ints) { hour=h; minute=m; second=s; } //重载输入运算符>> istream&operator>>(istream&in,CTime&t) { charch1,ch2; while(1) { cout<<"请输入时间(hh:mm:ss)"; cin>>t.hour>>ch1>>t.minute>>ch2>>t.second; if(ch1==':'&&ch2==':') if(t.hour>-1&&t.hour<24&&t.minute>-1&&t.minute<60&&t.second>-1&&t.second<60)break; cerr<<"时间格式不正确!请重新输入\n"; } returncin; } //重载输出运算符<< ostream&operator<<(ostream&out,CTimet) { out< (CTime&t)//判断时间t1>t2 { if(hour>t.hour)returntrue; if(hour t.minute)returntrue; if(minute t.second)returntrue; returnfalse; } boolCTime::operator<(CTime&t)//判断时间t1 t.hour)returnfalse; if(minute t.minute)returnfalse; if(second t)returnfalse; returntrue; } boolCTime::operator!=(CTime&t)//判断时间t1!=t2 { if(*this==t)returnfalse; returntrue; } boolCTime::operator>=(CTime&t)//判断时间t1>=t2 { if(*this t)returnfalse; returntrue; } //二目运算符的重载 //计算时间之和,返回c所规定的时、分、秒后的时间,例t1(8,20,25),t2(11,20,50),t1+t2为:41:15 CTimeCTime::operator+(CTime&t) { inth,m,s; s=second+t.second; m=minute+t.minute; h=hour+t.hour; if(s>59) { s-=60; m++; } if(m>59) { m-=60; h++; } while(h>23)h-=24; CTimet0(h,m,s); returnt0; } //返回s秒后的时间 CTimeCTime::operator+(ints) { intss=s%60; intmm=(s/60)%60; inthh=s/3600; CTimet0(hh,mm,ss); return*this+t0; } //计算时间之差 CTimeCTime::operator-(CTime&t) { inth,m,s; s=second-t.second; m=minute-t.minute; h=hour-t.hour; if(s<0) { s+=60; m--; } if(m<0) { m+=60; h--; } while(h<0)h+=24; CTimet0(h,m,s); returnt0; } //返回s秒前的时间 CTimeCTime::operator-(ints) { intss=s%60; intmm=(s/60)%60; inthh=s/3600; CTimet0(hh,mm,ss); return*this-t0; } //一目运算符的重载 CTimeCTime::operator++(int)//后置++,下一秒 { CTimet=*this; *this=*this+1; returnt; } CTime&CTime::operator++()//前置++,下一秒 { *this=*this+1; return*this; } CTimeCTime::operator--(int)//后置--,前一秒 { CTimet=*this; *this=*this-1; returnt; } CTime&CTime::operator--()//前置--,前一秒 { *this=*this-1; return*this; } //赋值运算符的重载 CTime&CTime::operator+=(CTime&c) { *this=*this+c; return*this; } CTime&CTime::operator-=(CTime&c) { *this=*this-c; return*this; } CTime&CTime::operator+=(ints)//返回s秒后的时间 { *this=*this+s; return*this; } CTime&CTime::operator-=(ints)//返回s秒前的时间 { *this=*this-s; return*this; } intmain() { CTimet1,t2,t; cout<<"t1为:"; cin>>t1; cout<<"t2为:"; cin>>t2; cout<<"下面比较两个时间大小:\n"; if(t1>t2)cout<<"t1>t2"< =t2)cout<<"t1≥t2"< 总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对毛票票的支持。如果你想了解更多相关内容请查看下面相关链接
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。