#include #include using namespace std; const int date_length[] = { 0, //dummy element so that January will have subscript 1 31, //January 28, //February 31, //March 30, //April 31, //May 30, //June 31, //July 31, //August 30, //September 31, //October 30, //November 31 //December }; class date { int year; int month; //1 to 12 inclusive int day; //1 to date_length[month] inclusive public: date(int initial_month, int initial_day, int initial_year); void next(int count); //Go count days forward. void next(); //Go one day forward. void print() const; //Output date to cout (function declaration). }; //Don't forget ; at end of class declaration. int main() { //Construct d by passing three arguments to the constructor for d. //The constructor will initialize d's data members. date d(1, 1, 2014); //parentheses around function arguments cout << "How many days forward from "; //cout << d.month << "/" << d.day << "/" << d.year; //won't compile d.print(); //Pass the address of d to the print member function. cout << " do you want to go? "; int count; //uninitialized variable cin >> count; d.next(count); cout << "The new date is "; d.print(); cout << ".\n"; return EXIT_SUCCESS; } date::date(int initial_month, int initial_day, int initial_year) { if (initial_month < 1 || initial_month > 12) { cerr << "bad month " << initial_month << "/" << initial_day << "/" << initial_year << "\n"; exit(EXIT_FAILURE); } if (initial_day < 1 || initial_day > date_length[initial_month]) { cerr << "bad day " << initial_month << "/" << initial_day << "/" << initial_year << "\n"; exit(EXIT_FAILURE); } year = initial_year; month = initial_month; day = initial_day; } void date::next(int count) { //Call the no-explicit-argument next (line 87) count times. //Pass along the implicit pointer we received. while (--count >= 0) { //call another member function of same object //(*this).next(); //this->next(); next(); } } void date::next() { //Move to the next date. if (++day > date_length[month]) { day = 1; if (++month > 12) { month = 1; ++year; } } } void date::print() const //This is a function definition. { //cout << "The address of this object is " << this << ".\n"; //cout << "Size in bytes of this object is " << sizeof *this << ".\n"; //cout << (*this).month << "/" << (*this).day << "/" << (*this).year; //cout << this->month << "/" << this->day << "/" << this->year; cout << month << "/" << day << "/" << year; }