Overload function to accept integer or char * argument : Function Overloaded « Function « C++






Overload function to accept integer or char * argument

Overload function to accept integer or char * argument
 

#include <iostream>
using namespace std;

void sleep(int n);
void sleep(char *n);

#define DELAY 100000

int main()
{
  cout << '.';
  sleep(3);
  cout << '.';
  sleep("2");
  cout << '.';

  return 0;
}

// Sleep() with integer argument.
void sleep(int n)
{
  long i;

  for( ; n; n--)
    for(i = 0; i <DELAY; i++) ;
}

// Sleep() with char * argument.
void sleep(char *n)
{
  long i;
  int j;

  j = atoi(n);

  for( ; j; j--)
    for(i = 0; i <DELAY; i++) ;
}

           
         
  








Related examples in the same category

1.Functions differ in number of parametersFunctions differ in number of parameters
2.Overload abs() three waysOverload abs() three ways
3.Overload function: int and longOverload function: int and long
4.Create three functions called prompt( ) that perform this task for data of types int, double, and long
5.Overload the min() function.Overload the min() function.
6.Compute area of a rectangle using overloaded functions.Compute area of a rectangle using overloaded functions.
7.Overload function: int and floatOverload function: int and float
8.function overloading between int and string type