⚙️Tutorial: C++ with Classes 2

A Practical Tutorial on Operators
Section titled “A Practical Tutorial on Operators”Welcome to the fifth in our C++ tutorial series. This tutorial is designed to accompany the second half of the “C++ with Classes” chapter. The questions are practical and aim to reinforce the concepts of const correctness, operator overloading, and memory management in C++.
This tutorial is designed to be a hands-on experience. The following 10 questions will guide you through the process of writing the code, and the solutions and explanations in the video solutions will help you understand the “why” behind each implementation.
Good luck!
Question 1: const-Correctness
Section titled “Question 1: const-Correctness”Create a Vector2D class representing a 2D vector with x and y components (as float). Implement a getMagnitude() method that calculates and returns the magnitude of the vector
sqrt(x^2 + y^2)
. Ensure that this method can be called on const Vector2D objects.
Note: You will need to include <cmath> to use the sqrt() function.
Question 2: Function Overloading
Section titled “Question 2: Function Overloading”Extend the Vector2D class with two overloaded add methods. One method should take another Vector2D object and perform vector addition. The other should take a single float value and add it to both the x and y components of the vector.
Question 3: Operator Overloading (+)
Section titled “Question 3: Operator Overloading (+)”Modify the Vector2D class to overload the + operator for vector addition. The operator should take two Vector2D objects and return a new Vector2D object that is their sum.
Question 4: Operator Overloading
Section titled “Question 4: Operator Overloading”Overload the == operator for the Vector2D class to check if two vectors are equal (i.e., their x and y components are the same).
Question 5: Overloaded Constructors
Section titled “Question 5: Overloaded Constructors”Create a Particle class with x, y coordinates and velocityX, velocityY as float. Provide three constructors:
- A default constructor that initializes all values to
0.0f. - A constructor that takes
xandypositions and initializes velocities to0.0f. - A constructor that takes
x,y,velocityX, andvelocityY.
Add a display() method to demonstrate that these constructors work correctly.
Question 6: Non-Member Operator Overloading (<<)
Section titled “Question 6: Non-Member Operator Overloading (<<)”Overload the stream insertion operator (<<) as a non-member function to allow for easy printing of Particle objects to an output stream like std::cout.
Question 7: Custom Copy Constructor
Section titled “Question 7: Custom Copy Constructor”Implement a custom copy constructor for the Particle class. When a Particle is copied, the new particle should have the same properties as the original, but a message “Particle copied!” should be printed to the console.
Question 8: Overloading the Assignment Operator (=)
Section titled “Question 8: Overloading the Assignment Operator (=)”Overload the assignment operator (=) for the Particle class. Ensure you handle self-assignment and return a reference to the current object to allow for chaining assignments (e.g., p1 = p2 = p3).
Question 9: Dynamic Memory and Arrays of Objects
Section titled “Question 9: Dynamic Memory and Arrays of Objects”Write a program that:
- Asks the user how many particles to create.
- Dynamically allocates an array of
Particleobjects of that size. - Initialises each
Particlewith random positions (0.0fto99.0f). - Prints the details of each particle.
- Deallocates the memory correctly.
You can use the <cstdlib> and <ctime> header files to bring in code to seed the random number generator using srand(time(0)); and then generate a random number using the rand() function. You can use the modulo % operator to constrain the output of the rand() function to the range (0.0f to 99.0f).
Question 10: Pre- and Post-increment Operators
Section titled “Question 10: Pre- and Post-increment Operators”Create a Stopwatch class that stores seconds. Overload the pre-increment (++stopwatch) and post-increment (stopwatch++) operators to increment the seconds.
Solutions
Section titled “Solutions”Here are the video solutions — please do not watch these solutions without having attempted the questions first. Please note that these solutions are somewhat warts and all in that I make errors and correct them live without edits.
Alternatively, you can view the video directly on YouTube: https://www.youtube.com/watch?v=bB0eIW9SbdQ
© 2026 Derek Molloy, Dublin City University. All rights reserved.