https://rentry.org/PPP2_p307

// Code derived from Stroustrup's PPP2 book
// § 9.3 Interface and implementation
//  -and beginning on p 307

class X {
 public:  // interface (the user’s view of the class)
  int f(int i)
  {
    m = i;
    return mf();
  }

 private:  // implementation details (the implementer’s view of the class)
  int m;
  int mf() { return m; }
};

// for something that’s just data,
// A struct is a class where members are public by default
struct Y {
  int m;

  // . . .
};

int main()
{
  X x;

  // int y = x.mf();  // error: mf is private (i.e., inaccessible)

  int y = x.f(2);

  return y;
}

build & run:

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

PrevUpNext

Edit
Pub: 06 Apr 2023 14:23 UTC
Edit: 03 May 2023 00:59 UTC
Views: 536