Implement function overloading to find Area of Shapes

What is Function Overloading?

When we define one function with more than one definition it is known as Function overloading. For example:

int shape(int x);

int shape(int x, int y);

We can have multiple definitions for the same function name in the same scope. The definition of the function must differ from each other by the types and/or the number of arguments in the argument list. We can not overload function declarations that differ only by return type.

Program to implement Function Overloading to find area of different shapes:

#include <iostream>

using namespace std;
class measure
{
    public:
        void shape(int r);
        void shape(int l,int b);
        void shape(float t,int d,int e);
        void shape(long a);
        void shape(float c, long int g);
        void shape(double j);
        void shape(float h, double f);
};
void measure::shape(int r)
{
    cout<<"area of the circle is "<<3.14*r*r;
}
void measure::shape(int l,int b)
{
    cout<<"area of the rectangle is "<<l*b;
}
void measure::shape(float t,int d,int e)
{
    cout<<"area of the triangle is "<<t*d*e;
}
void measure::shape(long a)
{
    cout<<"area of the square is "<<a*a;
}
void measure::shape(float c, long int g)
{
    cout<<"Volume of the cone is "<<((3.14*c*c*g)/3);
}
void measure::shape(double j)
{
    cout<<"Volume of the sphere is "<<(4/3)*3.14*j*j*j;
}
void measure::shape(float h, double f)
{
    cout<<"nVolume of the Cylinder is "<<3.14*f*f*h;
}
int main()
{
    char choice;
    int r,d,e,l,b;
    float t,c,h;
    long a;
    int ch;
    double j,f;
    long int g;
    measure obj;
    do
    {
        cout<<"tImplement Function Overloading";
        cout<<"nn1. area of circle";
        cout<<"n2. area of rectangle";
        cout<<"n3. area of triangle";
        cout<<"n4. area of square";
        cout<<"n5. Volume of the cone";
        cout<<"n6. Volume of the sphere";
        cout<<"n7. Volume of the cylinder";
        cout<<"ntEnter your choice ";
        cin>>ch;

        switch(ch)
        {
            case 1:
                cout<<"enter the value of radius of the circle n";
                cin>>r;
                obj.shape(r);
                break;
            case 2:
                cout<<"enter the sides of rectangle n";
                cin>>l>>b;
                obj.shape(l,b);
                break;
            case 3:
                cout<<"enter the sides of triangle n";
                cin>>d>>e;
                obj.shape(0.5,d,e);
                break;
            case 4:
                cout<<"enter the sides of square ";
                cin>>a;
                obj.shape(a);
                break;
            case 5:
                cout<<"nEnter the radius of the cone ";
                cin>>c;
                cout<<"nEnter the height of the cone ";
                cin>>g;
                obj.shape(c,g);
                break;
            case 6:
                cout<<"nEnter the radius ";
                cin>>b;
                obj.shape(b);
                break;
            case 7:
                cout<<"nEnter the radius ";
                cin>>f;
                cout<<"nEnter the height ";
                cin>>h;
                obj.shape(h,f);
                break;
            default:
                cout<<"nThe choice entered is a wrong choice";
        }
        cout<<"nWant to Continue? (Y/N)";
        cin>>choice;
    }
    while(choice=='Y' || choice == 'y');
    return 0;
}

Sample Output:

   Implement Function Overloading

1. area of circle
2. area of rectangle
3. area of triangle
4. area of square
5. Volume of the cone
6. Volume of the sphere
7. Volume of the cylinder
    Enter your choice 1
enter the value of radius of the circle
13
area of the circle is 530.66
Want to Continue? (Y/N)y
    Implement Function Overloading

1. area of circle
2. area of rectangle
3. area of triangle
4. area of square
5. Volume of the cone
6. Volume of the sphere
7. Volume of the cylinder
    Enter your choice 2
enter the sides of rectangle
12
8
area of the rectangle is 96
Want to Continue? (Y/N)y
    Implement Function Overloading

1. area of circle
2. area of rectangle
3. area of triangle
4. area of square
5. Volume of the cone
6. Volume of the sphere
7. Volume of the cylinder
    Enter your choice 3
enter the sides of triangle
6
9
area of the triangle is 27
Want to Continue? (Y/N)y
    Implement Function Overloading

1. area of circle
2. area of rectangle
3. area of triangle
4. area of square
5. Volume of the cone
6. Volume of the sphere
7. Volume of the cylinder
    Enter your choice 4
enter the sides of square 8
area of the square is 64
Want to Continue? (Y/N)y
    Implement Function Overloading

1. area of circle
2. area of rectangle
3. area of triangle
4. area of square
5. Volume of the cone
6. Volume of the sphere
7. Volume of the cylinder
    Enter your choice 5

Enter the radius of the cone 12

Enter the height of the cone 4
Volume of the cone is 602.88
Want to Continue? (Y/N)y
    Implement Function Overloading

1. area of circle
2. area of rectangle
3. area of triangle
4. area of square
5. Volume of the cone
6. Volume of the sphere
7. Volume of the cylinder
    Enter your choice 6

Enter the radius 12
area of the circle is 452.16
Want to Continue? (Y/N)y
    Implement Function Overloading

1. area of circle
2. area of rectangle
3. area of triangle
4. area of square
5. Volume of the cone
6. Volume of the sphere
7. Volume of the cylinder
    Enter your choice 7

Enter the radius 12

Enter the height 4

Volume of the Cylinder is 1808.64
Want to Continue? (Y/N)n

Leave a Comment