设计一个日期类Date,,要求: (1)包含年(year)、月(month)和日(day)私有数据成员。 (2)包含构造函数,重载关于一日期加上天数的加法运算符+、重载关于一日期减去天数的减加运算符-

来源:互联网 发布:黑客帝国台词 知乎 编辑:程序博客网 时间:2024/06/06 02:27
#include<iostream.h>
class Date{private:double year,month,day;public:Date(double y=0,double m=0,double d=0):year(y),month(m),day(d){};Date operator+(Date b);                         //重载运算符+Date operator-(Date b);                         //重载运算符-friend ostream &operator<<(ostream &os,Date &s);//重载流运算符<<friend istream &operator>>(istream &is,Date &s);//重载流运算符>>void Show();};Date Date::operator+(Date b){    if((day+b.day)>31){   month++;   int dd=day+b.day-31;return Date(year,month,dd);   }elsereturn Date(year,month,day+b.day);}Date Date::operator-(Date b){if(day-b.day<=0){month--;int dd=day-b.day+30;return Date(year,month,dd);}elsereturn Date(year,month,day-b.day);}void Date::Show(){cout<<year<<"-"<<month<<"-"<<day<<endl;}ostream& operator<<(ostream &os,Date &s){os<<s.year<<"\t";os<<s.month<<"\t";os<<s.day<<"\t"<<endl;return os;}istream &operator>>(istream &is,Date &s){cout<<"按顺序输入年 月 日"<<endl;is>>s.year;is>>s.month;is>>s.day;cout<<endl;return is;}void main(){Date t1(2013,10,11),t2(0,0,25),t3,t4;t1.Show();t3=t1+t2;t4=t1-t2;t3.Show();t4.Show();cin>>t1;cout<<t1;}

0 0