https://rentry.org/PPP2_p184

// Code derived from Stroustrup's PPP2 book
// § 6.3.3 Implementing tokens
//  -and beginning on p 184

#include <iostream>

using std::cout;

// a simple user-defined type
class Token {
 public:
  Token(char ch) : kind{ch} {}

  Token(char ch, double val) : kind{ch}, value{val} {}

  char   kind  = '0';
  double value = 0.0;
};

int main()
{
  // initialize t1 so that t1.kind = '+'
  Token t1{'+'};

  // initialize t2 so that t2.kind = '8' and t2.value = 11.5
  Token t2{'8', 11.5};

  cout << t1.kind << '\n'   //
       << t1.value << '\n'  //
       << t2.kind << '\n'   //
       << t2.value << '\n';
}

build & run:

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

PrevUpNext

Edit
Pub: 14 Mar 2023 01:57 UTC
Edit: 30 Apr 2023 07:28 UTC
Views: 361