Function Overloading with an Example in C++


Function overloading means, two or more functions have the same names but different argument lists.
The arguments may differ in the type of arguments or number of arguments, or both.
However, the return types of overloaded methods can be the same or different is called function overloading.
Function overloading conceptshelps us to use the same function names multiple time in the same program.
Following program demonstrates the overloaded function to find the area of the circle, rectangle, and square.

#include<iostream>
 using namespace std;

 int area(int x)
 {
     return x*x; 
} 

int area(int l, int b) 
{
     return l*b;
 }

 double area(double r)
 {
     return 3.142*r*r;
 }

 int main()
 {
     int x, l, b;
     double r;
     cout<<"Enter the length of a square: ";
     cin>>x;
     cout<<"Enter the length of rectangle: ";
     cin>>l;
     cout<<"Enter the width of rectangle: ";
     cin>>b;
     cout<<"Enter the radius of circle: ";
     cin>>r;
     cout<<endl<<"The area of square is "<<area(x)<<endl;
     cout<<endl<<"The area of rectangle is "<<area(l, b)<<endl;
     cout<<endl<<"The area of circle is "<<area(r)<<endl;
 }
OUTPUT:
Enter the length of a square: 10 Enter the length of rectangle: 10 Enter the width of rectangle: 15 Enter the radius of circle: 5.5 The area of square is 100 The area of rectangle is 150 The area of circle is 95.0455


Share to whatsapp

More Questions from Object Oriented Concepts Module 1

Static member and count the number of objects in C++
View
What are the basic differences between the POP (Procedure Oriented Programming) and OOP (Object Oriented Programming)?
View
Basic concepts (features) of Object-Oriented Programming C++
View
Friend function in C++ with Programming example
View
Function Overloading with an Example in C++
View
What is an inline function? Write a C++ program to find the maximum of two numbers using inline function.
View