Saturday 22 July 2017

Write a program to calculate the area of different shapes (circle, square, triangle, rectangle) using function overloading.

Coding:

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

#include<conio.h>
#define PI 3.141
class Shape
{
 float a;
 public:
    void area(int r);
    void area(float s);
    void area(float l,float b);
    void area(float a,float b,float c);
};
void Shape::area(int r)
{
  a=float(PI*r*r);
  cout<<"\nArea of Circle is:"<<a;
}
void Shape::area(float s)
{
 a=pow(s,2);
 cout<<"\nArea of Square is:"<<a;
}
void Shape::area(float l,float b)
{
  a=l*b;
  cout<<"\nArea of Rectangle is:"<<a;
}
void Shape::area(float a,float b,float c)
{
  float s;
  s=(a+b+c)/2;
  a=sqrt(s*(s-a)*(s-b)*(s-c));
  cout<<"\nArea of Tringle is:"<<a;
}
void main()
{
  clrscr();
  float k=6.5;
  Shape s;
  s.area(5);
  s.area(k);
  s.area(5,6.5);
  s.area(10,15.5,6.8);
  getch();
}


Output:


No comments:
Write comments