Skip to content
Exercise · Functions · lesson 8 of 11

Exercise: shape areas

Write three overloads of area: double area(double radius) returns the area of a circle, and double area(double width, double height) returns the area of a rectangle. The third overload, double area(double width, double height, double border), returns the area of a frame border wide whose outer dimensions are width by height. Take π from the constant kPi, which the file already defines.

  • area(2.0, 3.5) → 7.0
  • area(10.0, 6.0, 1.0) → 28.0
  • area(4.0, 10.0, 3.0) → 40.0

Constraints: the sizes are non-negative; if the frame is so wide that no hole is left inside it, its area is width * height.

shape_area.cpp — cpp
cpp · 14 lines
Ctrl+Enter run · Esc leave editor