Function Overloading (Polymorphism) Code IN C++ to Calculate Area

Simple Program to apply concept of Function overloading we are using two functions named area with different parameters each. To calculate area of circle we need only 1 parameter i.e radius and to calculate area of rectangle we need two parameters length and breadth.



Program to Calculate Area of Circle and Rectangle using Function Overloading concept in C++


#include<iostream.h>
#include<conio.h>
float area_circle(float,int);
int area_rect(int,int);
class area
{
public:
int a,b,c,d;
area()
{
a=b=c=d;
}
area(int i,int j,int k, int l)
{
a=i;
b=j;
c=k;
d=l;
}
float area_circle(float pi, int r)
{
 return(pi*r*r);
}
int area_rect(int len,int bred)
{
return (len*bred);
}
};
int main()
{
int r,len,bred,ans1;
float pi=3.14,ans;
clrscr();
area a1,a2(1,1,1,1);
cout<<"enter radius";
cin>>r;
ans=a1.area_circle(pi,r);
cout<<"area of circle is"<<ans;
cout<<"enter length and breadth";
cin>>len>>bred;
ans1=a1.area_rect(len,bred);
cout<<"area of rectangle is"<<ans1;
getch();
return 0;
}
/* Output
enter radius1
area of circle is3.14enter length and breadth4
5
area of rectangle is20
*/

To want more such programs subscribe to our free email newsletter.
SHARE

Unknown

  • Image
  • Image
  • Image
  • Image
  • Image
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment