Week 3 Lab02
WEDNESDAY 4PM - 6PM
THURSDAY 4PM - 6PM
Slides by Alvin Cherk (z5311001)
These slides are based upon the lab document on Teams or on GitLab.com
Floating point value representation in any computer language has some sort of error.
Sometimes, operations on floating point values may result in some sort of error, as such, when comparing the values of doubles, we cannot compare them directly.
This is where epsilon testing comes in.
The idea is that we take the absolute value of the difference of the two numbers we are comparing.
If this difference is some really small number (near 0), then we say the values are equal.
epslion
difference of the two values we are comparing
Operator overloads allow you to decrease your code complexity and utilise well defined semantics
Only create an overload if your type has a single, obvious meaning to an operator
Types | Operators | Member / Friend |
---|---|---|
I/O | <<, >> | Friend |
Arithmetic | +, -, *, / | Friend |
Relational, Equality | >, <, >=, <=, ==,!= | Friend |
Assignment | = | member (non-const) |
Compound assignment | +=, -=, *=, /= | member (non-const |
Subscript | [] | member (const & non-const) |
Increment/Decrement | ++, -- | member (non-const) |
Arrow, Defrefernece | ->, * | member (const & non-const) |
A friend operator operates on two instances whereas a member operator operates on itself and brings in something else.
When should one use these different function types?
When writing a class that manages heap resources, we have to consider the "rule of 5"
*Apparently you didn't need to learn this, but its good to know & you are implementing it all in the assignment anyway
SharedPointer.cpp Continuation
Implement a shared pointer to an int
in a class called SharedPointer
. The specification for SharedPointer
is given below:
or look at the pdf on teams
Complex.cpp
Create a Complex
class that models a complex number and implement various member functions and operator overloads.
Robot.cpp