Function Overloading with an Example in C++

 

Function Overloading with an Example in C++

In this tutorial, we will Learn:

What is function overloading in C++?

How to write a C++ program to find the areas of the circle (PI * r * r), rectangle (l * b), and square (x * x) by getting the r, l, b, and x through keyboard and printing the areas on the console using the method area() by applying the concept of function overloading.

Solution:

Video Tutorial:

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

Summary:

In this article, we understood What is Function Overloading with an Example in C++ and wrote an overloaded function to find the area of the circle, rectangle, and square.

If you like the tutorial share it with your friends. Like the Facebook page for regular updates and YouTube channel for video tutorials.

Leave a Comment

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

Welcome to VTUPulse.com


Computer Graphics and Image Processing Mini Projects -> Click Here

Download Final Year Project -> Click Here

This will close in 12 seconds