Object-oriented Programming in C++

Bachelor of Science, École Polytechnique

CSC_2F001_EP – Object-oriented Programming in C++ – Exam 2025-2026

Duration: 1 hour 15 minutes

Any printed documents allowed. Including header files and handling errors is not required.

In this exam, you will design a new service able to regularly execute a task. To make the code generic and reusable, we will consider abstract tasks. To achieve this goal, a task is an instance of the class task_t, and the class task_t defines the purely virtual method void execute(). This means that task_t does not implement execute() itself. Instead, this function is implemented by subclasses of task_t.

The tasks (5.5 points)

2.5 points

Give the code for the class task_t. The class task_t defines the purely virtual method void execute() and a function void loop(). This function executes an infinite loop that:

  • calls execute(),
  • and then sleeps for one second by calling sleep(1).

1 point

Give the code for the class sayhello_t. This class inherits from task_t. Its execute() method prints "hello" to the terminal.

2 point

Give the code for the class cmd_t. This class inherits from task_t. It has a field const char* cmd initialized through the constructor. Its execute() method executes the command by using the function system(const char* cmd) provided by the standard library. The system function executes the command cmd as if it were typed in the terminal. Thanks to this, we can, for example, use cmd_t to print the contents of the current directory every second with:

cmd_t cmd("ls"); cmd.loop();

A generic linked list (14.5 points)

We now want to execute a list of tasks regularly. To do this, we will create a new kind of task called a batch of tasks. A batch of tasks contains a list of tasks. Its execute() function calls the execute() method of each task in the list, one after the other.

To manage the list of tasks, we will implement an intermediate data structure: a linked list called list_t. To make this data structure reusable, we will implement it in a generic way with templates. This means that list_t is parameterized by the class T. For example, list_t<int> declares a linked list of integers.

3 points

Give the code for list_t. In this question, list_t must define:

  • An internal class node_t. This class represents a node of the linked list and contains:
    • A field called next that points to the next element in the list.
    • A field called elmt that stores the element represented by the node. elmt has the type T (the type parameter of list_t).
    • A constructor that (i) initializes next to nullptr, and (ii) takes an element as an argument and uses it to initialize the field elmt.
  • A field of type node_t called root that points to the first element of the list.
  • A constructor that initializes root to nullptr.

2.5 points

Give the code for the void prepend(const T& elmt) method of list_t. This function creates a new node to hold the element elmt and, as the name suggests, puts the new node at the front of the list (before the other nodes).

2 points

Now that we can handle a list, we can implement our batch of tasks. Give the code for the class batch_t. This class inherits from task_t and uses list_t to store the tasks in the batch. The execute() method of batch_t executes each task in the list, one after the other.

2 points

Give the code for the main function. This function:

  • creates a batch that contains two tasks: one task that says hello, and another that prints the contents of the current directory,
  • execute these tasks every second.

2 points

Now give the code for the destructor of list_t. The destructor must delete all the elements in the list.

3 points

Adding a new task at the front is not very convenient for the users of our class. Adding a new task at the back of the list, after the last element, would be more intuitive. For this, give the code for void append(const T& elmt), which appends the element at the end of the list.