C++ Programming

Function Overloading Program In C++ ( Explained With Algorithm )

The main difference between Object-oriented Programming & Procedural Oriented Programming is that OOPs has some extra clear-cut advantage over POP. The best example of these extra features is Overloading feature. The overloading feature allows us the creation of certain elements ( operators & functions ) have the same name but different properties. There are basically two type of overloading features available in c++ i.e.,

  • Operator Overloading
  • Function Overloading

In this post, I will try to give you some examples of function overloading with a proper algorithm. You may refer to ⇒ C++ book ( My Favorite ).

But the first question here is, What is Function Overloading?

In simple words,

Function Overloading is a special feature of C++ which allow the creation of multiple functions with same name but different parameters. It means that in the same program we can create as many functions as we want with the same name but not with the same signatures.

function overloading in oops

Declaration of functions

fun();
fun(int a);
fun(int a,int b);

Here we have created 3 functions having the same name “fun” but the signatures are completely different i.e.,

  • For the first function, there are no any parameters.
  • For the second function, there is a parameter “int a”.
  • For the third function, there are two parameters “int a” & “int b”.

Defining the functions –

fun()
{

//body of the function 1

}

fun(int a)
{

//body of function 2

}

fun(int a,int b)
{

//body of function 3

}

Algorithm for Program of Function Overloading in c++

This is the algorithm of a simple area program in which we have to find the area of the circle as well as the area of the cylinder.

  1. Start
  2. Declare two functions (area).
  3. Accept 1st input (radius).
  4. Accept 2nd input (height).
  5. Implement functions.
  6. Display result.
  7. Define 1st function.
  8. Define 2nd function.
  9. End

Program of Function Overloading in C++

#include<stdio.h>
using namespace std;
floar area(float r);
float area(float r,float h);

int main()
{
float radius,height,result1,result2;
cout<<"Enter the radius & height :\n";
cin>>radius>>height;
result1=area(radius);
result2=area(radius,height);
cout<<"\n Area of circle: "<<result1;
cout<<"\n Area of cylinder: "<<result2;

return 0;
}

float area(float r)
{
retrun(3.14*r*r);
}

float area(float r,float h)
{
retrun(2*3.14*r*h);
}

Step-By-Step Explanation

  • Declare the header file.
  • Declare the functions – In this program, we have declared two functions with the same name “area” but different parameters.
  • Define main() function.
  • Accept inputs from the user – Accept two inputs as radius & height.
  • Call the functions in main().
  • Display the result.
  • Define area(int r) function – In our program, we have used this function to find the area of the circle. We already know that the area of the circle will be equal to π*radius*radius. So we have returned it in our function.
  • Define area(int r,int h) function – In our program, we have used this function to find the area of the cylinder. We already know that the area of the cylinder will be equal to 2*π*radius*height. So we have returned it in our function.

Recommended

 

 

Leave a Reply