https://rentry.org/PPP2_p139

// Code derived from Stroustrup's PPP2 book
// § 5.3.3 Non-errors
//  -and beginning on p 139

#include <iostream>

using std::cout;

// calculate area of a rectangle
int area(int length, int width);

int main()
{
  int x4 = area(10, -7);  // OK: but what's a rectangle with a width of minus 7?
  int x5 = area(10.7, 9.3);   // OK: but calls area(10,9)
  char x6 = area(100, 9999);  // OK: but truncates the result

  cout << x4 << '\n'  //
       << x5 << '\n'  //
       << x6 << '\n';
}

int area(int length, int width) { return length * width; }

build & run:

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

PrevUpNext

Edit
Pub: 19 Feb 2023 06:05 UTC
Edit: 29 Apr 2023 09:01 UTC
Views: 460