date without validation

Example by: Ira Oldham, References: Savitch eighth edition, section 10.2; Gaddis seventh edition, not found

This Date class accepts integers for the day, month and year.

1  class Date
2  {
3    int day;
4    int month;
5    int year;
6  public:
7    void set_date(int day_parm, int month_parm, int year_parm);
8    int  get_day()   {return day;}
9    int  get_month() {return month;}
10   int  get_year()  {return year;}
11 };
12 int main(void)
13 {
14   Date today;
15   int day;
16   int month;
17   int year;
18   cout << "Input date:\nday:   ";
19   cin  >> day;
20   cout << "month: ";
21   cin  >> month;
22   cout << "year:  ";
23   cin  >> year;
24   today.set_date(day, month, year);
25   day   = today.get_day();
26   month = today.get_month();
27   year  = today.get_year();
28   cout << "Date: " << month << '/' << day << '/' << year << endl;
29   return 0;
30 }
31 void Date::set_date(int day_parm, int month_parm, int year_parm)
32 {
33   day   = day_parm;
34   month = month_parm;
35   year  = year_parm;
36 }
Input date:
day:   -4
month: 45
year:  1066
Date: 45/-4/1066

This date class works, but only if the user provides valid dates. It would be better if the set_date method only allowed valid dates.

The get_day, get_month, and get_year are accessor methods; they only access data from the object. The set_date method changes the data stored in this object, so it is called a mutator method.

It is very important that all mutator methods validate the data.