Function Overloading (C++)
Function Overloading:-
- Function overloading is compile time polymorphism.
- Appropriate function call is resolved at compile time only.
- This means writing a function with a same name but with the different definations.
- There are some rules to write a n overloaded functions.
- Functions are overloaded on to the basis of number of parameters and type of parameters.
- C++ compilers differentiate the overloaded function by using naming decoration or name mangling.
- We can also overload constructor but not destructor.
- We can not overload a function depend on its return type value because at the time of calling the function return value is not considered.
#include<iostream.h>
class demo
{
public:
int i;
int j;
int fun()
{
cout<<“int”;
}
char fun()
{
cout<<“in char”;
}
};
int main()
{
int no;
demo h;
//
// We can not
//
no = h.fun();
return 0;
}
///
We can change a function by changing the type of a parameter.
class demo
{
public:
int i;
int j;
int fun(int i)
{
cout<<“in int”;
return 0;
}
int fun(char c)
{
cout<<“in char”;
return ‘c’;
}
};
int main()
{
int no;
demo h;
no = h.fun(‘a’); // in char
no = h.fun(97); // in int
no = h.fun((char)97); // in char
return 0;
}
///
class demo
{
public:
int i;
int j;
int fun(int i)
{
cout<<“in int”;
return 0;
}
int fun(float f)
{
cout<<“in float”;
return ‘c’;
}
};
int main()
{
int no;
demo h;
// Error
// Ambiguous call to function fun
//
no = h.fun(3.2);
no = h.fun(97);
return 0;
}
///
class demo
{
public:
int i;
int j;
int fun(int i)
{
cout<<“in int”;
return 0;
}
int fun(float f)
{
cout<<“in float”;
return ‘c’;
}
};
int main()
{
int no;
demo h;
no = h.fun(3.2f); // in float
no = h.fun(97); // in int
return 0;
}
///
We can overload a function by a pointer.
class demo
{
public:
int i;
int j;
int fun(int *i)
{
cout<<“in int*”;
return 0;
}
int fun(char *f)
{
cout<<“in char*”;
return ‘c’;
}
};
int main()
{
int no=10;
char c=’s’;
demo h;
no = h.fun(&no); // in int*
no = h.fun(&c); // in char*
return 0;
}
///
class demo
{
public:
int i;
int j;
int fun(const int i)
{
cout<<“in const”;
return 0;
}
int fun(int f)
{
cout<<“in int”;
return ‘c’;
}
};
int main()
{
int no=10;
demo h;
//
// Error ambiguous call
//
no = h.fun(no);
return 0;
}
///
We can not overload a function by using reference variable.
class demo
{
public:
int i;
int j;
//
// Function which accept reference
//
int fun(int &g)
{
cout<<“in reference”;
return 0;
}
int fun(int f)
{
cout<<“in int”;
return ‘c’;
}
};
int main()
{
int no=10;
demo h;
//
// Error ambiguous call
//
no = h.fun(no); //It is call by reference
return 0;
}
- C++ distinguishes this overloaded function by using name mangling and name mangling decoration technique.
- By using this technique, name of each of overloaded function is different.
- For the name mangling, C++ compiler uses return value of the function and the parameter of the function.
- By default all the function of C++ program uses name mangling technique.
For more reading about technology news in singapore and seo to online marketing do view more about other pages.