1// money.h -- define the prototype
2class Money
3{
4 public:
5 Money & operator += (const Money &rhs);
6}
7
8// money.cpp -- define the implementation
9Money& Money :: operator += (const Money &rhs)
10{
11 // Yadda Yadda
12
13 return *this;
14}
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}
1#include <iostream>
2#include <string>
3
4class Car
5{
6private:
7 std::string m_make;
8 std::string m_model;
9
10public:
11 Car(const std::string& make, const std::string& model)
12 : m_make{ make }, m_model{ model }
13 {
14 }
15
16 friend bool operator== (const Car &c1, const Car &c2);
17 friend bool operator!= (const Car &c1, const Car &c2);
18};
19
20bool operator== (const Car &c1, const Car &c2)
21{
22 return (c1.m_make== c2.m_make &&
23 c1.m_model== c2.m_model);
24}
25
26bool operator!= (const Car &c1, const Car &c2)
27{
28 return !(c1== c2);
29}
30
31int main()
32{
33 Car corolla{ "Toyota", "Corolla" };
34 Car camry{ "Toyota", "Camry" };
35
36 if (corolla == camry)
37 std::cout << "a Corolla and Camry are the same.\n";
38
39 if (corolla != camry)
40 std::cout << "a Corolla and Camry are not the same.\n";
41
42 return 0;
43}
1ostream &operator<<(ostream &output, const MyClass &myObject)
2{
3 output << "P : " << myObject.property;
4 return output;
5}
6
1inline bool operator==(const X& lhs, const X& rhs){ return cmp(lhs,rhs) == 0; }
2inline bool operator!=(const X& lhs, const X& rhs){ return cmp(lhs,rhs) != 0; }
3inline bool operator< (const X& lhs, const X& rhs){ return cmp(lhs,rhs) < 0; }
4inline bool operator> (const X& lhs, const X& rhs){ return cmp(lhs,rhs) > 0; }
5inline bool operator<=(const X& lhs, const X& rhs){ return cmp(lhs,rhs) <= 0; }
6inline bool operator>=(const X& lhs, const X& rhs){ return cmp(lhs,rhs) >= 0; }