Home »
C++ programming language
Function overloading example based on Different Order of Arguments in C++
Learn: How to implement function overloading based on Different Order 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 order of arguments in C++
We can implement function overloading on the basis of different order 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 order of arguments is given below:
#include <iostream>
using namespace std;
void printChar(int num, char ch);
void printChar(char ch , int num);
int main()
{
printChar(10, '@');
printChar('*', 12);
return 0;
}
void printChar(int num, char ch)
{
int i=0;
cout<<endl;
for(i=0;i<num;i++)
cout<<ch;
}
void printChar(char ch, int num)
{
int i=0;
cout<<endl;
for(i=0;i<num;i++)
cout<<ch;
}
Output
@@@@@@@@@@
************
Example 2
Example of member function of class based function overloading according to different order of arguments is given below:
#include <iostream>
using namespace std;
class funOver
{
public:
void printChar(int num, char ch);
void printChar(char ch , int num);
};
void funOver::printChar(int num, char ch)
{
int i=0;
cout<<endl;
for(i=0;i<num;i++)
cout<<ch;
}
void funOver::printChar(char ch, int num)
{
int i=0;
cout<<endl;
for(i=0;i<num;i++)
cout<<ch;
}
int main()
{
funOver ob;
ob.printChar(10, '@');
ob.printChar('*', 12);
return 0;
}
Output
@@@@@@@@@@
************