程序代码:

#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);//设置时间 //重载>>实现输入时间
friend istream& operator >>(istream& input, CTime& c); //重载<<实现输出时间
friend ostream& operator <<(ostream& output, CTime& c); //比較运算符(二目)的重载
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 &c);//返回c所规定的时、分、秒后的时间。例t1(8,20,25),t2(11,20,50),t1+t2为:41:15
CTime operator-(CTime &c);//对比+理解
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;
} //初始化时间
void CTime::setTime(int h,int m,int s)
{
hour = h;
minute = m;
second = s;
} //重载>>实现输入时间
istream& operator >>(istream& input, CTime& c)
{
char ch1, ch2;//保存时和分、分和秒之间的冒号 do
{
input>>c.hour>>ch1>>c.minute>>ch2>>c.second; }while(!(':' == ch1 && ':' == ch2)); return input;
} //重载<<实现输出时间
ostream& operator <<(ostream& output, CTime& c)
{
output<<c.hour<<':'<<c.minute<<':'<<c.second<<endl; return output;
} //比較运算符(二目)的重载
bool CTime::operator > (CTime &t)
{
//计算时间为多少秒
int sec1 = hour * 3600 + minute * 60 + second;
int sec2 = t.hour * 3600 + t.minute * 60 + t.second; if(sec1 > sec2)
{
return true;
}
else
{
return false;
}
} bool CTime::operator < (CTime &t)
{
//计算时间为多少秒
int sec1 = hour * 3600 + minute * 60 + second;
int sec2 = t.hour * 3600 + t.minute * 60 + t.second; if(sec1 < sec2)
{
return true;
}
else
{
return false;
}
} bool CTime::operator >= (CTime &t)
{
//计算时间为多少秒
int sec1 = hour * 3600 + minute * 60 + second;
int sec2 = t.hour * 3600 + t.minute * 60 + t.second; if(sec1 >= sec2)
{
return true;
}
else
{
return false;
}
} bool CTime::operator <= (CTime &t)
{
//计算时间为多少秒
int sec1 = hour * 3600 + minute * 60 + second;
int sec2 = t.hour * 3600 + t.minute * 60 + t.second; if(sec1 <= sec2)
{
return true;
}
else
{
return false;
}
} bool CTime::operator == (CTime &t)
{
//计算时间为多少秒
int sec1 = hour * 3600 + minute * 60 + second;
int sec2 = t.hour * 3600 + t.minute * 60 + t.second; if(sec1 == sec2)
{
return true;
}
else
{
return false;
}
} bool CTime::operator != (CTime &t)
{
//计算时间为多少秒
int sec1 = hour * 3600 + minute * 60 + second;
int sec2 = t.hour * 3600 + t.minute * 60 + t.second; if(sec1 != sec2)
{
return true;
}
else
{
return false;
}
}
//二目运算符的重载
CTime CTime::operator+(CTime &c)//返回c所规定的时、分、秒后的时间。例t1(8,20,25),t2(11,20,50),t1+t2为:41:15
{
//将时间化成秒数
int sec1 = hour * 3600 + minute * 60 + second;
int sec2 = c.hour * 3600 + c.minute * 60 + c.second; //两个时间相加
int sec3 = sec1 + sec2; CTime t1; t1.hour = sec3 / 3600;//计算时间(时)
t1.minute = sec3 % 3600 / 60;//计算时间(分)
t1.second = sec3 % 3600 % 60;//计算时间(秒) return t1;
} CTime CTime::operator-(CTime &c)//对比+理解
{
//将时间化成秒数
int sec1 = hour * 3600 + minute * 60 + second;
int sec2 = c.hour * 3600 + c.minute * 60 + c.second; //两个时间相减
int sec3 = sec1 - sec2; CTime t1; t1.hour = sec3 / 3600;//计算时间(时)
t1.minute = sec3 % 3600 / 60;//计算时间(分)
t1.second = sec3 % 3600 % 60;//计算时间(秒) return t1;
} CTime CTime::operator+(int s)//返回s秒后的时间
{
CTime t; //将时间化成秒数
int sec1 = hour * 3600 + minute * 60 + second; //计算添加s秒后的时间
int sec2 = sec1 + s; t.hour = sec2 / 3600;//计算时间(时)
t.minute = sec2 % 3600 / 60;//计算时间(分)
t.second = sec2 % 3600 % 60;//计算时间(秒) return t;
} CTime CTime::operator-(int s)//返回s秒前的时间
{ CTime t; //将时间化成秒数
int sec1 = hour * 3600 + minute * 60 + second; //计算降低s秒后的时间
int sec2 = sec1 - s; t.hour = sec2 / 3600;//计算时间(时)
t.minute = sec2 % 3600 / 60;//计算时间(分)
t.second = sec2 % 3600 % 60;//计算时间(秒) return t;
} //一目运算符的重载
CTime CTime::operator++( int)//后置++。如i++
{
CTime t = *this;
*this = *this + 1; return t;
} CTime CTime::operator++()//前置++, 如++i;
{
*this = *this + 1; return *this;
} CTime CTime::operator--( int)//后置--, 如i--
{
CTime t = *this;
*this = *this + 1;
return t;
} CTime CTime::operator--()//前置--, 如--i
{
*this = *this - 1; return *this;
} //两个时间相加(this是指向Time类的指针)
CTime CTime::operator+=(CTime &c)
{
*this = *this + c; return *this;
} //两个时间相减(this是指向Time类的指针)
CTime CTime::operator-=(CTime &c)
{
*this = *this - c; return *this;
} //添加s秒
CTime CTime::operator+=(int s)
{
//将时间化成秒数
int sec1 = hour * 3600 + minute * 60 + second; //计算添加s秒后的时间
int sec2 = sec1 + s; CTime t1; t1.hour = sec2 / 3600;//计算时间(时)
t1.minute = sec2 % 3600 / 60;//计算时间(分)
t1.second = sec2 % 3600 % 60;//计算时间(秒) return t1;
} //降低s秒
CTime CTime::operator-=(int s)
{
//将时间化成秒数
int sec1 = hour * 3600 + minute * 60 + second; //计算添加s秒后的时间
int sec2 = sec1 - s; CTime t1; t1.hour = sec2 / 3600;//计算时间(时)
t1.minute = sec2 % 3600 / 60;//计算时间(分)
t1.second = sec2 % 3600 % 60;//计算时间(秒) return t1;
} void main()
{
//定义三个时间对象
CTime t1, t2, t; cout<<"请输入第一个时间:";
cin>>t1; cout<<"请输入第二个时间:";
cin>>t2; //显示第一个时间
cout<<"t1 = ";
cout<<t1; //显示第二个时间
cout<<"t2 = ";
cout<<t2; cout<<"\n以下比較两个时间大小:\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; //两个时间相加
cout<<"t1 + t2 = ";
t = t1 + t2;
cout<<t; //两个时间相减
cout<<"t1 - t2 = ";
t = t1 - t2;
cout<<t; //计算t1添加300秒后的时间
cout<<"t1 + 300秒 = ";
t = t1 + 300;
cout<<t; //计算t1降低300秒后的时间
cout<<"t1 - 300秒 = ";
t = t1 - 300;
cout<<t;
cout<<endl; cout<<"t1 = ";
cout<<t1; //计算t1++
cout<<"t1++ = ";
t = t1++;
cout<<t;
cout<<endl; cout<<"t1 = ";
cout<<t1; //计算++t1
cout<<"++t1 = ";
t = ++t1;
cout<<t;
cout<<endl; cout<<"t1 = ";
cout<<t1; //计算t1--
cout<<"t1-- = ";
t = t1--;
cout<<t;
cout<<endl; cout<<"t1 = ";
cout<<t1; //计算--t1
cout<<"--t1 = ";
t = --t1;
cout<<t;
cout<<endl; cout<<"t2 = ";
cout<<t2; cout<<"t = ";
cout<<t; t2 += t; cout<<"运行 t2 += t1 后 t2 = ";
cout<<t2;
cout<<endl; cout<<"t2 = ";
cout<<t2; cout<<"t = ";
cout<<t; t2 -= t; cout<<"运行 t2 -= t1 后 t2 = ";
cout<<t2;
cout<<endl; system("pause");
}

运行结果:

在CTime类中重载&lt;&lt;和&gt;&gt;的更多相关文章

  1. VC++ error C2248: “CObject::CObject”: 无法访问 private 成员(在“CObject”类中声明)

    在使用诸如:CArray或是 CList等类时,经常会出现此错误 此错误的原因是由于自定义的类的数组项时 有一个操作如  Add()  在这个操作中,实际上需要一个 = 操作,但是这个 =操作在 自定 ...

  2. C++中重载操作符[ ]

    1.首先说说为什么要重载操作符[ ] 主要是因为系统只给了整数类型(int)的重载函数,即只能在方括号中输入整数进行查找,但有时候我们可能存放数据时,下标的类型是自定义的,希望也能像数组直接通过下标访 ...

  3. c++类运算符重载遇到的函数形参问题

    class A { public: A(int arg1, int arg2); ~A(); A &operator = ( A &other); A operator + ( A & ...

  4. C++走向远洋——50(Time类中的运算符重载、一目,二目比较运算符、二目赋值运算符、二目加减法运算符)

    */ * Copyright (c) 2016,烟台大学计算机与控制工程学院 * All rights reserved. * 文件名:text.cpp * 作者:常轩 * 微信公众号:Worldhe ...

  5. java中重载一定在一个类里面吗?

    虽然这些概念在翻译成中文的过程中,有很多不同的翻译方式但本质上只有两种说法,就是Override和Overload其中,Overload一般都被翻译成重载而Override的翻译就乱七八糟了,所谓覆盖 ...

  6. delphi 中record 的类操作符重载简介

    今天简单介绍一下 delphi 中record 的类操作符重载使用,就是如何 实现 record 之间的简单操作. 关于类操作符重载 ,大家可以看官方的文档. Delphi allows certai ...

  7. C++解析(16):友元与类中的函数重载

    0.目录 1.友元的尴尬能力 2.类中的函数重载 3.小结 1.友元的尴尬能力 什么是友元? 友元是C++中的一种关系 友元关系发生在函数与类之间或者类与类之间 友元关系是单项的,不能传递 友元的用法 ...

  8. Kotlin操作符重载:把标准操作加入到任何类中(KAD 17)

    作者:Antonio Leiva 时间:Mar 21, 2017 原文链接:https://antonioleiva.com/operator-overload-kotlin/ 就像其他每种语言一样, ...

  9. C++类中的一些细节(重载、重写、覆盖、隐藏,构造函数、析构函数、拷贝构造函数、赋值函数在继承时的一些问题)

    1 函数的重载.重写(重定义).函数覆盖及隐藏 其实函数重载与函数重写.函数覆盖和函数隐藏不是一个层面上的概念.前者是同一个类内,或者同一个函数作用域内,同名不同参数列表的函数之间的关系.而后三者是基 ...

随机推荐

  1. visual studio 2013 使用IIS Express附加调试MVC5

    1.如何找到调试的站点的进程[由于图片无法上传,就不上传图片了] 2.vs运行的时候,在状态栏会存在一个IIS Express 进程,点击显示所有的应用程序,找到想要调试的程序的PID; 3.附加调试 ...

  2. JetBrains PhpStorm 使用

    · 在左侧显示当前文件位置 在左侧显示当前文件位置 alt + F1 在选择第1个在文件夹显示文件 在文件标签上 ctrl + 鼠标左键 或者 alt + F1 在选择第8个显示当前文件的函数,变量 ...

  3. 从PHP程序员到RAW开发~

    RAW是一款PHP网站开发系统,即使不懂PHP,也可以使用RAW进行PHP程序开发,当然如果已经掌握了PHP,那么使用RAW开发将会是如虎添翼! 怎么理解“如虎添翼”:我们平时要做一个项目的话,我们要 ...

  4. ssh无密登录

    ssh登录一般两种方式: 1.密码登录 2.密钥验证无需密码 使用方式:1.生成密钥 2.将公钥追加到authorized_keys中,需要注意的是执行权限需为600,这里因而第一次添加使用的是> ...

  5. mybatis+spring+c3p0+maven+ehcache

    项目截图 pom.xml如下 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http: ...

  6. TortoiseSVN是windows平台下Subversion的免费开源客户端。

    一般我们都是先讲讲服务器的配置,然后再讲客户端的使用,但是在TortoiseSVN上,却可以反过来.因为,如果你的要求不高,只是想在本机,或者是可信任的局域网络中使用SVN版本控制,可以不需要安装SV ...

  7. 射频识别技术漫谈(14)——Mifare S50与S70的存取控制

    存取控制指符合什么条件才能对卡片进行操作. S50和S70的块分为数据块和控制块,对数据块的操作有“读”.“写”.“加值”.“减值(含传输和存储)”四种,对控制块的操作只有“读”和“写”两种. S50 ...

  8. C#获取桌面壁纸图片的路径(Desktop Wallpaper)

    原文 C#获取桌面壁纸图片的路径(Desktop Wallpaper) 利用 Windows 的 API 获取桌面壁纸的实际路径,使用的是 SystemParametersInfo 这个API,此AP ...

  9. 带你轻松玩转Git--图解三区结构

    在上篇文章的结尾我们提到了Git 的三区结构,在版本控制体系中有这样两种体系结构,一种是两区结构一种是三区结构.接下来我们通过对Git三区的结构学习来帮助我们更好的去理解并运用Git. 两区结构是其他 ...

  10. Python学习入门基础教程(learning Python)--2.2 Python下的变量基础

    变量的基本概念,变量可以这样去理解,变量是一个值,这个值存储在计算机的内存里.以网购为例,您在选购傻商品的时候,是在不同页面里选不同的商品,选好一件点击“放入购物车”,选完了再点击去结帐,这些商品的价 ...