Function Overloading

Lab C++ Intermediate 20 points

Write three overloads of area: area(double r) (circle), area(double w, double h) (rectangle) and area(int side) (square, returns int). Read shapes and print each area (circle and rectangle with 2 decimals).

Input

n, then lines circle r, rect w h or square s.

Output

n lines.

Example

Input

3
circle 1
rect 2.5 4
square 7

Output

3.14
10.00
49
Sample input available Sample output available
Editor
C++

Hints

Overloads differ by their parameter types or count.

Solution explanation

The compiler picks the overload that best matches the argument types.

Show solution
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
using namespace std;

double area(double r) { return M_PI * r * r; }
double area(double w, double h) { return w * h; }
int area(int side) { return side * side; }

int main() {
    int n;
    cin >> n;
    cout << fixed << setprecision(2);
    for (int i = 0; i < n; i++) {
        string kind;
        cin >> kind;
        if (kind == "circle") {
            double r;
            cin >> r;
            cout << area(r) << "\n";
        } else if (kind == "rect") {
            double w, h;
            cin >> w >> h;
            cout << area(w, h) << "\n";
        } else {
            int s;
            cin >> s;
            cout << area(s) << "\n";
        }
    }
    return 0;
}
Output
Answer
HOME LEARN COMMUNITY DASHBOARD