forked from portfoliocourses/cplusplus-example-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoperator_overloading.cpp
More file actions
90 lines (75 loc) · 2.9 KB
/
operator_overloading.cpp
File metadata and controls
90 lines (75 loc) · 2.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*******************************************************************************
*
* Program: Operator Overloading Example
*
* Description: Examples of operator overloading in C++.
*
* YouTube Lesson: https://www.youtube.com/watch?v=9tHu4mWtrnM
*
* Author: Kevin Browne @ https://portfoliocourses.com
*
*******************************************************************************/
#include <iostream>
using namespace std;
// Define a simple Number class
class Number
{
public:
int n;
Number(int set_n)
{
n = set_n;
}
// Overload the + operator, the function header must follow a particular
// format, and the exact format depends on the operator. The left operand
// of the + operator will become the "this" object for which this member
// function is being called, and the right operand of the + operator will
// be provided as an argument to the function. The function is expected
// to return a Number object.
Number operator+(const Number &numA)
{
// create and return a new Number object with it's n member variable
// initialized to the sum of the two operand object's n member variables
return Number(this->n + numA.n);
// We *could* provide a definition of the overloaded operator that does not
// make sense, such as always returning a Number object with an n member
// variable value of 0. But we should not do this as it will make our
// program more difficult to understand, we should try to provide
// definitions that make sense given the operator's standard purpose.
//
// return Number(0);
}
// Overload the equality operator ==, which is expected to return a bool
// (given what the equality operator does, this makes sense). Again the
// left operand of the equality operator will be the "this" object for which
// this member function is called, and the right operand of the equality
// operator will be provided as an argument to this function.
bool operator==(const Number &numA)
{
// return true if the n member variable's of the two objets are equal,
// and false otherwise
if (this->n == numA.n) return true;
else return false;
}
};
int main()
{
// Create two number objects
Number a(5);
Number b(10);
// if we didn't overload the + operator, this would cause an error, but
// instead our member function runs and c is set to the return value
// of that function ('a' becomes the 'this object', and 'b' becomes the
// parameter object).
Number c = a + b;
// output the result, we will get c.n: 15
cout << "c.n: " << c.n << endl;
// test the overloaded equality operator (case where two objects aren't equal)
if (a == c) cout << "a == c" << endl;
else cout << "a != c" << endl;
// test the overloaded equality operator (case where two objects are equal)
Number d(15);
if (c == d) cout << "c == d" << endl;
else cout << "c != d" << endl;
return 0;
}