overloading functions c++ code example
Example 1: c++ function overload
/*A function over load in c++ is when you take a function with the same definition but change the input variables.
This can include multiple functions with the same name see below
*/
#include <iostream>
using namespace std;
int function1(int var1){//example of single variable
//do somthing
}
int function1(int var1,int var2){//of overload
//do somthing
}
int function1(int var1,string var3){//of overload
//do somthing
}
int main(){
cout << "Hello World" << endl;
function1(4);
function1(3,-90);
function1(34,"it works");//these should all work even tho they have different input variables
return 0;
}
Example 2: operator ++ overloading c++
class Point
{
public:
Point& operator++() { ... } // prefix
Point operator++(int) { ... } // postfix
friend Point& operator++(Point &p); // friend prefix
friend Point operator++(Point &p, int); // friend postfix
// in Microsoft Docs written "friend Point& operator++(Point &p, int);"
};