Time类中的运算符重载
来源:互联网 发布:搭建dns域名服务器 编辑:程序博客网 时间:2024/05/16 13:02
/** 程序的版权和版本声明部分* Copyright (c)2012, 烟台大学计算机学院学生* All rightsreserved.* 文件名称: object.cpp* 作者:隋 鑫* 完成日期: 2013年 4 月 19 日* 版本号: v1.0* 输入描述:无* 问题描述:* 程序输出:*/#include <iostream>using namespace std;class CTime{private:unsigned short int hour; // 时unsigned short int minute; // 分unsigned short int second; // 秒 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);//二目运算符的重载CTime operator+(CTime &c2);//返回c所规定的时、分、秒后的时间,例t1(8,20,25),t2(11,20,50),t1+t2为:41:15CTime operator-(CTime &c2);//对照+理解CTime operator+(int s);//返回s秒后的时间CTime operator-(int s);//返回s秒前的时间//一目运算符的重载CTime operator++( int);//后置++,下一秒CTime operator++();//前置++,下一秒,前置与后置返回值不一样CTime operator--( int);//后置--,前一秒CTime operator--();//前置--,前一秒//赋值运算符的重载 //CTime operator+=(CTime &c);//CTime operator-=(CTime &c);//CTime operator+=(int s); //CTime operator-=(int s); };//下面实现所有的运算符重载代码。//为简化编程,请注意通过调用已有函数,利用好各函数之间的关系 CTime::CTime(int h,int m,int s){hour=h;minute=m;second=s;} bool CTime::operator > (CTime &t){if(hour>t.hour)return true;else if(hour==t.hour&&minute>t.minute)return true;else if(minute==t.minute&&second>t.second)return true;elsereturn false;}bool CTime::operator < (CTime &t){if(hour<t.hour)return true;else if(hour==t.hour&&minute<t.minute)return true;else if(minute==t.minute&&second<t.second)return true;elsereturn false;}bool CTime::operator >= (CTime &t){if(hour>=t.hour)return true;else if(hour==t.hour&&minute>=t.minute)return true;else if(minute==t.minute&&second>=t.second)return true;elsereturn false;}bool CTime::operator <= (CTime &t){if(hour<=t.hour)return true;else if(hour==t.hour&&minute<=t.minute)return true;else if(minute==t.minute&&second<=t.second)return true;elsereturn false;}bool CTime::operator == (CTime &t){if(hour==t.hour&&minute==t.minute&&second==t.second)return true;elsereturn false;}bool CTime::operator != (CTime &t){if(hour!=t.hour)return true;else if(minute!=t.minute)return true;else if(second!=t.second)return true;elsereturn false;}CTime CTime::operator+(CTime &c2){CTime c;c.second=second+c2.second; c.minute=minute+c2.minute;if(c.second>59)c.second-=60; c.minute++;c.hour=hour+c2.hour;if(c.minute>59) c.minute-=60;c.hour++;if(c.hour>23)c.hour-=24; return c; }CTime CTime::operator-(CTime &c2){CTime c;c.second=second-c2.second;c.minute=minute-c2.minute;if(c.second>59) c.second+=60; c.minute--;c.hour=hour-c2.hour;if(c.minute>59) c.minute+=60;c.hour--;if(c.hour>23)c.hour+=24; return c;}CTime CTime::operator+(int s){CTime c;c.second=second+s;c.minute=minute;c.hour=hour;if(c.second>59) c.minute=c.minute+c.second/60; c.second%=60;if(c.minute>69)c.hour=c.hour+c.minute/60;if(c.hour>23)c.hour%=24;return c;}CTime CTime::operator-(int s){CTime c;c.second=second-s%60;c.minute=minute-s/60%60;c.hour=hour-s/3600%24;if(c.second>59)c.second+=60; c.minute--; if(c.minute>59)c.minute+=60;c.hour--; if(c.hour>23)c.hour+=24;return c;}CTime CTime::operator++( int){ CTime temp(*this); second++;{if(second>=60){second-=60;++minute;}else if(minute>=60){minute-=60;++hour;}else if (hour>=24) hour-=24;} return *this ;}CTime CTime::operator++(){{if(++second>=60){second-=60;++minute;}else if(minute>=60){minute-=60;++hour;}else if (hour>=24) hour-=24;} return *this ;}CTime CTime::operator--( int){ CTime temp(*this); second--;{if(second>=60){second+=60;--minute;}else if(minute>=60){minute+=60;--hour;}else if (hour>=24) hour+=24;} return *this ;}CTime CTime:: operator--(){{if(--second>=60){second+=60;--minute;}else if(minute>=60){minute+=60;--hour;}else if (hour>=24) hour+=24;} return *this ;}void CTime::display(){cout<<hour<<":"<<minute<<":"<<second<<endl;}int main(){CTime t1(8,20,25),t2(11,20,50),t;int s=500;cout<<"t1为:";t1.display();cout<<"t2为:";t2.display();cout<<"s为:"<<s<<"s"<<endl;cout<<endl;cout<<"下面比较两个时间大小:\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.display();t=t1-t2;cout<<"t1-t2="; t.display();cout<<endl;t=t1+s;cout<<"t1+s="; t.display();t=t2+s;cout<<"t2+s="; t.display();t=t1-s;cout<<"t1-s="; t.display();t=t2-s;cout<<"t2-s="; t.display();cout<<endl;t=t1++;cout<<"t1++="; t.display();t=t1--;cout<<"t1--="; t.display();t=t1--;cout<<"t1--="; t.display();t=t2--;cout<<"t2--="; t.display();t=++t1;cout<<"++t1="; t.display();t=++t2;cout<<"++t2="; t.display();t=--t1;cout<<"--t1="; t.display();t=--t2;cout<<"--t2="; t.display();cout<<endl;return 0;}
运行结果:
- 实现Time类中的运算符重载
- Time类中的运算符重载
- Time类中的运算符重载
- Time类中的运算符重载
- Time类中的运算符重载
- Time类中的运算符重载
- 实现Time类中的运算符重载
- 实现Time类中的运算符重载
- Time 类中的运算符重载
- Time类中的运算符重载
- Time类中的运算符重载
- Time类中的运算符重载
- Time类中的运算符重载
- Time类中的运算符重载
- Time类中的运算符重载
- Time类中的运算符重载
- Time类中的运算符重载
- Time类中的运算符重载
- android下有哪些不错的开源项目
- 请教怎么实现MFC Dlg界面多语言界面,急等
- canvas 绘制图形1
- Java 协变性 逆变性 学习笔记
- jstl -- c标签库
- Time类中的运算符重载
- 选择
- Linux NFS 服务器部署时权限设置的注意事项
- 删除修改Eclipse里的SVN账户
- 互斥锁属性
- 新的旅途
- UNICODE,GBK,ASCII,UTF-8等一些编码的区别(转载)
- C++拷贝构造函数(深拷贝,浅拷贝)
- ORA-00600: 内部错误代码, 参数: [19004], [], [], [], [], []