C++ program to demonstrate example of function overloading

Learn about the function overloading in C++, how to implement function overloading?
[Last updated : March 01, 2023]

Function Overloading in C++

Function overloading is an important feature in C++, using function overloading – in a block/ scope we can declare multiple functions with same name. Function overloading can be done by using different type and number of arguments; it does not depend on return type of the function.

Function Overloading Example in C++

/*C++ program to demonstrate example of function overloading.*/
#include  <iostream>
using namespace std;
 
void printChar();
void printChar( char c );
void printChar( char c, int num );
void printChar(int num, char c);
 
int main()
{
    printChar();
    printChar('#');
    printChar(10,'$');
    printChar('@',10);
     
    cout<< endl;
         
    return 0;
}
 
void printChar()
{
        cout<< endl<<"%";
}
void printChar( char c )
{
        cout<< endl<< c;
}
void printChar( char c, int num )
{
    int i=0;
         
    cout<< endl;
    for(i=0; i< num; i++)
        cout<< c;
}
void printChar(int num, char c)
{
    int i=0;
         
    cout<< endl;
    for(i=0; i< num; i++)
        cout<< c;
}

Output

    %
    #
    $$$$$$$$$$
    @@@@@@@@@@





Comments and Discussions!

Load comments ↻






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