/*
* 作    者: 霍雨佳
* 完成日期:2014 年4月15日
* 版 本 号:v1.0
* 问题描述:实现Time类中的运算符重载
* 样例输入:
* 样例输出:
* 项目要求:实现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为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秒前的时间
};
//下面实现所有的运算符重载代码。
void CTime::setTime(int h,int m,int s)
{
hour=h;
minute=m;
second=s;
}
void CTime::display()
{
cout<<"("<<hour<<":"<<minute<<":"<<second<<")"<<endl;
}
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;
return false;
}
bool CTime::operator < (CTime &t)
{
if (hour>t.hour) return false;
if (hour<t.hour) return true;
if (minute>t.minute) return false;
if (minute<t.minute) return true;
if (second>t.second) return false;
return true;
}
bool CTime::operator == (CTime &t)
{
if (*this <t||*this>t) return false;
return true;
}
bool CTime::operator != (CTime &t)
{
if (*this == t) return false;//可以直接使用已经重载了的运算实现新运算
return true;
}
bool CTime::operator >= (CTime &t)
{
if (*this < t) return false;
return true;
}
bool CTime::operator <= (CTime &t)
{
if (*this > t) return false;
return true;
}
CTime CTime::operator+(CTime &t)
{
int ss,mm,hh;
ss=second+t.second;
mm=minute+t.minute;
hh=hour+t.hour;
if(ss>59)
{
ss=ss-60;
mm++;
}
if(mm>59)
{
mm=mm-60;
hh++;
}
if(hh>23)
{
hh-=24;
}
CTime q(hh,mm,ss);
return q;
}
CTime CTime::operator+(int s)//返回s秒后的时间
{
int ss=s%60;  //抄之有道,`(*∩_∩*)′
int mm=(s/60)%60;
int hh=s/3600;
CTime t1(hh,mm,ss);
return *this+t1;
}
CTime CTime::operator-(CTime &t)//对照+理解
{
int ss,mm,hh;
ss=second-t.second;
mm=minute-t.minute;
hh=hour-t.hour;
if(ss<0)
{
ss+=60;
mm--;
}
if(mm<0)
{
mm+=60;
hh--;
}
if(hh<0)
{
hh+=24;
}
CTime q(hh,mm,ss);
return q;
}
CTime CTime::operator-(int s)//返回s秒前的时间
{
int ss=s%60;  //抄之有道,`(*∩_∩*)′
int mm=(s/60)%60;
int hh=s/3600;
CTime t1(hh,mm,ss);
return *this-t1;
}
//二目赋值运算符的重载
//可以直接使用已经重载了的加减运算实现
//这种赋值, 例如 t1+=20,直接改变当前对象的值,所以在运算完成后,将*this作为返回值
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)//返回s秒前的时间
{
*this=*this-s;
return *this;
}
CTime CTime::operator+=(int s)//返回s秒后的时间
{
*this=*this+s;
return *this;
}
//自行编制用于测试的main()函数,有些结果不必依赖display()函数,提倡用单步执行查看结果
int main()
{
CTime t1(9,28,8),t2(7,56,27),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<<endl;
cout<<"t1与t2的计算 \n";
t=t1+t2;
cout<<"t1+t2=";
t.display();
t=t1-t2;
cout<<"t=t1-t2=";
t.display();
t=t1+80;
cout<<"t=t1+80=";
t.display();
t=t1-80;
cout<<"t=t1-80=";
t.display();
t1+=t2;
cout<<"t1+=t2=";
t1.display();
t1-=t2;
cout<<"t1-=t2=";
t1.display();
t1+=80;
cout<<"t1+=80=";
t1.display();
t1-=80;
cout<<"t1-=80=";
t1.display();
return 0;
}

运行结果:

心得体会:

通过这个项目对运算符重载有了更深一步的理解与体会(*^__^*) ……

8-2 实现Time类中的运算符重载相关推荐

  1. 第十三周项目一-分数类中的运算符重载

    /**Copyright(c)2016,烟台大学计算机与控制工程学院*All rights reserved*文件名称:123.cpp*作 者:王蕊*完成日期:2016年5月25日*版 本 号:v1. ...

  2. 第十二周项目二-Time类中的运算符重载

    /**Copyright(c)2016,烟台大学计算机与控制工程学院*All rights reserved*文件名称:123.cpp*作 者:王蕊*完成日期:2016年5月24日*版 本 号:v1. ...

  3. 项目3-分数类中的运算符重载

    /* * Copyright (c) 2011, 烟台大学计算机学院 * All rights reserved. * 作 者:王静 * 完成日期:2013 年 5 月 5 日 * 版 本 号:v1. ...

  4. /项目3-分数类中的运算符重载

    /* * Copyright (c) 2011, 烟台大学计算机学院 * All rights reserved. * 作 者:王静 * 完成日期:2013 年 4 月 24 日 * 版 本 号:v1 ...

  5. 实现复数类中的运算符重载(含有double类型)

    /* * 作 者: 霍雨佳 * 完成日期:2014 年4月15日 * 版 本 号:v1.0 * 问题描述:实现复数类中的运算符重载. * 样例输入: * 样例输出: * 问题分析:一个定义完整的类,是 ...

  6. 8-3 实现分数类中的运算符重载

    /* * 作 者: 霍雨佳 * 完成日期:2014 年4月15日 * 版 本 号:v1.0 * 问题描述:实现分数类中的运算符重载 * 样例输入: * 样例输出: * 项目要求:实现分数类中的运算符重 ...

  7. 第十二周上机实践项目 项目1-实现复数类中的运算符重载 (2)

    问题及代码: [项目-实现复数类中的运算符重载] (1)请用类的成员函数,定义复数类重载运算符+.-.*./,使之能用于复数的加减乘除 class Complex { public:Complex() ...

  8. 第7周-项目1-完整实现复数类中的运算符重载-扩展+、-、*、/运算符的功能

    问题及代码: /* *Copyright (c)2015,烟台大学计算机与控制工程学院 *All rights reserved. *文件名称:number.cpp *作 者:单昕昕 *完成日期:20 ...

  9. 第十二周-实现复数类中的运算符重载

    /* *Copyright (c) 2016,烟台大学计算机学院 *All rights reserved. *文件名称:main.cpp *作    者:李德彪 *完成时间:2016年5月23日 * ...

最新文章

  1. 最全综述 | 图像目标检测
  2. php 操作mssql,php操作mssql
  3. 职称计算机和职称英语期限,专业技术职称考试,职称英语和职称计算机有效期多长时间,每年什么时候报名啊?上海的...
  4. 华为百度再次携手AI人工智能未来科技,为什么不是腾讯阿里巴巴?
  5. 零基础转行学Java到底难不难?
  6. error LNK2019: unresolved external symbol __imp__ func@20 referenced in function..
  7. Nature封面:大团队日趋中庸,小团队更容易出颠覆性创新
  8. 可以创建专业的客户端/服务器视频会议应用程序的音频和视频控件LEADTOOLS Video Conferencing SDK...
  9. 扩展Reporting Services和SQL Server 2016中的更改
  10. jms.jar 2.0_JMS API概述:JMS 1.x和JMS 2.x
  11. mysql 子查询 性能_mysql子查询性能
  12. FastDFS原理及维护
  13. 无人机飞控系统硬件设计
  14. 谈谈那些“你以为”的安全
  15. 通过slave_exec_mode=IDEMPOTENT跳过主从复制中的错误(1032 1062)
  16. IPv6 内网穿透(一)
  17. 计算机网络并行传输和串行传输,网线RJ45是并行传输还是串行
  18. SQL 开窗函数使用
  19. Maven 自定义 archeType
  20. github相关的网站

热门文章

  1. tp-link tl-wr740n 虚拟服务器,TPlinktl-wr740n服务器无响应,路由器上的广域网信号? 爱问知识人...
  2. vCenter通过vMotion迁移虚拟机条件
  3. 【语音增强论文解读 02】DCCRN: Deep Complex Convolution Recurrent Network for Phase-AwareSpeech Enhancement
  4. 分享二维码领红包系统开发
  5. 3dmax入门 | 学3d建模必备软件技能基础教学
  6. 家园守卫战新系统冲入海盗营地漏洞,无限赚钱
  7. 基于STM32F103单片机的无线病床呼叫系统
  8. 常见商品期货量化易略
  9. 基于体感的三维虚拟试衣
  10. beyond compare 2 中文乱码问题