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.

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 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 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

@@@@@@@@@@
************





Was this page helpful? YES NO

Are you a blogger? Join our Blogging forum.



Comments and Discussions


We are using Google to publish ads on our website; Google has its own privacy policies. They may save log, cookies on your system. Google may also collect information of your system like IP address, region, city, country. For more details please go through the Google’s privacy policy.