Skip to content

Commit 6754621

Browse files
committed
Operator Overloading updated
1 parent c66e461 commit 6754621

File tree

4 files changed

+239
-51
lines changed

4 files changed

+239
-51
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ This change ensures that VSCode uses the "Ninja Multi-Config" generator by defau
241241
* [Header Guard](src/class/header_guard)
242242
* [Inheritance, Inheritance Prevention (final)](src/class/inheritance.cpp)
243243
* [Multiple Inheritance Polymorphism](src/class/multiple_inheritance_polymorphism.cpp)
244-
* [Operator Overloading](src/class/operator_overloading.cpp)
244+
* [Operator Overloading](docs/operator_overloading.md)
245245
* [Object Slicing](src/class/object_slicing.cpp)
246246
* [Passing Arguments To Parent Constructor](src/class/passing_arg_to_parent_constructor.cpp)
247247
* [Private Public Protected Inheritance](src/class/private_public_protected_inheritance.cpp)

docs/operator_overloading.md

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
# Operators Overloading
2+
3+
Any of the following 38 (until C++20)40 (since C++20) operators:
4+
```
5+
+ - * / % ^ & |
6+
~ ! = < > += -=
7+
*= /= %= ^= &= |= << >> >>= <<= == != <= >= <=> (since C++20) && || ++ -- , ->*
8+
-> ( ) [ ]
9+
co_await (since C++20)
10+
```
11+
12+
In this example we gonna overload the following operators:
13+
```cpp
14+
()
15+
-
16+
<<
17+
>>
18+
= deep copy (Copy constructor, Assignment operator)
19+
```
20+
21+
Our class money:
22+
23+
```cpp
24+
class money {
25+
private:
26+
int m_size = 10;
27+
28+
public:
29+
int m_value;
30+
int *data;
31+
32+
money(int value = 10) : m_value(value) {
33+
std::cout << "Constructor money with m_value: " << m_value << std::endl;
34+
35+
data = new int[m_size];
36+
37+
int upperBound = 20;
38+
int lowerBound = 0;
39+
int numberOfRandomNumbers = m_size;
40+
41+
std::random_device rd;
42+
std::mt19937 gen(rd());
43+
std::uniform_int_distribution<> dis(lowerBound, upperBound);
44+
45+
for (int n = 0; n < numberOfRandomNumbers; ++n)
46+
data[n] = dis(gen);
47+
}
48+
49+
~money() {
50+
std::cout << "Destructor money with value: " << m_value << std::endl;
51+
delete[] data;
52+
}
53+
54+
double operator()() { return m_value; }
55+
56+
friend money operator-(const money &t1, int m_value);
57+
friend std::ostream &operator<<(std::ostream &os, const money &t);
58+
friend std::istream &operator>>(std::istream &is, const money &t);
59+
60+
bool operator<(const money &other) const { return m_value < other.m_value; }
61+
62+
money operator-(const money &other) {
63+
m_value - other.m_value;
64+
return *this;
65+
}
66+
67+
// Copy constructor for deep copy
68+
money(const money &other) : m_value(other.m_value), m_size(other.m_size) {
69+
data = new int[m_size];
70+
std::copy(other.data, other.data + m_size, data);
71+
}
72+
73+
// Copy assignment operator for deep copy
74+
money &operator=(const money &other) {
75+
if (this != &other) {
76+
delete[] data;
77+
78+
m_value = other.m_value;
79+
m_size = other.m_size;
80+
data = new int[m_size];
81+
std::copy(other.data, other.data + m_size, data);
82+
}
83+
return *this;
84+
}
85+
};
86+
```
87+
88+
89+
### () operator overloading
90+
91+
```cpp
92+
double operator()() { return value; }
93+
```
94+
95+
96+
### < operator overloading
97+
98+
```cpp
99+
bool operator<(const money &other) const { return value < other.value; }
100+
101+
money operator-(const money &other) {
102+
value - other.value;
103+
return *this;
104+
}
105+
```
106+
107+
### Implementations of the < operator inside or outside of the class
108+
109+
inside of the class:
110+
```cpp
111+
struct cell {
112+
int index;
113+
float cost;
114+
bool operator<(const cell &otherside) { return cost < otherside.cost; }
115+
};
116+
```
117+
118+
ouside of the class:
119+
120+
```cpp
121+
bool operator<(const cell &lhs, const cell &rhs) { return lhs.cost < rhs.cost; }
122+
```
123+
124+
125+
### - operator overloading
126+
127+
```cpp
128+
friend money operator-(const money &t1, int value);
129+
money operator-(const money &t1, int value) { return money(t1.value - value); }
130+
```
131+
132+
### << operator overloading
133+
134+
```cpp
135+
friend std::ostream &operator<<(std::ostream &os, const money &t);
136+
std::ostream &operator<<(std::ostream &os, const money &t) {
137+
for (int i = 0; i < t.size; i++)
138+
os << t.data[i] << " ";
139+
return os;
140+
}
141+
```
142+
143+
### >> operator overloading
144+
145+
146+
```cpp
147+
friend std::istream &operator>>(std::istream &is, const money &t);
148+
std::istream &operator>>(std::istream &is, const money &t) {
149+
is >> t.value;
150+
return is;
151+
}
152+
```
153+
154+
now we can use it as:
155+
156+
```cpp
157+
money money3;
158+
std::cout << "Enter the size: " << std::endl;
159+
160+
std::cin >> money3;
161+
std::cout << "The operator >> gives you:" << money3 << std::endl;
162+
163+
money money1(7), money2(3);
164+
std::cout << "The operator - gives you:" << std::endl;
165+
std::cout << money2 - money1 << std::endl;
166+
std::cout << "The operator () gives you:\n" << money1() << std::endl;
167+
std::cout << "The operator () << gives you:\n" << money1 << std::endl;
168+
169+
```
170+
171+
172+
173+
174+
[code](../src/class/operator_overloading.cpp)

src/class/operator_overloading.cpp

Lines changed: 51 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,23 @@
1-
/*
2-
3-
In this example we gonna overload the following operators:
4-
()
5-
-
6-
<<
7-
>>
8-
= deep copy (Copy constructor, Assignment operator)
9-
*/
101
#include <iostream>
112
#include <istream>
123
#include <random>
134

14-
/*
15-
any of the following 38 (until C++20)40 (since C++20) operators:+ - * / % ^ & |
16-
~ ! = < > += -=
17-
*= /= %= ^= &= |= << >> >>= <<= == != <= >= <=> (since C++20) && || ++ -- , ->*
18-
-> ( ) [ ]
19-
* co_await (since C++20)
20-
*/
21-
225
class money {
236
private:
24-
int size = 10;
7+
int m_size = 10;
258

269
public:
27-
int value;
10+
int m_value;
2811
int *data;
2912

30-
money(int value = 10) {
31-
this->value = value;
32-
std::cout << "Constructor money with value: " << this->value << std::endl;
13+
money(int value = 10) : m_value(value) {
14+
std::cout << "Constructor money with m_value: " << m_value << std::endl;
3315

34-
data = new int[size];
16+
data = new int[m_size];
3517

3618
int upperBound = 20;
3719
int lowerBound = 0;
38-
int numberOfRandomNumbers = size;
20+
int numberOfRandomNumbers = m_size;
3921

4022
std::random_device rd;
4123
std::mt19937 gen(rd());
@@ -46,59 +28,78 @@ class money {
4628
}
4729

4830
~money() {
49-
std::cout << "Destructor money with value: " << this->value << std::endl;
31+
std::cout << "Destructor money with value: " << m_value << std::endl;
5032
delete[] data;
5133
}
5234

53-
double operator()() { return value; }
35+
double operator()() { return m_value; }
5436

55-
friend money operator-(const money &t1, int value);
37+
friend money operator-(const money &t1, int m_value);
5638
friend std::ostream &operator<<(std::ostream &os, const money &t);
5739
friend std::istream &operator>>(std::istream &is, const money &t);
5840

59-
bool operator<(const money &other) const { return value < other.value; }
41+
bool operator<(const money &other) const { return m_value < other.m_value; }
6042

6143
money operator-(const money &other) {
62-
value - other.value;
44+
m_value - other.m_value;
45+
return *this;
46+
}
47+
48+
// Copy constructor for deep copy
49+
money(const money &other) : m_value(other.m_value), m_size(other.m_size) {
50+
data = new int[m_size];
51+
std::copy(other.data, other.data + m_size, data);
52+
}
53+
54+
// Copy assignment operator for deep copy
55+
money &operator=(const money &other) {
56+
if (this != &other) {
57+
delete[] data;
58+
59+
m_value = other.m_value;
60+
m_size = other.m_size;
61+
data = new int[m_size];
62+
std::copy(other.data, other.data + m_size, data);
63+
}
6364
return *this;
6465
}
6566
};
6667

67-
money operator-(const money &t1, int value) { return money(t1.value - value); }
68+
money operator-(const money &t1, int m_value) {
69+
return money(t1.m_value - m_value);
70+
}
6871

6972
std::ostream &operator<<(std::ostream &os, const money &t) {
70-
for (int i = 0; i < t.size; i++)
73+
for (int i = 0; i < t.m_size; i++)
7174
os << t.data[i] << " ";
7275
return os;
7376
}
7477

75-
std::istream &operator>>(std::istream &is, const money &t) {
76-
is >> t.value;
78+
std::istream &operator>>(std::istream &is, money &t) {
79+
is >> t.m_value;
7780
return is;
7881
}
7982

83+
struct cell {
84+
int index;
85+
float cost;
86+
// bool operator<(const cell &otherside) { return cost < otherside.cost; }
87+
};
88+
89+
bool operator<(const cell &lhs, const cell &rhs) { return lhs.cost < rhs.cost; }
90+
8091
int main() {
8192

82-
////////////////////////// - operator overloading ///////////////////////
93+
money money3;
94+
std::cout << "Enter the size: " << std::endl;
95+
96+
// getchar();
97+
std::cin >> money3;
98+
std::cout << "The operator >> gives you:" << money3 << std::endl;
8399

84100
money money1(7), money2(3);
85101
std::cout << "The operator - gives you:" << std::endl;
86102
std::cout << money2 - money1 << std::endl;
87-
88-
////////////////////////// < operator overloading ///////////////////////
89-
90-
std::cout << (money2 < money1) << std::endl;
91-
92-
////////////////////////// () operator overloading ///////////////////////
93-
94-
std::cout << "The operator () gives you:\n" << money1() << std::endl;
95-
96-
//////////////////////// << operator overloading ///////////////////////
97-
103+
std::cout << "The operator () gives you:\n" << money1() << std::endl;
98104
std::cout << "The operator () << gives you:\n" << money1 << std::endl;
99-
100-
////////////////////// >> operator overloading ///////////////////////
101-
// getchar();
102-
// std::cin>> money1;
103-
// std::cout<< "The operator >> gives you:"<< money1<<std::endl;
104105
}

src/main.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,14 @@ class Solution {
147147
}
148148
};
149149

150+
struct cell {
151+
int index;
152+
float cost;
153+
// bool operator<(const cell &otherside) { return cost < otherside.cost; }
154+
};
155+
156+
bool operator<(const cell &lhs, const cell &rhs) { return lhs.cost < rhs.cost; }
157+
150158
int main() {
151159
// double number = 3.1914;
152160

@@ -220,4 +228,9 @@ int main() {
220228
s.removeDuplicates(nums);
221229

222230
// std::transform()
231+
232+
cell c1{1, 5};
233+
cell c2{2, 3};
234+
// std::boolalpha <<
235+
std::cout << std::boolalpha << (c1 < c2) << std::endl;
223236
}

0 commit comments

Comments
 (0)