Home »
C++ programming language
Function overloading example based on Different types of Arguments in C++
Learn: How to implement function overloading based on Different types of arguments in C++ programming language?
If you didn't read about the function overloading, I would recommend please read C++ function overloading before reading this article.
Function overloading based on different types of arguments in C++
We can implement function overloading on the basis of different types of arguments pass into function. Function overloading can be implementing in non-member function as well as member function of class.
Example 1
Example of non-member function based function overloading according to different types of arguments is given below:
#include <iostream>
using namespace std;
void printVal(int A);
void printVal(char A);
void printVal(float A);
int main()
{
printVal(10 );
printVal('@' );
printVal(3.14f );
return 0;
}
void printVal(int A)
{
cout<< endl << "Value of A : "<< A;
}
void printVal(char A)
{
cout<< endl << "Value of A : "<< A;
}
void printVal(float A)
{
cout<< endl << "Value of A : "<< A;
}
Output
Value of A : 10
Value of A : @
Value of A : 3.14
Example 2
Example of member function of class based function overloading according to different types of arguments is given below:
#include <iostream>
using namespace std;
class funOver
{
public:
void printVal(int A);
void printVal(char A);
void printVal(float A);
};
void funOver::printVal(int A)
{
cout<< endl << "Value of A : "<< A;
}
void funOver::printVal(char A)
{
cout<< endl << "Value of A : "<< A;
}
void funOver::printVal(float A)
{
cout<< endl << "Value of A : "<< A;
}
int main()
{
funOver ob;
ob.printVal(10 );
ob.printVal('@' );
ob.printVal(3.14f );
return 0;
}
Output
Value of A : 10
Value of A : @
Value of A : 3.14