Create overloaded print() and println() functions that display various types of data : function overload « Function « C++ Tutorial






#include <iostream> 
using namespace std; 
 
void println(bool b); 
void println(int i); 
void println(long i); 
void println(char ch); 
void println(char *str); 
void println(double d); 
 
void print(bool b); 
void print(int i); 
void print(long i); 
void print(char ch); 
void print(char *str); 
void print(double d); 
 
int main() 
{ 
  println(true); 
  println(1); 
  println("test"); 
  println('x'); 
  println(9L); 
  println(1.23); 
 
  print("string"); 
  print(false); 
  print(' '); 
  print(8); 
  print(' '); 
  print(100L); 
  print(' '); 
  print(1.01); 
 
  println(" Done!"); 
 
  return 0; 
} 
 
void println(bool b) 
{ 
  if(b) 
    cout << "true\n"; 
  else 
    cout << "false\n"; 
} 
 
void println(int i) 
{ 
  cout << i << "\n"; 
} 
 
void println(long i) 
{ 
  cout << i << "\n"; 
} 
 
void println(char ch) 
{ 
  cout << ch << "\n"; 
} 
 
void println(char *str) 
{ 
  cout << str << "\n"; 
} 
 
void println(double d) 
{ 
  cout << d << "\n"; 
} 
 
void print(bool b) 
{ 
  if(b) 
     cout << "true"; 
  else 
     cout << "false"; 
} 
 
void print(int i) 
{ 
  cout << i; 
} 
 
void print(long i) 
{ 
  cout << i; 
} 
 
void print(char ch) 
{ 
  cout << ch; 
} 
 
void print(char *str) 
{ 
  cout << str; 
} 
 
void print(double d) 
{ 
  cout << d; 
}
true
1
test
x
9
1.23
stringfalse 8 100 1.01 Done!








7.12.function overload
7.12.1.Overload a function three times.
7.12.2.Overload function by parameter type: int, double and long
7.12.3.Overload functions with two parameters
7.12.4.Overloading functions with difference in number of parameters
7.12.5.Create overloaded print() and println() functions that display various types of data
7.12.6.Overload function with array type
7.12.7.Overloading a function with const reference parameters
7.12.8.Overloading a function with reference parameters
7.12.9.Default Arguments vs. Overloading
7.12.10.Finding the Address of an Overloaded Function