How to Overload a Function in C++

Function Overloading is a process in which we define a function with same name but their arguments (parameters) are different according to the data types . Function can also be overload with increasing or decreasing the number of arguments in a function. Remember Compiler choose the function according to the input when it deal with overloaded functions.

Source Code


#include<iostream>
using namespace std;

  class Function_overloading
  {
   public:
   void calculate (int a,int b)
   {
    cout<<" SUM IS : " <<a+b;
   }
   void calculate (double a,double b)
   {
    cout<<" SUM IS : " <<a+b;
   }
   void calculate (float a,float b)
   {
    cout<<" SUM IS : " <<a+b;
   }
  };
  int main ()
  {
   Function_overloading f;
   f.calculate(24,45);
   cout<<endl;
   f.calculate(45.3,67.68);
   cout<<endl;
   f.calculate(1234567,7654321);
  }

Output of the Program

C++ Functional Programming


Share on Google Plus

About Asad

Asad Niazi is Software Engineer , Programmer, Web Developer and a young mentor of BloggersTown and PProgramming. Asad Love to writes about Technology, Programming, Blogging and make money online.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment