C++ Operator overloading example

Photo by Albert S on Unsplash

C++ Operator overloading example

Simplest form here:

# Simple reference for overloading '+' opertator here:

#include <iostream>
using namespace std;
struct Test
{
  int i;
  Test (){}// Default constructor
  Test (int i):i (i){ }    //Initializer list constructor

  //Essense
  Test operator+ (const Test & object)
  {
    Test final_object;
    final_object.i = i + object.i;
    return final_object;
  }
};

int main ()
{
  Test a (1);
  Test b (2);
  Test c;
  c = a + b;
  cout << c.i << endl;
  return 0;
}