fork download
  1. /*create a program with an area function that is overloaded to calcuilate
  2. the area of a circle (given the radius)
  3. the area of rectangle (given length and width)
  4. the area of triangle (given the three side of the triangle)*/
  5. #include <iostream>
  6. using namespace std;
  7. #include <cmath>
  8.  
  9. #define PI 3.14159
  10.  
  11. // Area of a circle
  12. double area(double radius) {
  13. return PI * radius * radius;
  14. }
  15.  
  16. // Area of a rectangle
  17. double area(double length, double width) {
  18. return length * width;
  19. }
  20.  
  21. // Area of a triangle using Heron's formula
  22. double area(double a, double b, double c) {
  23. double s = (a + b + c) / 2; // semi-perimeter
  24. return sqrt(s * (s - a) * (s - b) * (s - c));
  25. }
  26.  
  27. int main() {
  28. std::cout << "Area of circle (r=5): " << area(5.0) << std::endl;
  29. std::cout << "Area of rectangle (l=4, w=6): " << area(4.0, 6.0) << std::endl;
  30. std::cout << "Area of triangle (sides=3,4,5): " << area(3.0, 4.0, 5.0) << std::endl;
  31.  
  32. return 0;
  33. }
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
Area of circle (r=5): 78.5397
Area of rectangle (l=4, w=6): 24
Area of triangle (sides=3,4,5): 6