CSC_2F001_EP – Object-oriented Programming in C++ – Exam 2024-2025
Duration: 1 hour 15 minutes
All documents allowed
Including the header files and handling errors is not required.
In this exam, you will design the inventory of a player in a video game.
The items (4 points)
In this first exercise, you must design the items that will be stored in the player's inventory.
1.5 points
An item in the inventory inherits from the item_t class. Any item has a weight (an integer), a pure virtual void display() method used to display the item on the screen, and a constructor to initialize the weight. Provide the code for item_t.
2.5 points
Next, design the sword_t class. This class represents a sword. A sword has a damage field (an integer) that defines the strength of the sword. sword_t includes a constructor to initialize the damage and weight of the sword. The display method of sword_t prints the damage and weight of the sword in the terminal. Provide the code for sword_t.
A linked list of items (7 points)
To store the player's items, we will use a linked list. In this exercise, you will design a generic linked list, meaning the linked list is not specifically tailored for items. For this, we will define list_t as a template class.
2.5 points
The list_t class is a template class parameterized by the T type. list_t contains an internal class called node_t (i.e., node_t is defined inside list_t). node_t represents a node in the linked list. It has two fields:
- A field called next that points to the next element in the list,
- A field called value. value is a pointer to an object of type T.
The list_t class has a single field called root. This field points to the first element in the linked list. It is initialized in the list_t constructor to nullptr.
Provide the code for list_t (including the internal node_t class).
1.5 points
Provide the code for the method void push(T* value). This method is defined inside list_t. It adds value to the beginning of the linked list. To do so, it creates a new node that (i) points to the old root node and (ii) stores the given value.
3 points
Provide the code for the method T* pop(). This method is defined inside list_t. If root is null, pop returns nullptr. Otherwise, pop removes the first element from the list, deletes it, and returns the value associated with the node.
The inventory (6 points)
We can now implement the player's inventory.
1 point
The inventory_t class contains a list of items. Provide the declaration of inventory_t.
1 point
Provide the code for the method void add(item_t* item). This method belongs to inventory_t. It adds the item to the item list.
2 points
Provide the code for the method int weight(). This method belongs to inventory_t. It returns the total weight of the inventory.
2 points
Provide the code for the method void display(). This method belongs to inventory_t. It prints each item in the terminal.
Container interface (3 points)
In this exercise, you will design a better application programming interface for list_t.
Since the number of points for this question is not proportional to its difficulty, we recommend attempting this exercise only after completing the others.
3 points
We want to be able to enumerate the elements of list_t using a range-based loop, for example:
This range-based loop is internally rewritten by the C++ compiler into:
This means you need to provide the implementation for:
- iterator_t begin(): defined in list_t. It returns an iterator to enumerate the elements of the linked list.
- iterator_t end(): defined in list_t. It returns an iterator that represents the end of the list.
-
struct iterator_t: defined in list_t. This internal class of list_t is the iterator class.
It must implement:
- bool operator != (const iterator_t& it): returns true if the receiver iterator and the it parameter reference different elements in the linked list,
- iterator_t& operator ++ (): advances the iterator to the next node of the linked list,
- T* operator * (): returns a pointer to the value referenced by the current node.