// // dateclass1.cpp // // This program contains the definition and implementation // of a simple date class. // #include class date { private: int month; int day; int year; public: date(); int getDay(); int getMonth(); int getYear(); void set(int newMonth, int newDay, int newYear); }; // // This is the definition of the constructor function. // date::date() { month = 0; day = 0; year = 0; } // // These are the definitions of the three accessor // functions. // int date::getDay() { return day; } int date::getMonth() { return month; } int date::getYear() { return year; } // // set sets the date object to the month, day, // and year passed as parameters if it's a // legal date. // void date::set(int newMonth, int newDay, int newYear) { if (newMonth >= 1 && newMonth <= 12 && newDay >= 1 && newDay <= 31 && newYear > 0) { month = newMonth; day = newDay; year = newYear; } return; } // // This overloads the << operator so that // dates can be printed using it. // ostream& operator<<(ostream& out, date d) { out << d.getMonth() << "/" << d.getDay() << "/" << d.getYear(); return out; } // // This overloads the >> operator so that // date in the form 99/99/99 can be input using it. // istream& operator>>(istream& in, date& d) { char c; int month, day, year; in >> month >> c >> day >> c >> year; d.set(month, day, year); return in; } int main() { date anyDate; int month, day, year; cout << "Please enter a date as month/day/year: "; cin >> anyDate; cout << "That date is: " << anyDate << endl; return 0; }