A program to implement function overloading for calculating the area of four different figures which are given below:
a) Area of Circle
b) Area of Rectangle
c) Area of Square
d) Area of Triangle

#include<iostream.h>
#include<stdio.h>
#include<math.h>
#include<conio.h>

double ar;

void area(long double r)
{
long double const pi=3.14159;
ar= pi * pow(r,2);
cout<<"The area of the circle is: "<<ar<<endl;
}

void area(double b, double h)
{
ar= b*h;
cout<<"The area of the rectangle is: "<<ar<<endl;
}

void area(float s)
{
ar= pow(s,2);
cout<<"The area of the square is: "<<ar<<endl;
}

void area(float b, float h)
{
ar= 0.5 * b * h;
cout<<"The area of the triangle is :"<<ar<<endl;
}

void main()
{
 clrscr();
 int ch;

 do
  {
   cout<<"1. Area of Circle."<<endl;
   cout<<"2. Area of Recatangle."<<endl;
   cout<<"3. Area of Square."<<endl;
   cout<<"4. Area of Triangle."<<endl;
   cout<<"5. Exit."<<endl;
   cout<<"Enter your choice: ";
   cin>>ch;
   cout<<endl;

   switch(ch)
    {
     case 1: {
                  long double radius;
                  cout<<"Enter radius of the circle: ";
                  cin>>radius;
                  cout<<endl;
                  area(radius);
                  break;
                 }
     case 2: {
                  double base_rec,height_rec;
                  cout<<"Enter base of rectangle: ";
                  cin>>base_rec;
                  cout<<"Enter height of rectangle: ";
                  cin>>height_rec;
                  cout<<endl;
                  area(base_rec,height_rec);
                  break;
                 }
     case 3: {
                  float side;
                  cout<<"Enter side of the square: ";
                  cin>>side;
                  cout<<endl;
                  area(side);
                  break;
                 }
     case 4: {
                  float base_tri,height_tri;
                  cout<<"Enter base of triangle: ";
                  cin>>base_tri;
                  cout<<"Enter height of triangle: ";
                  cin>>height_tri;
                  cout<<endl;
                  area(base_tri,height_tri);
                  break;
                 }
     case 5: {
                  break;
                 }
     default: {
                  cout<<"Wrong choice entered"<<endl;
                  break;
                }
    }
   cout<<endl;
  }
 while(ch!=5);
}

 

< Back