Write the code using C++ ( from the TEXTBOOK Data Structure using C++)
Add a function to the dateType class.
The function's name is compareDates.
Its prototype is
int compareDates(const dateType& otherDate;
The function returns -1 if otherDate is greater than than this date
The function returns 0 if otherDate equals this date
The function returns 1 if otherDate is less than this date
Examples
dateType d1(1, 1, 2019);
dateType d2(11,1, 2019)
d1.compareDates(d2) returns -1
d2.compareDates(d1) returns 1
d2.compareDates(d2) returns 0
int compareDates(const dateType& otherDate) {
if (this->day == otherDate.day && this->month == otherDate.month && this->year == otherDate.year) {
return 0;
}
if (this->year > otherDate.year) {
return 1;
} else if (this->year < otherDate.year) {
return -1;
} else {
if (this->month > otherDate.month) {
return 1;
} else if (this->month < otherDate.month) {
return -1;
} else {
if (this->day > otherDate.day) {
return 1;
} else if (this->day < otherDate.day) {
return -1;
} else {
return 0;
}
}
}
}
Get Answers For Free
Most questions answered within 1 hours.