This document discusses function overloading in C++. It explains that function overloading allows functions to have the same name but differ in their arguments. It provides examples of functions named "add" that take different argument types, including integers and doubles. The document also includes a full C++ program demonstrating how functions can be overloaded and called based on the argument types without specifying which overloaded function to use. The compiler will automatically select the correct function based on the argument types.
2. In C language we cannot make functions using same
name. But in C++ we can use function with same
name.
For eg:
void add();
int add();
In C language, It’s not allowed in functions using
same name in same program
In C++ it’s allowed in same program but this is
wrong too...
They obey the function are some conditions
3. Function overloading is defined as two or more functions
having same name but they should be differ in arguments.
For eg:
Void add();
Void add(int a);
int add(double a);
Void add (int a,int b);
These all are valid functions in the same program.
In this program four function have same name with different
arguments.
Void & int is return type in this program but return type is
doesn’t matter here. Arguments make the functions unique.
4. # include<iostream>
void add(int a,int b)
{
cout<<“ the addition of two numbers is “<<a+b<<endl;
}
void add(double a,double b)
{
cout<<“ the addition of two numbers is “<<a+b<<endl;
}
void main()
{
int first,second;
double fir,sec;
cout<< “enter two integers”<<endl;
cin>>first>>second; //we take input from user.
add(first,second);
cout<<“ enter two doubles value:”<<endl;
cin>>fir>>sec; //we take input from user.
add(fir,sec);}
5. In this program,
void add (int a, int b);
void add (double a, double b);
Two different function with same name & different
arguments.
add (first, second);
add (fir,sec);
We dont have to specify which function should be run.
Complier automatically decides depending on types.