https://rentry.org/PPP2_p328

// Code derived from Stroustrup's PPP2 book
// § 9.7.3 Default constructors
//  -and beginning on p 328

enum class Month {
  jan = 1,
  feb,
  mar,
  apr,
  may,
  jun,
  jul,
  aug,
  sep,
  oct,
  nov,
  dec
};

//------------------------------------------------------------------------------

// let’s provide [a default constructor] (just to show we can)
class Date {
 public:
  Date();  // default constructor

  // . . .

  int   year() { return y; }
  Month month() { return m; }
  int   day() { return d; }

 private:
  int   y;
  Month m;
  int   d;
};

// We have to pick a default date. The first day of the 21st century might be a
// reasonable choice
Date::Date() : y{2001}, m{Month::jan}, d{1} {}

//------------------------------------------------------------------------------

int main() {}

build & run:

g++ -std=c++20 -O2 -Wall -pedantic ./ch_09/main_p328.cpp && ./a.out

PrevUpNext

Edit
Pub: 06 Apr 2023 17:49 UTC
Edit: 03 May 2023 01:18 UTC
Views: 607