Computer Science Canada Relational operator overloading |
Author: | Imajica [ Sun Mar 21, 2004 7:46 am ] |
Post subject: | Relational operator overloading |
All of my overloaded relational operators return a "false" when tested w/ the instructors driver. My code appears to be valid. I have no idea what is causing this. #ifndef DATE_H #define DATE_H #include<iostream> using std::ostream; using std::istream; class Date { friend ostream &operator<<(ostream &, const Date &); friend istream &operator>>(istream &, Date &); public: Date(int m = 1, int d = 1, int y = 1); Date(const Date &); void setMonth(int); void setDay(int); void setDate(int, int, int ); void setYear(int); const int getMonth()const{return month;} const int getDay()const{return day;} const int getYear()const{return year;} int operator<(Date &); bool leapYear(int)const; void printText()const; private: int month, day, year; static const int days[]; bool lastDay(int); void increment(); void decrement(); }; #endif #include<iostream> #include"date.h" using std::endl; using std::cout; const int Date::days[] = {0,31,28,31,30,31,30,31,31,30,31,30,31}; Date::Date( int m, int d, int y ) { setDay( d ); setMonth( m ); setDate( m, d, y ); } Date::Date( const Date &d ) { month = d.month, day = d.day, year = d.year; setDate( month, day, year ); } void Date::setMonth( int m ) { month = m; } void Date::setDay( int d ) { day = d ; } void Date::setYear( int y ) { year = y ; } void Date::setDate( int m, int d, int y ) { if( m < 1 || m > 12 ) { cout<<"Invalid date has been reset\n"; } month = ( m >= 1 && m <= 12 ) ? m : 1; year = ( y >= 1 && y <= 9999 ) ? y : 2004; if ( day = ( d < 1 || d > days[ month ] )) { cout <<"Invalid date has been reset\n\n"; } if ( month == 2 && leapYear( year )) day = ( d >= 1 && d <= 29 ) ? d : 1; else day = ( d >= 1 && d <= days[ month ] ) ? d : 1; } bool Date::leapYear( int y )const { if (( y % 4 == 0 && y % 100 != 0 ) || y % 400 == 0 ) return true; else return false; } bool Date::lastDay( int d ) { if ( month == 2 && leapYear( year )) return d == 29; else return d == days[ month ]; } int Date::operator<( Date & d ) { if ( this->year == d.year ) { if ( this->month == d.month ) { if( this ->day < d.day ) { cout <<"here " << day; } return this->day < d.day; } else { return this->month < d.month; } } return this->year < d.year; } ostream &operator<<( ostream &output, const Date &d ) { static char *month[ 13 ] = { "", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; output << month[ d.month ] << ' ' << d.day << ", " << d.year; return output; } istream &operator>>( istream &input, Date &d ) { int mo, dy, yr; char c; input >> mo >> c >> dy >> c >> yr ; d.setDate(mo, dy, yr); return input; } #include"Date.h" #include<iostream> using std::cout; using std::cin; #include<cstring> void relational(); Date date1, date2, result; int main() { relational(); return 0; } void relational() { char yn[100], op[4], answer[6]; do { cout << "Enter the < symbol with 2 dates in the form dd/mm/yyyy < dd/mm/yyyy\n"; cin >> date1 >> op >> date2; if ( strcmp( op, "<" ) == 0 ) strcpy( answer, (date1 < date2, "true", "false") ); else { cout << "Invalid operator. Valid relational " << "operators are <n"; strcpy( answer, "bogus" ); } cout << date1 <<" "<< op <<" "<< date2 <<" = "<< answer << "\n"; cout << "\nWould you like to see the invalid response again? "; cin >> yn; } while ( yn[0] == 'y' || yn[0] == 'Y' ); } |