⚙️Tutorial: C++ with Classes

A Robotics Tutorial
Section titled “A Robotics Tutorial”Welcome to the fourth in our C++ tutorial series. In this tutorial, we will shift our focus from theoretical concepts to practical application, using the principles of Object-Oriented Programming (OOP) to build a simple but extensible robotics control system.
We will explore key C++ features such as classes, constructors, destructors, inheritance, polymorphism, and casting. Unlike the bank account examples in the core text, our robotics application will provide a fresh perspective on how these concepts are applied in a real-world engineering context.
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: The Base Robot Class
Section titled “Question 1: The Base Robot Class”Create a base Robot class. This class should have a private integer member robotID and a private float member batteryLevel. Implement a constructor that takes an ID and an initial battery level as arguments and initialises these members. Also, add a public method displayStatus() that prints the robot’s ID and current battery level.
Question 2: Encapsulation with Accessors and Mutators
Section titled “Question 2: Encapsulation with Accessors and Mutators”Enhance the Robot class by adding public accessor (getter) and mutator (setter) methods for the batteryLevel. The setBatteryLevel() method should validate the input, only allowing values between 0.0 and 100.0. If an invalid value is provided, it should print an error message and not change the battery level.
Question 3: Destructors and Dynamic Memory Management
Section titled “Question 3: Destructors and Dynamic Memory Management”Modify your Robot class to manage dynamic memory. Add a private char* member name. In the constructor, dynamically allocate memory for this name (e.g., using new char[]) and copy the provided name string into it. Implement a destructor ~Robot() that frees this dynamically allocated memory using delete[] and prints a message indicating that the robot object is being destroyed.
Question 4: Working with Pointers to Objects
Section titled “Question 4: Working with Pointers to Objects”In your main() function, create a Robot object on the heap using the new keyword. Use a pointer to this object to call its methods, such as displayStatus() and setBatteryLevel(). Remember to use the -> operator for member access with pointers. Finally, release the dynamically allocated memory using delete.
Question 5: Simple Inheritance and Method Overriding
Section titled “Question 5: Simple Inheritance and Method Overriding”Create a new class WheeledRobot that inherits publicly from the Robot class. Add a new private member numWheels. Implement a constructor for WheeledRobot that calls the base class constructor and initialises numWheels. Override the displayStatus() method to also print the number of wheels.
Question 6: Abstract Classes and Pure Virtual Functions
Section titled “Question 6: Abstract Classes and Pure Virtual Functions”Make the Robot class an abstract class by adding a pure virtual function virtual void performTask() = 0;. This function will have no implementation in the base class. In the WheeledRobot class, provide an implementation for performTask() that prints a message like "Wheeled robot is moving cargo."
Question 7: Polymorphism in Action
Section titled “Question 7: Polymorphism in Action”Create a second derived class, Drone, that also inherits from Robot. Implement its performTask() method to print "Drone is performing aerial surveillance." In your main() function, create a std::vector of Robot* pointers. Add a WheeledRobot object and a Drone object to this vector. Loop through the vector and call performTask() on each element. Explain why this works.
You haven’t covered the use of vectors in the notes at this point, so I have included the code necessary. Add #include<vector> at the top of your program and the code below should be placed within the main() function. Adapt the code to make it work with your exact class names. Note that the for loop simply iterates through all of the elements in the vector and provides you with a pointer of type Robot* to each of them.
std::vector<Robot*> robotFleet;robotFleet.push_back(new WheeledRobot(201, 80.0f, "CargoBot", 8));robotFleet.push_back(new Drone(202, 90.0f, "SkyWatch"));
for(Robot *r : robotFleet) { r->displayStatus(); r->performTask(); cout << endl;}Question 8: Safe Casting with dynamic_cast
Section titled “Question 8: Safe Casting with dynamic_cast”Using the vector from the previous question, write code to iterate through the Robot* pointers. For each pointer, use dynamic_cast to safely check if the object is a WheeledRobot. If the cast is successful, call a new WheeledRobot-specific method (e.g., spinWheels()) that you have added to the WheeledRobot class. Explain why dynamic_cast is the correct choice here instead of static_cast.
Question 9: Default and Constant Parameters
Section titled “Question 9: Default and Constant Parameters”Add a new method recharge(float amount = 10.0) to the Robot class that increases the battery level. This method should have a default parameter. Demonstrate calling this method with and without an argument. Additionally, add a const method checkBattery() that returns the current battery level. Create a const Robot object and demonstrate that you can only call const methods on it.
Question 10: Structs and Unions
Section titled “Question 10: Structs and Unions”In your program, define a struct named SensorData with public members float temperature and float pressure. Also, define a union named RobotID that can hold either an int serialNumber or a char* modelName. In main(), create an instance of SensorData and RobotID, and demonstrate how to set and retrieve values from them, highlighting the memory-sharing nature of the union.
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=EUipeF0_4nU
© 2026 Derek Molloy, Dublin City University. All rights reserved.