C++ Program: Function Overloading
// 9 C++ program - Find the area of square/ rectangle/triangle using function overloading.
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
class Funcoverload
{
public:
float area(float a);
float area(float l, float b);
float area(float s1, float s2, float s3);
};
float Funcoverload::area(float a) //To compute area of square
{
return a*a;
}
float Funcoverload::area(float l,float b) //To compute area of rectangle
{
return l*b;
}
float Funcoverload::area(float s1,float s2,float s3) //To compute area of triangle
{
float s=(s1+s2+s3)/2;
return sqrt(s*(s-s1)*(s-s2)*(s-s3));
}
void main()
{
float s1,s2,s3;
int choice;
Funcoverload f;
clrscr( );
while(1)
{
cout<<"Program demonstrates Function Overloaded...!!!"<<endl;
cout<<"1.To find area of square"<<endl;
cout<<"2.To find area of rectangle"<<endl;
cout<<"3.To find the area of triangle"<<endl;
cout<<"4.Exit"<<endl;
cout<<"Enter your Choice"<<endl;
cin>>choice;
switch(choice)
{
case 1: cout<<"Enter the input for square"<<endl;
cin>>s1;
cout<<"Area of Square= "<<f.area(s1)<<endl;
break;
case 2: cout<<"Enter the input for rectangle"<<endl;
cin>>s1>>s2;
cout<<"Area of Rectangle= "<<f.area(s1,s2)<<endl;
break;
case 3: cout<<"Enter the input for triangle"<<endl;
cin>>s1>>s2>>s3;
cout<<"Area of Triangle= "<<f.area(s1,s2,s3)<<endl;
break;
case 4: cout<<"End of Program....."<<endl;
getch();
exit(1);
default:cout<<"Invallid Choice....!!!"<<endl;
}
getch( );
}
}
Comments
Post a Comment