【项目2-Time类中的运算符重载】实现Time类中的运算符重载
来源:互联网 发布:2015年出境旅游数据 编辑:程序博客网 时间:2024/05/17 21:39
/** 程序的版权和版本声明部分* Copyright (c)2014, 烟台大学计算机学院学生* All rightsreserved.* 文件名称: array.cpp* 作 者: 刘江* 完成日期:2013年4月17日* 版本号: v1.0* 输入描述:无* 问题描述:运算符的重载,实现Time类中的运算符重载*/#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):hour(h),minute(m),second(s) {} 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为: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秒前的时间};//下面实现所有的运算符重载代码。void CTime::display(){ cout<<hour<<":"<<minute<<":"<<second<<endl;}void CTime::setTime(int h,int m,int s){ hour=h; minute=m; second=s;}bool CTime::operator > (CTime &t){ if(hour>t.hour) return true; if(hour<t.hour) return false; if(minute>t.minute) return true; if(minute<t.minute) return false; if(second>t.second) return true; if(second<t.second) return false;}bool CTime::operator < (CTime &t){ if(hour<t.hour) return true; if(hour>t.hour) return false; if(minute<t.minute) return true; if(minute>t.minute) return false; if(second<t.second) return true; if(second>t.second) return false;}bool CTime::operator == (CTime &t){ if(*this<t||*this>t) return false; else return true;}bool CTime::operator >= (CTime &t){ if(*this<t) return false; else return true;}bool CTime::operator <= (CTime &t){ if(*this>t) return false; else return true;}bool CTime::operator != (CTime &t){ if(*this==t) return false; else return true;}CTime CTime::operator+(CTime &t){ int h1,m1,s1; h1=hour+t.hour; m1=minute+t.minute; s1=second+t.second; if(s1>59) { s1-=60; m1++; } if(m1>59) { m1-=60; h1++; } if(h1>23) { h1-=24; } CTime tt(h1,m1,s1); return tt;}CTime CTime::operator-(CTime &t){ int h1,m1,s1; h1=hour-t.hour; m1=minute-t.minute; s1=second-t.second; if(s1<0) { s1+=60; m1--; } if(m1<0) { m1+=60; h1--; } if(h1<0) { h1+=24; } CTime tt(h1,m1,s1); return tt;}CTime CTime::operator+(int s){ int h1,m1,s1; s1=s%60; m1=(s/60)%60; h1=s/3600; CTime tt(h1,m1,s1); return *this+tt;}CTime CTime::operator-(int s){ int h1,m1,s1; s1=s%60; m1=(s/60)%60; h1=s/3600; CTime tt(h1,m1,s1); return *this-tt;}CTime CTime::operator+=(CTime &c){ *this=*this+c; return *this;}CTime CTime::operator-=(CTime &c){ *this=*this-c; return *this;}CTime CTime::operator+=(int s){ *this=*this+s; return *this;}CTime CTime::operator-=(int s){ *this=*this-s; return *this;}//自行编制用于测试的main()函数,有些结果不必依赖display()函数,提倡用单步执行查看结果int main(){ CTime t1(8,20,25),t2(11,20,50),t; cout<<"t1为:"; t1.display(); cout<<"t2为:"; t2.display(); 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;t.display(); t=t1-t2;t.display(); t=t1+2000;t.display(); t=t1-5000;t.display(); t1+=t2;t1.display(); t1-=t2;t1.display(); t1+=2000;t1.display(); t1-=5000;t1.display(); return 0;}
运行结果:
心得体会:
对于老贺的借鉴了一部分,发现自己想的太多了,而且错误百出,但绝对会抄之有道 0 0
- 【项目2-Time类中的运算符重载】实现Time类中的运算符重载
- 实现Time类中的运算符重载
- 实现Time类中的运算符重载
- 实现Time类中的运算符重载
- 第八周【项目2-Time类中的运算符重载】实现Time类中的运算符重载
- 项目2-Time类中的运算符重载
- 项目2-Time类中的运算符重载
- 项目2-Time类中的运算符重载
- 第八周 项目2 TIME类中的运算符重载
- 第八周 项目2 Time类中的运算符重载
- 第八周项目2-TIME类中的运算符重载
- 八周 项目2 Time类中的运算符重载
- 第八周 项目2-Time类中的运算符重载
- 第八周项目2-time类中的运算符重载
- 第九周-项目2-Time类中的运算符重载
- 项目2-Time类中的运算符重载
- 第八周项目2 TIME类中的运算重载符
- 第八周【项目2-Time类中的运算符重载】
- WPF Tips:Using d:DataContext design time expression
- 学习方法书籍 很好推荐
- 唯爱小粽子:无法解析导入Java类库中的包的方法
- 在CI中使用自定义钩子
- UML_状态图
- 【项目2-Time类中的运算符重载】实现Time类中的运算符重载
- 关于蓝牙的几篇文章
- Android设置无标题跟全屏
- ubuntu vsftpd 的默认根目录是什么
- 搜索引擎盘点 痛苦和爱交织
- 《黑马程序员》 IO之复制图片
- 《Head First 设计模式》代理模式
- 你所不知道的配置管理(1)——配置管理概念
- restore hadoop primary namenode from secondary namenode checkpoint step by step