1#include <iostream>
2
3class ExampleClass {
4 public:
5 ExampleClass() {}
6 ExampleClass(int ex) {
7 example_ = 0;
8 }
9 int& example() { return example_; }
10 const int& example() const { return example_; }
11 //Overload the "+" Operator
12 ExampleClass operator+ (const ExampleClass& second_object_of_class) {
13 ExampleClass object_of_class;
14 object_of_class.example() = this -> example() + second_object_of_class.example();
15 return object_of_class;
16 }
17 private:
18 int example_;
19};
20
21int main() {
22 ExampleClass c1, c2;
23 c1.example() = 1;
24 c2.example() = 2;
25 ExampleClass c3 = c1 + c2;
26 //Calls operator+() of c1 with c2 as second_object_of_class
27 //c3 gets set to object_of_class
28 std::cout << c3.example();
29}
1ostream &operator<<(ostream &output, const MyClass &myObject)
2{
3 output << "P : " << myObject.property;
4 return output;
5}
6
1struct Foo
2{
3 double val;
4 friend bool operator<(const Foo& l, const Foo& r)
5 {
6 //Custom comparison for l < r goes here
7 return l.val < r.val;
8 }
9};
1class Point
2{
3public:
4 Point& operator++() { ... } // prefix
5 Point operator++(int) { ... } // postfix
6 friend Point& operator++(Point &p); // friend prefix
7 friend Point operator++(Point &p, int); // friend postfix
8 // in Microsoft Docs written "friend Point& operator++(Point &p, int);"
9};
10