https://rentry.org/PPP2_p326

// Code derived from Stroustrup's PPP2 book
// § 9.7.2 Copying
//  -and beginning on p 326

#include <iostream>

using namespace std;

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

// simple Date (use Month type):
class Date {
 public:
  Date(int yy, Month mm, int dd) : y{yy}, m{mm}, d{dd}
  {
    // initialize & check for valid date
    // ...
  }

  friend ostream& operator<<(ostream& os, const Date& d)
  {
    return os << '{' << d.y << ',' << int(d.m) << ',' << d.d << '}';
  }

 private:
  int   y;  // year
  Month m;
  int   d;  // day
};

int main()
{
  Date holiday{1978, Month::jul, 4};  // initialization

  [[maybe_unused]] Date d2 = holiday;

  Date d3 = Date{1978, Month::jul, 4};
  holiday = Date{1978, Month::dec, 24};  // assignment
  d3      = holiday;

  cout << Date{1978, Month::dec, 24} << '\n';
}

build & run:

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

PrevUpNext

Edit
Pub: 06 Apr 2023 17:44 UTC
Edit: 03 May 2023 01:16 UTC
Views: 372