本文主要是介绍在CTime类中重载和,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
程序代码:
#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:15CTime 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;//计算++t1cout<<"++t1 = ";t = ++t1;cout<<t;cout<<endl;cout<<"t1 = ";cout<<t1;//计算t1--cout<<"t1-- = ";t = t1--;cout<<t;cout<<endl;cout<<"t1 = ";cout<<t1;//计算--t1cout<<"--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类中重载和的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!