Skip to content

Search is only available in production builds. Try building and previewing the site to test it out locally.

⚙️Tutorial: C++ with Classes 2

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!


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.


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.


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.


Overload the == operator for the Vector2D class to check if two vectors are equal (i.e., their x and y components are the same).


Create a Particle class with x, y coordinates and velocityX, velocityY as float. Provide three constructors:

  1. A default constructor that initializes all values to 0.0f.
  2. A constructor that takes x and y positions and initializes velocities to 0.0f.
  3. A constructor that takes x, y, velocityX, and velocityY.

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.


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:

  1. Asks the user how many particles to create.
  2. Dynamically allocates an array of Particle objects of that size.
  3. Initialises each Particle with random positions (0.0f to 99.0f).
  4. Prints the details of each particle.
  5. 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.


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