Function Overloading for Area Calculation

Function Overloading for Area Calculation

Medium C++ Functions 46 views
Explanation Complexity

Problem Statement

Create multiple functions with same name for different shapes.
Real Life: Same "calculate area" action works for circle, rectangle etc.

Input Format

No user input.
Different shapes’ dimensions are passed to functions.

Output Format

Area of different shapes calculated using functions with the same name.

Example

Circle radius = 5
Rectangle length = 4, width = 6
Area of Circle = 78.5
Area of Rectangle = 24

Constraints

• Same function name

• Different parameter lists

• Compile-time polymorphism

Concept Explanation

In C++, function overloading allows multiple functions with the same name
as long as their parameters are different.
This is useful when the same action (like calculating area) applies to different shapes.

Step-by-Step Explanation

1.Create a function area(double r) for circle.

2.Use formula: π × r × r.

3.Create another function area(int l, int w) for rectangle.

4.Use formula: l × w.

5.Both functions have the same name area, but different parameters.

6.When the function is called:

• Compiler decides which function to call based on arguments.

7.Print the calculated area.

Concept Explanation

In C++, function overloading allows multiple functions with the same name
as long as their parameters are different.
This is useful when the same action (like calculating area) applies to different shapes.

Step-by-Step Explanation

1.Create a function area(double r) for circle.

2.Use formula: π × r × r.

3.Create another function area(int l, int w) for rectangle.

4.Use formula: l × w.

5.Both functions have the same name area, but different parameters.

6.When the function is called:

• Compiler decides which function to call based on arguments.

7.Print the calculated area.

Input / Output Format

Input Format
No user input.
Different shapes’ dimensions are passed to functions.
Output Format
Area of different shapes calculated using functions with the same name.
Constraints
• Same function name

• Different parameter lists

• Compile-time polymorphism

Examples

Input:
Circle radius = 5 Rectangle length = 4, width = 6
Output:
Area of Circle = 78.5 Area of Rectangle = 24

Example Solution (Public)

C++
int calculateArea(int length, int width) {
    return length * width;
}

float calculateArea(float radius) {
    return 3.14159 * radius * radius;
}

void function_q7_overloading() {
    int rectArea = calculateArea(10, 5);
    float circArea = calculateArea(7.0f);
    
    cout << "Rectangle area: " << rectArea << endl;
    cout << "Circle area: " << circArea << endl;
}

Official Solution Code

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

float calculateArea(float radius) {
    return 3.14159 * radius * radius;
}

void function_q7_overloading() {
    int rectArea = calculateArea(10, 5);
    float circArea = calculateArea(7.0f);
    
    cout << "Rectangle area: " << rectArea << endl;
    cout << "Circle area: " << circArea << endl;
}
Please login to submit solutions.
Editor
Output

                                        
Please login to submit solutions.