Write a C++ program that defines a class DateV3 with the following: (1) private member variables: int year, month, day; (2) Has three constructors and one destructor as follows: The first constructor takes three parameters, int y, int m, int n; The second is the copy constructor that takes a DateV3 object as the parameter; The third is the default constructor that takes no parameter; The destructor takes no parameter. (3) Has overloaded operators: int operator-(DateV3 & oneDate); // return difference in days between the calling object and oneDate DateV3 operator+(int inc); // return a Date object that is inc days later than the calling object DateV3 operator-(int dec); // return a Date object that is dec days earlier than the calling object DateV3 operator++(); // overload the prefix ++ operator DateV3 operator++(int); // overload the postfix ++ operator friend ostream& operator<< (ostream& outputStream, DateV3& theDate); // overload the << operator Test class DateV3 in the main function as follows: (1) Declare and initialize an object to represent today, which should be the date that you work on this assignment. (2) Declare and initialize an object to represent your OWN birthday. (3) Express John’s birthday given John is 5 days older than yours. (4) Create Tom’s birthday by using the copy constructor, assuming Tom has the same birthday as you. (5) Display how many days have passed since your birth, John’s birth, and Tom’s birth, respectively. (6) Create an DateV3 object, someday, by cloning Tom’s birthday. Increment someday by the prefix operator ++ once, and by postfix operator ++ once. (7) Display someday, today, your birthday, John’s birthday, and Tom’s birthday. (8) Declare a DateV3 object to represent 28 February 2024, display it, apply the prefix ++ operator on it, display it again, and apply the postfix ++ operator on it and display it again. Hint: i) A good idea is to first design a function to compute the number of days that has passed since Year 1, Month 1, and Day 1, and then to use this function to compute the difference between two dates. ii) You can store the number of days for each of the 12 months in an integer array, which helps in counting the days and implementing the overloaded operators.
时间: 2024-02-14 10:16:48 浏览: 68
以下是 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
```
阅读全文