객체 선행++, 선행 --후면++, 후면 -- (및 연산자를 다시 로드하지 않음)

6291 단어
/*           
*All right reserved.
*    *           
*All right reserved.
*    :    ++,  --;  ++,  --
*  :  
*    :2014 4 11 
*   :v1.0
*             :   :    ++,  --;  ++,  --
*    :*/
#include <iostream>
using namespace std;
class CTime
{
private:
    int hour;    //  
    int minute;  //  
    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);
    //           
    //  t    、 、     , t1(8,20,25),t2(11,20,50),t1+t2 :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     
    CTime operator++(int);//  ++
    CTime operator++();//  ++
    CTime operator--(int);//  --
    CTime operator--();//  --
};
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;
}
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 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(*this<t)
    return false;
    return true;
}
 bool CTime::operator <= (CTime &t)
 {
     if(*this>t)
     return false;
    return true;

 }
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(*this==t)
     return false;
    return true;
}
CTime CTime::operator+(CTime &t)
{
     int h,m,s;
    s=second+t.second;
    m=minute+t.minute;
   h=hour+t.hour;
    if (s>59)
    {
        s-=60;
        m++;
   }
    if (m>59)
    {
        m-=60;
       h++;
    }
    if (h>23) h-=24;
    CTime t3(h,m,s);
    return t3;


}
CTime CTime::operator-(CTime &t)
{

    int h,m,s;
    s=second-t.second;
    m=minute-t.minute;
    h=hour-t.hour;
    if (s<0)
    {
        s+=60;
        m--;
   }
   if (m<0)
    {
       m+=60;
        h--;
   }
    if (h<0) h+=24;
    CTime t3(h,m,s);
    return t3;

}
CTime CTime::operator+(int s)
{
    int s1=s%60;
    int m1=s/60;
    int h1=s/3600;
    CTime t3(h1,m1,s1);
    return *this+t3;
}
CTime CTime::operator-(int s)
{
    int  s1,m1,h1;
    s1=s%60;
    m1=s/60;
    h1=s/3600;
    CTime t3(h1,m1,s1);
    return *this-t3;
}
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;
}
CTime CTime::operator++(int)
{
    CTime temp(*this);
       second++;
    if(second>59)
    {
        second-=60;
        minute++;
    }
    if(minute>59)
    {
        minute-=60;
        hour++;
    }
    if(hour>23)
    {
        hour-=24;
    }
   return  temp;
}
CTime CTime::operator++()
{
      ++second;
    if(second>59)
    {
        second-=60;
        minute++;
    }
    if(minute>59)
    {
        minute-=60;
        hour++;
    }
    if(hour>23)
    {
        hour-=24;
    }
    return *this;
}
CTime CTime::operator--(int)
{
    CTime temp(*this);
     second--;
     if (second<0)
    {
        second+=60;
        minute--;
   }
    if (minute<0)
    {
       minute+=60;
        hour--;
    }
    if (hour<0) hour+=24;
    return temp;
}
CTime CTime::operator--()
{
    --second;
    if (second<0)
    {
        second+=60;
        minute--;
   }
    if (minute<0)
    {
       minute+=60;
        hour--;
    }
    if (hour<0) hour+=24;
    return *this;
}
int main()
{
   CTime t1(10,20,25),t2(6,20,45),t;
   cout<<"t1 :";
   t1.display();
   cout<<"t2 :";
   t2.display();
   cout<<"          :
"; 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; t=t1+t2; cout<<" ;"<<endl; t.display(); t=t1-t2; cout<<" ;"<<endl; t.display(); cout<<"+600 ;"<<endl; t=t1+600; t.display(); cout<<"-400 ;"<<endl; t=t1-400; t.display(); cout<<" "<<endl; t1+=t2; t1.display(); cout<<" "<<endl; t1-=t2; t1.display(); cout<<" 600 "<<endl; t1+=600; t1.display(); cout<<" 600 "<<endl; t1-=600; t1.display(); cout<<" ++ :"<<endl; ++t1; t1.display(); cout<<" -- :"<<endl; --t1; t1.display(); cout<<" ++ :"<<endl; t1++; t1.display(); cout<<" -- :"<<endl; t1--; t1.display(); return 0; }

실행 결과:
체험

좋은 웹페이지 즐겨찾기