Teaching Notes: Module 02
Key Learning Objectives
Section titled “Key Learning Objectives”- Orthodox Canonical Form (OCF)
- Copy constructor vs assignment operator
- Operator overloading
- Fixed-point number representation
Exercise 00: Orthodox Canonical Form
Section titled “Exercise 00: Orthodox Canonical Form”What It Tests
Section titled “What It Tests”- Default constructor
- Copy constructor
- Copy assignment operator
- Destructor
- Basic getters/setters
Common Mistakes
Section titled “Common Mistakes”-
Confusing copy constructor and assignment
Fixed a;Fixed b(a); // Copy constructorFixed c = a; // Also copy constructor (initialization)!Fixed d;d = a; // Assignment operator (d already exists) -
Missing self-assignment check
Fixed& operator=(const Fixed& other) {// Without this check, a = a; would be buggyif (this != &other) {_rawBits = other._rawBits;}return *this;} -
*Not returning this from assignment
- Needed for chaining:
a = b = c;
- Needed for chaining:
Guiding Questions
Section titled “Guiding Questions”- “When does the copy constructor get called vs the assignment operator?”
- “Why do we return
*thisfrom the assignment operator?” - “What could go wrong if we don’t check for self-assignment?”
Exercise 01: Constructors and Conversions
Section titled “Exercise 01: Constructors and Conversions”What It Tests
Section titled “What It Tests”- Converting from int/float to fixed-point
- Converting from fixed-point to int/float
- Stream insertion operator (<<)
The Math
Section titled “The Math”Fixed-point with 8 fractional bits:- Integer part: bits 31-8- Fractional part: bits 7-0
To convert:int -> fixed: value << 8fixed -> int: value >> 8float -> fixed: roundf(value * 256)fixed -> float: value / 256.0fCommon Mistakes
Section titled “Common Mistakes”-
Wrong bit shifting direction
<<shifts left (multiplies by 2^n)>>shifts right (divides by 2^n)
-
Forgetting to use roundf()
- Float to fixed needs rounding for accuracy
-
Integer division instead of float
float toFloat() const {return _rawBits / 256; // WRONG - integer division!return _rawBits / 256.0f; // CORRECT}
Guiding Questions
Section titled “Guiding Questions”- “What does shifting left by 8 bits do mathematically?”
- “Why do we need roundf() when converting from float?”
- “How many values can 8 fractional bits represent between 0 and 1?”
Exercise 02: Operator Overloading
Section titled “Exercise 02: Operator Overloading”What It Tests
Section titled “What It Tests”- Comparison operators
- Arithmetic operators
- Increment/decrement operators
- Static member functions
Pre vs Post Increment Pattern
Section titled “Pre vs Post Increment Pattern”// Pre-increment: ++a (returns new value)Fixed& operator++() { _rawBits++; return *this;}
// Post-increment: a++ (returns old value)Fixed operator++(int) { // int is just a marker Fixed temp(*this); // Save current _rawBits++; // Increment return temp; // Return old}Common Mistakes
Section titled “Common Mistakes”-
Return types for pre vs post increment
- Pre: returns reference (efficient, can chain)
- Post: returns copy (must, because returning old value)
-
Epsilon value
- Smallest representable value is 1 in raw bits
1 / 256.0f = 0.00390625
-
Arithmetic overflow
- Multiplication:
(a * b) >> 8 - Division:
(a << 8) / b
- Multiplication:
Guiding Questions
Section titled “Guiding Questions”- “Why does post-increment need to return a copy?”
- “What’s the smallest value you can add to a fixed-point number?”
- “How do you multiply two fixed-point numbers correctly?”
Exercise 03: BSP (Binary Space Partitioning)
Section titled “Exercise 03: BSP (Binary Space Partitioning)”What It Tests
Section titled “What It Tests”- Using the Fixed class in practice
- Point-in-triangle algorithm
- const correctness
Algorithm (Cross Product Method)
Section titled “Algorithm (Cross Product Method)”For triangle ABC and point P:1. Compute cross product signs for: - AB x AP - BC x BP - CA x CP2. If all same sign (or zero) -> inside3. If on edge (cross = 0) -> return false (per subject)Common Mistakes
Section titled “Common Mistakes”-
Returning true for points on edges
- Subject says: on edge or vertex = false
-
Not handling const Fixed
- Point attributes are
Fixed const
- Point attributes are
Guiding Questions
Section titled “Guiding Questions”- “What does the cross product tell us about which side of a line a point is on?”
- “Why might the subject want points on edges to return false?”
General Tips for Module 02
Section titled “General Tips for Module 02”OCF Checklist
Section titled “OCF Checklist”For every class from now on:
- Default constructor
- Copy constructor
- Copy assignment operator (with self-assignment check)
- Destructor
Operator Overloading Guidelines
Section titled “Operator Overloading Guidelines”| Operator | Member? | Return Type |
|---|---|---|
= | Must | T& |
+, -, *, / | Usually | T |
==, !=, <, > | Either | bool |
++, -- (pre) | Must | T& |
++, -- (post) | Must | T |
<< | Cannot | ostream& |
Common Conceptual Struggles
Section titled “Common Conceptual Struggles”-
“Why is << not a member function?”
- Because
ostreamis on the left side - We can’t modify
ostreamclass
- Because
-
“What’s the difference between copy constructor and assignment?”
- Copy constructor: creates new object
- Assignment: modifies existing object
-
“Why Fixed-point instead of float?”
- Predictable precision
- No floating-point rounding errors
- Faster on some hardware