MTRN2500

Week 3 Lab02

WEDNESDAY 4PM - 6PM

THURSDAY 4PM - 6PM
 

Slides by Alvin Cherk (z5311001)

Info

These slides are based upon the lab document on Teams or on GitLab.com

Today

  • Epsilon Testing (Assignment)
  • More constructors i.e. copy and move constructors, copy and move assignment operators.
  • Differences between methods, static methods, and friend functions.
  • Operator overloading.

Epsilon Testing

Epsilon Testing

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.

Epsilon Testing

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 Overloading

Operator Overloading

Operator overloads allow you to decrease your code complexity and utilise well defined semantics

  • C++ supports a rich set of operator overloads
  • All operator overloads must have at least one operand of its type
  • Advantages:
    • Readability & reuse existing code semantics
    • Save memory space, consistency, readability
    • Flexible and easy to maintain
    • No verbosity required for simple operations
  • Disadvantages:
    • Lack of context on operators

 

Only create an overload if your type has a single, obvious meaning to an operator

Operator Overloading

Operator Overloading

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)

Friends

Friends

A friend operator operates on two instances whereas a member operator operates on itself and brings in something else.

  • Use friends when:
    • The data should be available to everyone
    • There is a piece of code very related to this particular class

Static vs Friends

When should one use these different function types?

  • We should prefer methods by default since this provides the best security for member variables of an object.
  • We should prefer static methods only if we can say yes to the following methods:
    • Is this method closely related to the class it's declared in? And,
    • Does it make sense to call this method when there's no object?
  • We should prefer friend functions:
    • For specific operator overloads.
    • If our friend function wants to be friends with more than 1 class.

Rule of 5

Rule of 5

When writing a class that manages heap resources, we have to consider the "rule of 5"

  1. Destructor
  2. Copy constructor
  3. Copy assignment
  4. Move assignment
  5. Move constructor

*Apparently you didn't need to learn this, but its good to know & you are implementing it all in the assignment anyway

Code Demo

SharedPointer.cpp Continuation

Code Demo

Implement a shared pointer to an int in a class called SharedPointer. The specification for SharedPointer is given below:

Spec link here

or look at the pdf on teams

Code Demo

Complex.cpp

Code Demo

Create a Complex class that models a complex number and implement various member functions and operator overloads.

 

See spec here

Code Demo

Robot.cpp

Code Demo

Model a Robot based on the spec

 

See spec here

Feedback