C++练习继承类

#include 
using namespace std;
class Time{
public:
    Time(){}
    Time(int h,int m,int s)
    {
     hour=h;
     minute=m;
     second=s;
    }
    friend istream & operator>>(istream &,Time &);
    friend ostream & operator<<(ostream &,Time &);
    void operator =(Time &);
    friend bool operator >(Time &t1,Time &t2);
    friend bool operator <(Time &t1,Time &t2);
    friend bool operator ==(Time &t1,Time &t2);
    friend void compare(Time &,Time &);
    void display()
    {
     cout<<hour<<":"<<minute<<":"<<second<int hour;
    int minute;
    int second;
};
istream & operator>>(istream &input,Time &t1)
{
 input>>t1.hour>>t1.minute>>t1.second;
 return input;
}
ostream & operator<<(ostream &output,Time &t1)
{
 output<hour<<":"<minute<<":"<second;
 return output;
}
void Time::operator =(Time & t1)
{
  hour=t1.hour;
  minute=t1.minute;
  second=t1.second;
}
bool operator >(Time &t1,Time &t2)
{
 if(t1.hour>t2.hour)
     return true;
 else if(t1.hour==t2.hour&&t1.minute>t2.minute)
     return true;
 else if(t1.hour==t2.hour&&t1.minute>t2.minute&&t1.second>t2.second)
     return true;
 else
     return false;
}
bool operator <(Time &t1,Time &t2)
{
 if(!operator >(t1,t2))
     return true;
 else
     return false;
}
bool operator ==(Time &t1,Time &t2)
{
 if(t1.hour==t2.hour&&t1.minute==t2.minute&&t1.second==t2.second)
     return true;
 else
     return false;
}
void compare(Time &t1,Time &t2)
{
 if(operator>(t1,t2))
     cout<<"t1>t2";
 if(operator<(t1,t2))
     cout<<"t1;
 if(operator==(t1,t2))
     cout<<"t1=t2";
}
class Date{
public:
    Date(){}
    Date(int y,int mo,int d)
    {
     year=y;
     month=mo;
     day=d;
    }
    void get_value()
    {
     cin>>year>>month>>day;
    }
    void show()
    {
     cout<<year<<"-"<<month<<"-"<<day<int year;
    int month;
    int day;
};
class DateTime:public Date,public Time
{
public: 
    DateTime(){}
    DateTime(int y,int mo,int d,int h,int m,int s):Date(y,mo,d),Time(h,m,s){}
    DateTime operator -(DateTime &c2);
    DateTime operator +(Time &t1);
    DateTime operator -(Time &t1);
    void show()
    {
     cout<<year<<"-"<<month<<"-"<<day<<" "<<hour<<":"<<minute<<":"<<second<year=year-c2.year;
 c3.month=month-c2.month;
 c3.day=day-c2.day;
 c3.hour=hour-c2.hour;
 c3.minute=minute-c2.minute;
 c3.second=second-c2.second;
 return c3;
}
DateTime DateTime::operator +(Time &t1)
{
 DateTime c3;
 c3.year=year;
 c3.month=month;
 c3.day=day;
 c3.hour=hour+t1.hour;
 c3.minute=minute+t1.minute;
 c3.second=second+t1.second;
 return c3;
}
DateTime DateTime::operator -(Time &t1)
{
 DateTime c3;
 c3.year=year;
 c3.month=month;
 c3.day=day;
 c3.hour=hour-t1.hour;
 c3.minute=minute-t1.minute;
 c3.second=second-t1.second;
 return c3;
}
int main()
{
 Time t1(12,20,30),t2(10,21,25);
 cout<<" t1="<" t2="<Time t3,t4;
 cout<<" t3=:";
 cin>>t3;
 cout<<" t3="<"t1=t4"<" t4=";
 t4.display();
 cout<<"比较时间t1和时间t2:";
 compare(t1,t2);
 Date d1;
 cout<" d1:";
 d1.get_value();
 cout<<" d1=";
 d1.display();
 DateTime c1(1996,10,12,12,20,14),c2(1994,4,12,10,1,2);
 DateTime c3,c4,c5;
 cout<<" c3=c 1-c2:"<" c3=";
 c3.display();
 cout<<" c4=c1+ t1:"<" c4=";
 c4.display();
 cout<<" c5= c1- t1:"<" c5=";
 c5.display();
 return 0;
}


你可能感兴趣的:(C++练习继承类)