-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotes_operator_overloading.cpp
70 lines (60 loc) · 2.14 KB
/
notes_operator_overloading.cpp
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
#include <iostream>
using namespace std;
/** OPERATOR OVERLOADING
* Rational Operators: + - / *
* Relational Operators: > < <= >= == !=
* Assignment & Compound Operators: = += -= *= /=
* Subscript Operator: [] // treat class like array
* You can overload overloaded operators.
* Try to keep overload functions relevant, and don't use unnecessarily.
* SYNTAX: Functions with 'operator' keyword in name, ex:
* void operator*(double y); // Name of function: operator*
* bool operator<(Vector3& rhs); // Function name: operator<
* SomeObject& operator[](int index); // Function name: operator[]
* Object& operator=(const Object& m); // Function name: operator=
* Used to customize use of classes (can also overload regular functions, not covered in class).
* Similar to concept of overloading functions (multiple different constructors for different parameters).
* Shorthand for functions.
*/
/**
// TRADITIONAL VS. OVERLOADED FUNCTION
class Values {
public:
int a, b, c;
Values(int a, int b, int c) : a(a), b(b), c(c) {}
};
// Traditional Function | Name = AddDigits
int AddDigits(int hundreds, int tens, int ones) {return (100 * hundreds) + (10 * tens) + ones;}
// to call: sum = AddDigits(valuesObject.a, ValuesObject.b, ValuesObject.c);
// Overloaded + Operator Function | Name = operator+
int operator+(Values valuesObject) {return (100 * valuesObject.a) + (10 * valuesObject.b) + valuesObject.c;}
// to call: sum = +valuesObject1;
// OVERLOAD OPERATOR WITHIN CLASS: MODIFYING ATTRIBUTES
class Floaties {
float x, y, z;
public:
Floaties() {x = y = z = 0;}
void Display() {cout << x << " " << y << " " << z << endl;}
Floaties& operator+=(Floaties) {
x += 0.50;
y += 0.50;
z += 0.50;
return *this;
}
};
int main() {
// TRADITIONAL VS. OVERLOADED FUNCTION
Values set1(4, 8, 9);
int sum;
sum = AddDigits(set1.a, set1.b, set1.c);
cout << sum << endl;
sum = +set1;
cout << sum << endl;
// OVERLOAD OPERATOR WITHIN CLASS: MODIFYING ATTRIBUTES
Floaties floaties;
floaties.Display();
floaties += floaties;
floaties.Display();
return 0;
}
*/