Skip to content

Extra Exercises

Practice Questions

  1. In the example of a set, is the following code snippet usable? Why or why not?
    coursesSet.insert( 5 ); // We would insert an int into the course set.

Homework Practice Tasks I.

  1. Write a class that represents a Course!
    • The Course has a name (of type string), a code (string), and a maximum capacity indicating how many students can enroll.
    • The data members should not be public, but provide getter methods for them!
    • The class should have a constructor where we can specify these characteristics and the constructor initializes them!
    • Include a main function that asks for the course data, then creates it and prints out the course details!
    • Solution
  2. The constructor parameter names should match the data member names, and use this to initialize the data members!
    • Use default parameters so that if the max capacity is not provided, it defaults to 25!
    • Solution
  3. Use a constructor initializer list to initialize the class data members! Provide 2 constructors (function overloads) where in the second case the max capacity does not need to be specified and defaults to 25! Solution.
  4. Initialize the max capacity using default member initialization! Solution.
  5. The two constructors behave the same, so the 2-parameter constructor “calls” the other constructor (delegating constructor)! Solution.
  6. Write a function (not a Course method in this example) that takes a course by reference as a parameter, reads an integer from input, and changes the course's max capacity by that amount. Extend the Course class with the necessary method as well. Solution.

Common Errors with Compile Error Logs

TODO


Last update: 2025-09-04
Created: 2025-09-04