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

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

Related Tutorials



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.