Demonstrate function overloading - C++ Function

C++ examples for Function:Function Overload

Description

Demonstrate function overloading

Demo Code

#include <iostream>
using namespace std;
void repchar();             //declarations
void repchar(char);
void repchar(char, int);
int main()/*  www .  jav  a2  s.co m*/
{
   repchar();
   repchar('=');
   repchar('+', 30);
   return 0;
}
// displays 45 asterisks
void repchar()
{
   for(int j=0; j<45; j++)  // always loops 45 times
      cout << '*';          // always prints asterisk
   cout << endl;
}
// displays 45 copies of specified character
void repchar(char ch)
{
   for(int j=0; j<45; j++)  // always loops 45 times
      cout << ch;           // prints specified character
   cout << endl;
}
// displays specified number of copies of specified character
void repchar(char ch, int n)
{
   for(int j=0; j<n; j++)   // loops n times
      cout << ch;           // prints specified character
   cout << endl;
}

Result


Related Tutorials