// Code derived from Stroustrup's PPP2 book
// § 9.4.2 Member functions and constructors
// -and beginning on p 311
// simple D// simple Date:
// guarantee initialization with constructor
// provide some notational convenience
struct Date {
Date(int y, int m, int d); // check for valid date and initialize
void add_day(int n); // increase the Date by n days
int y, m, d; // year, month, day
};
Date::Date(int y, int m, int d) : y{y}, m{m}, d{d} {}
// naive
void Date::add_day(int n) { d += n; }
int main()
{
// Date my_birthday; // error: my_birthday not initialized
Date today{12, 24, 2007}; // oops! run-time error
Date last{2000, 12, 31}; // OK (colloquial style; that is, prefer this form)
[[maybe_unused]] Date next = {2014, 2, 14}; // also OK (slightly verbose)
[[maybe_unused]] Date christmas =
Date{1976, 12, 24}; // also OK (verbose style)
// We can now try to use our newly defined variables:
last.add_day(1);
// add_day(2); // error: what date?
}