C++ program of function overloading to calculate area of square and rectangle.

Calculate area of square and rectangle.

This program illustrate the concept of function overloading to calculate area of square and rectangle.

In C++ ,Function overloading reffers to same name of function but differ in number of parameters or type of parameters.

Ways to Overload Functions

1. By passing different number of arguments(Refer to example 1).
2. By have different types of argument(refer to example 2).

eg 1:-
void scan(int a){}
void scan(int a,int b){}
void scan(int a,int b,int c){}

Here all 3 functions are overloaded because arguments passed to these function are different in numbers.

eg 2:-
int scan(int x){}
double scan(double x){}
float scan(float x){}

In this example of function overloading number of parameters passed are same but differ in type of parameter passed.

In this example we will calculate area of square and rectangle by using two different function with same name but different in number of parameters passed.

Program

#include<iostream.h>
#include<conio.h>
void area(int);
void area(int,int);
void main()
{
clrscr();
int x,y,z;
cout<<"Enter Side of Square:- ";
cin>>x;
cout<<"Enter Length of rectangle:- ";
cin>>y;
cout<<"Enter Breadth of rectangle:- ";
cin>>z;
area(x);
area(y,z);
getch();
}
void area(int a)
{
int i;
i=a*a;
cout<<"Area of square is:- "<<i<<endl;

}
void area(int b,int c)
{
int j;
j=b*c;
cout<<"Area of rectangle is:- "<<j;
}

OUTPUT:-

calculate area of square and rectangle.


Comment below if you are facing any problem and subscribe to our newsletter for latest update.


Also read

C++ Program to find greatest between three numbers using Functions
C++ program to swap two numbers using call by value


Summary
C++ program of function overloading to calculate area of square and rectangle.
Article Name
C++ program of function overloading to calculate area of square and rectangle.
Author
Publisher Name
Scanfcode
Publisher Logo

Leave a Reply

Your email address will not be published. Required fields are marked *