https://rentry.org/PPP2_p315

// Code derived from Stroustrup's PPP2 book
// § 9.4.4 Defining member functions
//  -and beginning on p 315

#include <iostream>

using namespace std;

// simple Date (some people prefer implementation details last)
class Date {
 public:
  // We can also define member functions right in the class definition

  Date(int yy, int mm, int dd) : y{yy}, m{mm}, d{dd}
  {
    // . . .

    cout << y << m << d << '\n';  // rm's 'unused variable' error
  }

  void add_day([[maybe_unused]] int n)
  {
    // . . .
  }

  int month() { return m; }

  // . . .

 private:
  int y, m, d;  // year, month, day
};

int main()
{
  Date sunday{2004, 8, 29};  // initialize sunday with {2004, 8, 29}
  sunday.add_day(7);         // skip 7 days
  return sunday.month();     // get sunday's month
}

build & run:

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

PrevUpNext

Edit
Pub: 06 Apr 2023 17:18 UTC
Edit: 03 May 2023 01:09 UTC
Views: 555