Looking for NEET / JEE Coaching? Enquire Now
• # Program to calculate area of circle and area of rectangle using function overloading using C++

Facing a difficulty in writing C program? Want to know a C program to calculate area of a circle and rectangle using function overloading? Find advice from experts on this Ask Expert page.

Following is a question that was asked in the MSBTE (Maharashtra State Board of Technical Education, Mumbai) Diploma in Computer Engineering question paper for Winter 2018 examination.

This question is asked for 04 marks and hence requires some explanation along with the program code. Please let me know the solution.

Q) Write a program to calculate area of circle and area of rectangle using function overloading.
[4 marks]

In a program, if you use the same function more than once but with different parameters, that is called as Function Overloading. In the example, below, the function that I have used is Area. I am calling the same function area twice, the first time when it is called, it executes the area of a rectangle and second time when its called it executes the area of a circle.

If you don't have C++ compiler, you can execute it in below playground to check the result.
https://code.sololearn.com/c39N9RN6b4Md/#cpp

#C++ Program to calculate the Area of Circle and Rectangle.

#include

using namespace std;

int area(int);
int area(int,int);
float area(float);
float area(float,float);

int main()
{
int l,b;
float r;

cout<<"Enter length and breadth of rectangle:";
cin>>l>>b;

cin>>r;

cout<<"\nArea of rectangle is "< cout<<"\nArea of circle is "<
}

int area(int l,int b)
{
return(l*b);
}
float area(float r)
{
return(3.14*r*r);
} Delete Attachment

• Object Oriented Programming has given us many new features. Function overloading is one that features. The classic definition of function overloading is that the class may have functions with same name but with different arguments. Depending upon the arguments in number, data type and sequence the appropriate function among available functions get executed. Though class is having more than two functions with same name but with different prototyping, only one function among them which exactly match in number of arguments, data type and the sequence is able to execute. This is called as function overloading.
Below is the program which find out area circle when no argument is supplied to area function and it find out area of rectangle when two float arguments were supplied to area function. Note that the function names were same but their prototyping are different.
#include
#include
class shape
{
float l,b,r,ac,ar;
public:
void getc(void)
{
cin>>r;
cout<<"\n******************************************************************\n";
}
void area(void)
{
ac=3.14*r*r;
cout<<"\n*******************************************************************\n";
cout<<"\nRadius of circle is "< cout<<"\nArea of circle is "< }
void area(float x, float y)
{
l=x;
b=y;
ar=l*b;
cout<<"\nLength of rectangle is "< cout<<"\nBreadth of rectangle is "< cout<<"\nArea of rectangle is "< }
};
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();
}
In above program when area function is called by object s1 without any argument then it goes for the area function which is used for area calculation of circle. This is because that is the only function which is perfect match for function call. Another area function is also available but that is not the match for function call as it requires two float arguments.
Now when area function is called by s2 object, then it goes for the area function which is used for area calculation of rectangle. This is because that is the only function which is perfect match for function call.