Interview Questions   Tutorials   Discussions   Programs   Videos   

C++ - Write a program for function overloading?

asked marvit August 18, 2014 08:35 PM  

Write a program for function overloading?


1 Answers

answered By bhjahnavi   0  
 Function overloading means two or more functions can have the same name but either the number of arguments or the data type of arguments has to be different. Return type has no role because function will return a value when it is called and at compile time compiler will not be able to determine which function to call.
/*program to demonstrate function overloading*/
#include <iostream>
 using namespace std;
 long add(long, long);
float add(float, float);
 int main()
  long a, b, x;
  float c, d, y;
  cout << "Enter two integers\n";
  cin >> a >> b;
  x = add(a, b);
  cout << "Sum of integers: " << x << endl;
  cout << "Enter two floating point numbers\n";
  cin >> c >> d;
  y = add(c, d);
  cout << "Sum of floats: " << y << endl;
  return 0;
 long add(long x, long y)
  long sum;
  sum = x + y;
  return sum;
 float add(float x, float y)
  float sum;
  sum = x + y;
  return sum;
   add comment

Your answer

Join with account you already have



Ready to start your tutorial with us? That's great! Send us an email and we will get back to you as soon as possible!