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.

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





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.