https://rentry.org/PPP2_p310

// Code derived from Stroustrup's PPP2 book
// § 9.4.2 Member functions and constructors
//  -and beginning on p 310

#include <iostream>

using namespace std;

// simple Date (too simple?):
struct Date {
  int y;  // year
  int m;  // month in year
  int d;  // day of month
};

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

// helper functions:

void init_day(Date& dd, int y, int m, int d)
{
  // check that (y,m,d) is a valid date
  // if it is, use it to initialize dd

  cout << dd.y << y << m << d << '\n';
}

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

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

void f()
{
  [[maybe_unused]] Date today;

  // ...

  init_day(today, 12, 24, 2005);  // oops! (no day 2005 in year 12)

  // ...

  [[maybe_unused]] Date tomorrow;
  tomorrow.y = today.y;
  tomorrow.m = today.m;
  tomorrow.d = today.d + 1;  // add 1 to today
}

int main() { f(); }

build & run:

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

PrevUpNext

Edit
Pub: 06 Apr 2023 14:31 UTC
Edit: 03 May 2023 01:02 UTC
Views: 692