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<
}
/*
* 程序的版权和版本声明部分
* Copyright (c)2012, 烟台大学计算机学院学生
* All rightsreserved.
* 文件名称: object.cpp
* 作者:隋 鑫
* 完成日期: 2013年 4 月 19 日
* 版本号: v1.0
* 输入描述:无
* 问题描述:
* 程序输出:
*/
#include
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:15
CTime 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;
else
return false;
}
bool CTime::operator < (CTime &t)
{
else if(hour==t.hour&&minute
else if(minute==t.minute&&second
else
return 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;
else
return 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;
else
return false;
}
bool CTime::operator == (CTime &t)
{
if(hour==t.hour&&minute==t.minute&&second==t.second)
return true;
else
return 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;
else
return 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.m