Reputation: 11
I'm trying to do a code, but every time I ran it, it uses the wrong operator:
class Date {
private:
int m_year;
int m_mon;
int m_day;
int m_ErrorCode;
int m_CUR_YEAR;
int daysSince0001_1_1()const; // returns number of days passed since the date 0001/1/1
bool validate(); /* validates the date setting the error code and then returning the result
true, if valid, and false if invalid.*/
void errCode(int); // sets the error code
int systemYear()const; // returns the current system year
bool bad()const; // return true if
int mdays()const; // returns the number of days in current month
void setToToday(); // sets the date to the current date (system date)
public:
Date(); // creates a date with current date
Date(int year, int mon, int day); /* create a date with assigned values
then validates the date and sets the
error code accordingly */
int errCode()const; // returns the error code or zero if date is valid
const char* dateStatus()const; // returns a string corresponding the current status of the date
int currentYear()const; // returns the m_CUR_YEAR value;
bool operator==(Date& date) const;
bool operator!=(Date& date) const;
bool operator>=(Date& date) const;
bool operator<=(Date& date) const;
bool operator<(Date& date) const;
bool operator>(Date& date) const;
int operator-(Date& date) const;
operator bool() const;
std::istream& read(std::istream& is = std::cin);
std::ostream& write(std::ostream& os = std::cout)const;
};
This is my class module
bool Date::bad()const {
return m_ErrorCode != 0;
}
bool Date::operator==(Date& date) const {
return daysSince0001_1_1() == date.daysSince0001_1_1();
}
bool Date::operator!=(Date& date) const {
return daysSince0001_1_1() != date.daysSince0001_1_1();
}
bool Date::operator>=(Date& date) const {
return daysSince0001_1_1() >= date.daysSince0001_1_1();
}
bool Date::operator<=(Date& date) const {
return daysSince0001_1_1() <= date.daysSince0001_1_1();
}
bool Date::operator<(Date& date) const {
return daysSince0001_1_1() < date.daysSince0001_1_1();
}
bool Date::operator>(Date& date) const {
return daysSince0001_1_1() > date.daysSince0001_1_1();
}
int Date::operator-(Date& date) const {
return daysSince0001_1_1() - date.daysSince0001_1_1();
}
Date::operator bool() const {
return !bad();
}
And this is part of my implementations. However, if I do this:
if (B > A) {
cout << B << " > " << A << endl;
}
else {
cout << B << " <= " << A << endl;
}
if (B < A) {
cout << B << " < " << A << endl;
}
else {
cout << B << " >= " << A << endl;
}
if (B <= A) {
cout << B << " <= " << A << endl;
}
else {
cout << B << " > " << A << endl;
}
if (B >= A) {
cout << B << " >= " << A << endl;
}
else {
cout << B << " < " << A << endl;
}
if (B == A) {
cout << B << " == " << A << endl;
}
else {
cout << B << " != " << A << endl;
}
if (B != A) {
cout << B << " != " << A << endl;
}
else {
cout << B << " == " << A << endl;
}
cout << "Days between the two dates: " << B - A << endl;
It uses the operator bool() const instead of the right function
I already tried using chatgpt and reading about the overload methods, but it didn't change
Upvotes: 0
Views: 69