#ifndef DATE_H #define DATE_H class Date { public: Date() { month = 1; day = 1; year = 2001; } Date(int mm, int dd, int yy) { month = mm; day = dd; year = yy; } Date(const Date &old) { month = old.month; day = old.day; year = old.year; } void setDate(int mm, int dd, int yy=2003) { month = mm; day = dd; year = yy; } void showDate() const; void showWithName() const; void operator=(const Date&); int operator==(const Date&) const; int operator!=(const Date&) const; int operator<(const Date&) const; int operator>(const Date&) const; private: int month; int day; int year; }; #endif
#include "Date.h" #include <iostream.h> // print the Date in mm/dd/yy format void Date::showDate() const { cout << month << '/' << day << '/' << year; } // print the Date with month name void Date::showWithName() const { switch (month) { case 1 : cout << "January "; break; case 2 : cout << "February "; break; case 3 : cout << "March "; break; case 4 : cout << "April "; break; case 5 : cout << "May "; break; case 6 : cout << "June "; break; case 7 : cout << "July "; break; case 8 : cout << "August "; break; case 9 : cout << "September "; break; case 10 : cout << "October "; break; case 11 : cout << "November "; break; case 12 : cout << "December "; break; } cout << day << ", " << year; } // assignment operator void Date::operator=(const Date& old) { month = old.month; day = old.day; year = old.year; } // equality operator int Date::operator==(const Date& second) const { if (month == second.month && day == second.day && year == second.year) return 1; else return 0; } // inequality operator int Date::operator!=(const Date& second) const { if (month != second.month || day != second.day || year != second.year) return 1; else return 0; } // less than operator int Date::operator<(const Date& second) const { int less = 0; if (year < second.year) less = 1; else if (year == second.year && month < second.month) less = 1; else if (year == second.year && month == second.month && day < second.day) less = 1; return less; } // greater than operator int Date::operator>(const Date& second) const { int more = 0; if (year > second.year) more = 1; else if (year == second.year && month > second.month) more = 1; else if (year == second.year && month == second.month && day > second.day) more = 1; return more; }
#include <iostream.h> #include "Date.h" // test the Date class int main() { int mon; int da; Date today(10,17,2003); Date entry; cout << "Enter your birthday (month and day): "; cin >> mon >> da; entry.setDate(mon,da); cout << "This year your birthday is on "; entry.showDate(); cout << " ("; entry.showWithName(); cout << ")" << endl; if (today == entry) cout << "Happy Birthday!!" << endl; else if (today < entry) cout << "Your birthday is still to come this year." << endl; else cout << "You already had your birthday this year." << endl; }
>g++ Date.C main.C >a.out
> a.out Enter your birthday (month and day): 4 21 This year your birthday is on 4/27/2003 (April 27, 2003) You already had your birthday this year. > a.out Enter your birthday (month and day): 12 4 This year your birthday is on 12/4/2003 (December 4, 2003) Your birthday is still to come this year. > a.out Enter your birthday (month and day): 10 17 This year your birthday is on 10/17/2003 (October 17, 2003) Happy Birthday!!
Email Me |
Office Hours |
My Home Page |
Department Home |
MCC Home Page
© Copyright Emmi Schatz 2002