/*定义一个满下要求的Dat足如e类: (1)用日 / 月 / 年的格式输出日期; (2)可运行在日期上加一天操作; (3)设置日期操作。*/ #include<iostream> using namespace std; class Date { private: int day; int month; int year; public: int addday(int day) { return day = day + 1; } int setday(int day, int month, int year) { day = day; month = month; year = year; } };
//----------------------------------- //Date.h //----------------------------------- #include<iostream> using namespace std; //----------------------------------- class Date{ int year, month, day; bool isLeapYear(){ return (year%400==0)| |(year%100!=0)&&(year%4==0);} public: frinend ostream& operator<<(ostream& out, const Date& d){ return out<<d.day<<"/"<<d.month<<"/"<<d.year<<"\n"; } Date& operator++(){ bool mEnd,yEnd=0; switch(month){ case 12: yEnd = day==31 case 10: case 8: case 7: case 5: case 3: case 1: mEnd = day==31; break case 2:mEnd = day==28+isLeapYear(); break; case 11: case 9: case 6: case 4: mEnd = day==30; break; } yEnd ? (day=1,month=1,year++) : (mEnd ? day=1, month++ : day++); return *this; } void set(int y, int m, int d){ year=y, month=m, day=d; } };//=================================