Default Arguments vs. Overloading : function overload « Function « C++ Tutorial






#include <iostream>
#include <cstring>
using namespace std;
   
void mystrcat(char *s1, char *s2, int len = -1);
   
int main()
{
  char str1[80] = "This is a test";
  char str2[80] = "0123456789";
   
  mystrcat(str1, str2, 5); // concatenate 5 chars
  cout << str1 << '\n';
   
  strcpy(str1, "This is a test"); // reset str1
   
  mystrcat(str1, str2); // concatenate entire string
  cout << str1 << '\n';
   
  return 0;
}
   
// A custom version of strcat().
void mystrcat(char *s1, char *s2, int len)
{
  // find end of s1
  while(*s1) s1++;
   
  if(len == -1) len = strlen(s2);
   
  while(*s2 && len) {
    *s1 = *s2; // copy chars
    s1++; 
    s2++;
    len--;
  }
 
  *s1 = '\0'; // null terminate s1
}








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