Experiment No 7 - Function Overloading in C++
Experiment No 7 - Function Overloading in C++
AIM7: Write a C++ program to find area of triangle, circle, and rectangle
using function overloading.
Requirements:
Theoretical Description:
Program 1:
#include<iostream.h>
#include<conio.h>
float area(float a)
{
return (3.14*a*a);
}
int area(int p,int q)
{
return(p*q);
}
void main()
{
clrscr();
cout<<"Area of circle:"<<area(6);
cout<<"Area of Rectangle:"<<area(5,6);
getch();
Outputs:
Program 2:
#include<iostream.h>
#include<conio.h>
const float pi=3.14;
float area(float r)
{
float ar;
ar=pi*r*r;
return ar;
}
Program 3:
#include<iostream>
using namespace std;
int area(int);
int area(int,int);
float area(float);
float area(float,float);
int main()
{
int s,l,b;
float r,bs,ht;
cout<<"Enter side of a square:";
cin>>s;
cout<<"Enter length and breadth of rectangle:";
cin>>l>>b;
cout<<"Enter radius of circle:";
cin>>r;
cout<<"Enter base and height of triangle:";
cin>>bs>>ht;
cout<<"Area of square is"<<area(s);
cout<<"\nArea of rectangle is "<<area(l,b);
cout<<"\nArea of circle is "<<area(r);
cout<<"\nArea of triangle is "<<area(bs,ht);
}
int area(int s)
{
return(s*s);
}
int area(int l,int b)
{
return(l*b);
}
float area(float r)
{
return(3.14*r*r);
}
float area(float bs,float ht)
{
return((bs*ht)/2);
}
Explanation:
Sample Input
Enter side of a square:2
Enter length and breadth of rectangle:3 6
Enter radius of circle:3
Enter base and height of triangle:4 4
Sample Output
Area of square is4
Area of rectangle is 18
Area of circle is 28.26
Area of triangle is 8
Program 4:
#include<iostream>
#include<cstdlib>
using namespace std;
float area(float r)
{
return(3.14 * r * r);
}
float area(float b,float h)
{
return(0.5 * b * h);
}
float area(float l,float b)
{
return (l * b);
}
int main()
{
float b,h,r,l;
int ch;
do
{
cout<<"\n\n *****Menu***** \n";
cout<<"\n 1. Area of Circle";
cout<<"\n 2. Area of Triangle";
cout<<"\n 3. Area of Rectangle";
cout<<"\n 4. Exit";
cout<<"\n\n Enter Your Choice : ";
cin>>ch;
switch(ch)
{
case 1:
{
cout<<"\n Enter the Radius of Circle : ";
cin>>r;
cout<<"\n Area of Circle : "<<area(r);
break;
}
case 2:
{
cout<<"\n Enter the Base & Height of Triangle : ";
cin>>b>>h;
cout<<"\n Area of Triangle : "<<area(b,h);
break;
}
case 3:
{
cout<<"\n Enter the Length & Bredth of Rectangle : ";
cin>>l>>b;
cout<<"\n Area of Rectangle : "<<area(l,b);
break;
}
case 4:
exit(0);
default:
cout<<"\n Invalid Choice... ";
}
}
while(ch!=4);
return 0;
}
Program 5:
#include
#include
class shape
{
float l,b,r,ac,ar;
public:
void getc(void)
{
cout<<"Enter radius of circle\n";
cin>>r;
cout<<"\n**********************************************************\n";
}
void area(void)
{
ac=3.14*r*r;
cout<<"\n**************************************************************\n";
cout<<"\nRadius of circle is "<<r;
cout<<"\nArea of circle is "<<ac;
}
void area(float x, float y)
{
l=x;
b=y;
ar=l*b;
cout<<"\nLength of rectangle is "<<l;
cout<<"\nBreadth of rectangle is "<<b;
cout<<"\nArea of rectangle is "<<ar;
}
};
void main(void)
{
float m,n;
clrscr();
class shape s1,s2;
s1.getc();
cout<<"\nEnter length and breadth of rectangle\n";
cin>>m>>n;
s1.area();
cout<<"\n*************************************************************\n";
s2.area(m,n);
getch();
}