Answer to Question #323883 in C++ for khan

Question #323883

You are supposed to construct a class Date that shall contains the following private members



such as:



 Day: integer type data.



 Month: integer type data.



 Year: integer type data.



You are supposed to perform operators overloading on the following operators



 > : greater than operator.



 < : less than operator.



 == : equal operator.

1
Expert's answer
2022-04-05T09:30:13-0400
#include <iostream>
using namespace std;


class Date {
    int day;
    int month;
    int year;
    bool is_leap_year();


public:
    Date(int d, int m, int y);
    void print(ostream& os) const;
    bool operator>(const Date& rhs) const;
    bool operator<(const Date& rhs) const;
    bool operator==(const Date& rhs) const;
};


bool Date::is_leap_year() {
    return (year%4 == 0) && (year%100 != 0 || year%400 == 0); 
}


Date::Date(int d, int m, int y) {
    static int days[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31,
                          30, 31, 30, 31};
    day = month = year = 0;
    if (m<=0 || m > 12) {
        return;
    }
    int dd = days[m] + ((m == 2) && is_leap_year() ? 1 : 0);
    if (d<=0 || d > dd) {
        return;
    }
    day = d;
    month = m;
    year = y;
}


void Date::print(ostream& os) const {
    os << day << "/" << month << "/" << year;
}


bool Date::operator>(const Date& rhs) const {
    if (year > rhs.year) {
        return true;
    }
    else if (year < rhs.year) {
        return false;
    }
    if (month > rhs.month) {
        return true;
    }
    else if (month < rhs.month) {
        return false;
    }
    if (day > rhs.day) {
        return true;
    }
    return false;
}


bool Date::operator<(const Date& rhs) const {
    return rhs > *this;
}


bool Date::operator==(const Date& rhs) const {
    return year == rhs.year && month == rhs.month && day == rhs.day;
}


ostream& operator<<(ostream& os, const Date& d) {
    d.print(os);
    return os;
}


int main() {
    int d, m, y;
    
    cout << "Enter the first date (day, month, year): ";
    cin >> d >> m >> y;
    Date date1(d, m, y);


    cout << "Enter the second date (day, month, year): ";
    cin >> d >> m >> y;
    Date date2(d, m, y);


    if (date1 > date2) {
        cout << date1 << " is greater than " << date2 << endl;
    }
    else {
        cout << date1 << " is not greater than " << date2 << endl;
    }


    if (date1 < date2) {
        cout << date1 << " is less than " << date2 << endl;
    }
    else {
        cout << date1 << " is not less than " << date2 << endl;
    }

    if (date1 == date2) {
        cout << date1 << " is equal " << date2 << endl;
    }
    else {
        cout << date1 << " is not equal " << date2 << endl;
    }

    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS