You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Remove <=> and hidden friends from operators exercise.
This exercise is used both in the essentials and the advanced course. We
can therefore not require students to work with hidden friends and <=>.
Here, all mentions of <=> are removed (there is a dedicated exercise on
the third day of the advanced course), and the tasks are rephrased such
that students can work on this exercise also in the essentials course.
The diff between the original file and the solution is minimised, so
viewing the solution with a diff program hopefully helps the students.
- Add a free operator<<, reusing str(), and simplify main() first lines.
6
-
- Replace equal() with operator==(), and upgrade tests.
7
-
- Add operator!=(), reusing operator==(), and upgrade tests.
8
-
- Replace compare() with operator<=>(), reusing <=> between doubles,
9
-
and upgrade tests.
10
-
- Replace multiply() with operator*(), and upgrade tests.
4
+
### Main Tasks
5
+
- Write an ostream operator with the following signature: `operator<<(ostream &, Fraction const &)`.
6
+
The `str()` function of Fraction will help you to implement it. Use this operator to make the `cout`s in the first lines of `main()` look a bit more natural.
7
+
-**Note**: If you do this exercise as part of the advanced course, implement the operators as hidden friends.
8
+
- Replace the function `equal()` with `operator==()`, and upgrade tests.
9
+
Note that equality isn't the same as equivalence. The compare function returns 0
10
+
for 1/1, 2/2, etc, but these are not equal.
11
+
- Add `operator!=()`, reusing `operator==()`, and upgrade tests.
12
+
- Replace `multiply()` with `operator*()`, and upgrade tests.
11
13
12
-
STEP 2
13
-
- Replace TestResultPrinter::process() with operator()(), and upgrade CHECK().
14
-
15
-
OPTIONAL STEP 3
16
-
- Add an inplace multiplication operator*=(), and add tests.
17
-
- Review operator*() so to reuse operator*=().
18
-
- Ensure calls to operator*=() can be chained, the same as operator<<().
14
+
### Optional if you have time
15
+
- Add an inplace multiplication `operator*=()`, and add tests.
16
+
- Review `operator*()` so to reuse `operator*=()`.
17
+
- Ensure calls to `operator*=()` can be chained, the same as `operator<<()`.
19
18
20
19
## Take away
21
20
21
+
- Operators can make certain expressions much more readable.
22
22
- Do not confuse equality and equivalence.
23
23
- We can very often implement an arithmetic operator@ in terms of operator@=.
24
-
- When implementing <=>, you get <, >, <=, >= for free.
25
-
- Object-functions are very used with standard algorithms,
26
-
yet tend to be often replaced by lambdas in modern C++.
0 commit comments