https://rentry.org/PPP2_p95

// Code derived from Stroustrup's PPP2 book
// § 4.3 Expressions
// -beginning on p 95

#include <iostream>

using std::cout;

int main()
{
  // compute area:
  int length = 99;  // assign 99 to length
  int width  = 40;
  int area   = length * width;  // a multiplication

  int perimeter = (length + width) * 2;  // add then multiply
  // int perimeter = length * 2 + width * 2;  // (clumsy)
  // int perimeter = length + width * 2;      // add width * 2 to length (error)

  cout << length << '\n'  // output above variable values to console...
       << width << '\n'   //
       << area << '\n'    //
       << perimeter << '\n';
}

build & run:

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

PrevUpNext

Edit
Pub: 21 Jan 2023 14:51 UTC
Edit: 29 Apr 2023 06:40 UTC
Views: 466