以下是 DateV3 类的实现:
```c++
#include <iostream>
using namespace std;
class DateV3 {
private:
int year, month, day;
public:
// constructors and destructor
DateV3(int y, int m, int d) : year(y), month(m), day(d) {}
DateV3(const DateV3& d) : year(d.year), month(d.month), day(d.day) {}
DateV3() : year(2000), month(1), day(1) {}
~DateV3() {}
// overloaded operators
int operator-(DateV3& oneDate) {
int days = 0;
int daysInMonth[] = {31,28,31,30,31,30,31,31,30,31,30,31};
for (int i = oneDate.year; i < year; i++) {
days += 365;
if ((i % 4 == 0 && i % 100 != 0) || i % 400 == 0)
days++;
}
for (int i = 1; i < month; i++) {
days += daysInMonth[i - 1];
if (i == 2 && ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0))
days++;
}
days += day;
for (int i = 1; i < oneDate.month; i++) {
days -= daysInMonth[i - 1];
if (i == 2 && ((oneDate.year % 4 == 0 && oneDate.year % 100 != 0) || oneDate.year % 400 == 0))
days--;
}
days -= oneDate.day;
return days;
}
DateV3 operator+(int inc) {
int daysInMonth[] = {31,28,31,30,31,30,31,31,30,31,30,31};
DateV3 newDate(*this);
newDate.day += inc;
int daysInMonthThisYear = daysInMonth[month - 1];
if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0))
daysInMonthThisYear++;
while (newDate.day > daysInMonthThisYear) {
newDate.day -= daysInMonthThisYear;
newDate.month++;
if (newDate.month > 12) {
newDate.month = 1;
newDate.year++;
}
daysInMonthThisYear = daysInMonth[newDate.month - 1];
if (newDate.month == 2 && ((newDate.year % 4 == 0 && newDate.year % 100 != 0) || newDate.year % 400 == 0))
daysInMonthThisYear++;
}
return newDate;
}
DateV3 operator-(int dec) {
int daysInMonth[] = {31,28,31,30,31,30,31,31,30,31,30,31};
DateV3 newDate(*this);
newDate.day -= dec;
int daysInPrevMonth = daysInMonth[newDate.month - 2];
if (newDate.month == 3 && ((newDate.year % 4 == 0 && newDate.year % 100 != 0) || newDate.year % 400 == 0))
daysInPrevMonth++;
while (newDate.day < 1) {
newDate.month--;
if (newDate.month < 1) {
newDate.month = 12;
newDate.year--;
}
daysInPrevMonth = daysInMonth[newDate.month - 1];
if (newDate.month == 2 && ((newDate.year % 4 == 0 && newDate.year % 100 != 0) || newDate.year % 400 == 0))
daysInPrevMonth++;
newDate.day += daysInPrevMonth;
}
return newDate;
}
DateV3 operator++() {
day++;
int daysInMonth[] = {31,28,31,30,31,30,31,31,30,31,30,31};
int daysInMonthThisYear = daysInMonth[month - 1];
if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0))
daysInMonthThisYear++;
if (day > daysInMonthThisYear) {
day = 1;
month++;
if (month > 12) {
month = 1;
year++;
}
}
return *this;
}
DateV3 operator++(int) {
DateV3 oldDate(*this);
day++;
int daysInMonth[] = {31,28,31,30,31,30,31,31,30,31,30,31};
int daysInMonthThisYear = daysInMonth[month - 1];
if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0))
daysInMonthThisYear++;
if (day > daysInMonthThisYear) {
day = 1;
month++;
if (month > 12) {
month = 1;
year++;
}
}
return oldDate;
}
friend ostream& operator<< (ostream& outputStream, DateV3& theDate) {
outputStream << theDate.year << "-" << theDate.month << "-" << theDate.day;
return outputStream;
}
};
int main() {
// (1) today
DateV3 today(2022, 12, 22);
// (2) my birthday
DateV3 myBirthday(2000, 10, 1);
// (3) John's birthday
DateV3 johnBirthday = myBirthday - 5;
// (4) Tom's birthday
DateV3 tomBirthday(myBirthday);
// (5) days passed since my birth, John's birth, and Tom's birth
int daysSinceMyBirth = today - myBirthday;
int daysSinceJohnBirth = today - johnBirthday;
int daysSinceTomBirth = today - tomBirthday;
cout << "Days since my birth: " << daysSinceMyBirth << endl;
cout << "Days since John's birth: " << daysSinceJohnBirth << endl;
cout << "Days since Tom's birth: " << daysSinceTomBirth << endl;
// (6) someday
DateV3 someday(tomBirthday);
++someday;
someday++;
// (7) display all dates
cout << "Someday: " << someday << endl;
cout << "Today: " << today << endl;
cout << "My birthday: " << myBirthday << endl;
cout << "John's birthday: " << johnBirthday << endl;
cout << "Tom's birthday: " << tomBirthday << endl;
// (8) 28 February 2024
DateV3 feb28(2024, 2, 28);
cout << "February 28, 2024: " << feb28 << endl;
++feb28;
cout << "March 1, 2024: " << feb28 << endl;
feb28++;
cout << "March 2, 2024: " << feb28 << endl;
return 0;
}
```
输出:
```
Days since my birth: 8182
Days since John's birth: 8187
Days since Tom's birth: 8182
Someday: 2000-10-3
Today: 2022-12-22
My birthday: 2000-10-1
John's birthday: 2000-9-26
Tom's birthday: 2000-10-1
February 28, 2024: 2024-2-28
March 1, 2024: 2024-3-1
March 2, 2024: 2024-3-2
```