时间类--运算符重载函数--gyy

来源:互联网 发布:电子商务软件提供商 编辑:程序博客网 时间:2024/04/29 22:16
/*定义一个时间类Time,其成员如下:有3个私有数据成员hour、min、sec,分别表示时间类中的时、分、秒公有成员函数声明如下,函数体自己填写:Time(int h, int m, int s);//带3个参数值的构造函数,注意时间是有规则的,注意在传递参数时如果遇到非法时间,则将hour、min、sec值都设为0 或要求重新输入~Time();//析构函数void  setTime(int h,int m,int s); //重新设置时间void  showTime(); //显示时间void  timeAdd(int h, int m ,int s);//进行时间相加,参数为时、分、秒void  timeAdd(int s); //进行时间相加,参数为秒利用运算符重载函数作为友元函数来完成相应类对象的操作friend Time operator+(Time t1,Time t2);   friend Time operator+(Time t1,int s);friend bool operator>(Time t1,Time t2);friend bool operator<(Time t1,Time t2);  friend bool operator==(Time t1,Time t2); friend bool operator!=(Time t1,Time t2); friend ostream & operator<<(ostream & output,Time & t);friend istream & operator>>(istream & input,Time & t);利用运算符重载函数作为成员函数来完成相应类对象的操作Time operator++();  Time operator++(int);在主函数中建立Time对象,通过对象调用成员函数来完成相应操作。*/#include <iostream.h>class Time{  public:   Time(int h=0, int m=0, int s=0); //带3个参数值的构造函数,注意时间是有规则的,注意在传递参数时如果遇到非法时间,则将hour、min、sec值都设为0 或要求重新输入   ~Time();//析构函数   void  setTime(int h,int m,int s); //重新设置时间   void  showTime(); //显示时间   void  timeAdd(int h, int m ,int s); //进行时间相加,参数为时、分、秒   void  timeAdd(int s); //进行时间相加,参数为秒    Time operator++();   //对++进行运算符重载,作为对象的前置运算符   Time operator++(int);  //对++进行运算符重载,作为对象的后置运算符   friend Time operator+(Time t1,Time t2);  //对+进行运算符重载,完成对两个Time类对象的相加操作   friend Time operator+(Time t1,int s);  //对+进行运算符重载,完成对1个Time类对象和整数的相加操作   friend ostream & operator<<(ostream & output,Time & t); //对<<进行运算符重载,完成对Time类对象的输出操作   friend istream & operator>>(istream & input,Time & t);  //对>>进行运算符重载,完成对Time类对象的输入操作   friend bool operator>(Time t1,Time t2); //对>进行运算符重载,完成对两个Time类对象的大于比较操作   friend bool operator<(Time t1,Time t2);  //对<进行运算符重载,完成对两个Time类对象的小于比较操作   friend bool operator==(Time t1,Time t2);  //对==进行运算符重载,完成对两个Time类对象的等于比较操作   friend bool operator!=(Time t1,Time t2);  //对!=进行运算符重载,完成对两个Time类对象的不等于比较操作  private:    int hour; int min;int sec;};Time::Time(int h, int m, int s) //带3个参数值的构造函数,注意时间是有规则的,注意在传递参数时如果遇到非法时间,则将hour、min、sec值都设为0 或要求重新输入{hour=(h>=0&&h<24)?h:0;min=(m>=0&&m<60)?m:0;    sec=(s>=0&&s<60)?s:0;}Time:: ~Time()//析构函数{}void  Time::setTime(int h,int m,int s) //重新设置时间{  hour=(h>=0&&h<24)?h:0;min=(m>=0&&m<60)?m:0;    sec=(s>=0&&s<60)?s:0;}void  Time::showTime()  //显示时间{  cout<<hour<<":"<<min<<":"<<sec<<endl;}void  Time::timeAdd(int h, int m ,int s) //进行时间相加,参数为时、分、秒{if(h<0||h>=24||m<0||m>=60||s<0||s>=60){cout<<"时间参数有误,计算时间相加不成功"<<endl;}else{  cout<<hour<<":"<<min<<":"<<sec;  hour=(hour+h+(min+m+(sec+s)/60)/60)%24;  min=(min+m+(sec+s)/60)%60;  sec=(sec+s)%60;  cout<<"+"<<h<<":"<<m<<":"<<s<<"="<<hour<<":"<<min<<":"<<sec<<endl;}    }void  Time::timeAdd(int s) //进行时间相加,参数为秒 {      cout<<hour<<":"<<min<<":"<<sec;  hour=(hour+(min+(sec+s)/60)/60)%24;  min=(min+(sec+s)/60)%60;  sec=(sec+s)%60;  cout<<"+"<<s<<"="<<hour<<":"<<min<<":"<<sec<<endl;}Time Time::operator++()  //对++进行运算符重载,作为对象的前置运算符{    hour=(hour+(min+(sec+1)/60)/60)%24;   min=(min+(sec+1)/60)%60;   sec=(sec+1)%60;   return *this;  //返回改变后的对象}Time Time::operator++(int)  //对++进行运算符重载,作为对象的后置运算符{   Time temp=*this;   hour=(hour+(min+(sec+1)/60)/60)%24;   min=(min+(sec+1)/60)%60;   sec=(sec+1)%60;   return temp;  //返回改变前的对象} Time operator+(Time t1,Time t2)//对+进行运算符重载,完成对两个Time类对象的相加操作{  Time t3;  t3.hour=(t1.hour+t2.hour+(t1.min+t2.min+(t1.sec+t2.sec)/60)/60)%24;  t3.min=(t1.min+t2.min+(t1.sec+t2.sec)/60)%60;  t3.sec=(t1.sec+t2.sec)%60;  return t3;}  Time operator+(Time t1,int s) //对+进行运算符重载,完成对1个Time类对象和整数的相加操作{  Time t2;  t2.hour=(t1.hour+(t1.min+(t1.sec+s)/60)/60)%24;  t2.min=(t1.min+(t1.sec+s)/60)%60;  t2.sec=(t1.sec+s)%60;  return t2;}ostream & operator<<(ostream & output,Time & t) //对<<进行运算符重载,完成对Time类对象的输出操作{  output<<t.hour<<":"<<t.min<<":"<<t.sec<<endl;  return output;}istream & operator>>(istream & input,Time & t) //对>>进行运算符重载,完成对Time类对象的输入操作{  cout<<"input the hour :";  input>>t.hour;  cout<<"input the min :";  input>>t.min;  cout<<"input the sec:";  input>>t.sec;  return input;}  bool operator>(Time t1,Time t2) //对>进行运算符重载,完成对两个Time类对象的大于比较操作  {    if(t1.hour>t2.hour)return true;if(t1.hour==t2.hour&&t1.min>t2.min)return true;if(t1.hour==t2.hour&&t1.min==t2.min&&t1.sec>t2.sec)return true;return false;  }  bool operator<(Time t1,Time t2) //对<进行运算符重载,完成对两个Time类对象的小于比较操作  {    if(t1.hour<t2.hour)return true;if(t1.hour==t2.hour&&t1.min<t2.min)return true;if(t1.hour==t2.hour&&t1.min==t2.min&&t1.sec<t2.sec)return true;return false;  }  bool operator==(Time t1,Time t2) //对==进行运算符重载,完成对两个Time类对象的等于比较操作  {if(t1.hour==t2.hour&&t1.min==t2.min&&t1.sec==t2.sec)return true;elsereturn false;    }  bool operator!=(Time t1,Time t2) //对!=进行运算符重载,完成对两个Time类对象的不等于比较操作  {if(t1.hour==t2.hour&&t1.min==t2.min&&t1.sec==t2.sec)return false;elsereturn true;    }int main(){ Time t1(1,2,3);   //定义时间对象 cout<<"t1="; t1.showTime();    //显示时间信息 t1.timeAdd(2,3,4);   //进行分、时、秒的相加操作 cout<<"t1="; t1.showTime();    //显示时间信息 t1.timeAdd(22,59,4);  //进行分、时、秒的相加操作 cout<<"t1="; t1.showTime();   //显示时间信息 t1.timeAdd(155);  //进行秒的相加操作 cout<<"t1="; t1.showTime();   //显示时间信息   Time t2(25,62,-9); //定义时间对象 cout<<"t2="; t2.showTime(); //显示时间信息 cout<<endl;  t2=t1++;   //++运算符重载函数调用,对象后置运算符 cout<<"after  t2=t1++"<<endl; cout<<"t1="; t1.showTime(); cout<<"t2="; t2.showTime(); t2=++t1; //++运算符重载函数调用,对象前置运算符 cout<<"after  t2=++t1"<<endl; cout<<"t1="; t1.showTime(); cout<<"t2="; t2.showTime(); Time t3; t3=t1+t2;  //+运算符重载函数调用,完成两个对象相加 cout<<"after  t3=t1+t2"<<endl; cout<<"t3="; t3.showTime(); t3=t3+100; //+运算符重载函数调用,完成一个对象和秒的相加 cout<<"after  t3=t3+100"<<endl; cout<<"t3="; t3.showTime(); cout<<"input t1 again"<<endl; cin>>t1;          //>>运算符重载函数调用,完成对象输入 cout<<"t1="<<t1;  //<<运算符重载函数调用,完成对象输出 cout<<"input t2 again"<<endl; cin>>t2;         //>>运算符重载函数调用,完成对象输入 cout<<"t2="<<t2; //<<运算符重载函数调用,完成对象输出 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; return 0;}



原创粉丝点击