Skip to content

Extra Exercises

In-Class Practice Exercises I

  1. In the previous session, we completed the Course class. The implementation of the class can be found in the student-course-1.cpp file, which also includes a Student class.

    • Let's examine the types of parameter passing used (const string&)!
    • Check what modifiers the methods have (const)!
  2. Implement the ability to add students to a course!

    • Use the + operator for adding!
    • Also implement the += operator!
    • Don't worry about error handling for now — if the course is full, just print a message saying that the student couldn't be added, and do nothing else!
  3. Implement the "merging" of courses!

    • It should be possible to add two courses together, where the students from the second course are added to the first one (using the + operator)!
    • Also implement the += operator for Course objects!
    • No need to handle errors here either — if the students from the second course don't fit in the first one, just print an error message!
  4. Implement the index operator for the Course class!

    • It should be possible to modify the given element!
    • It should also work for const objects and return the i-th element in a non-modifiable way!
    • No need for full error handling — if the index is out of range, just print a message and return the 0th element (the 0th element is guaranteed to exist, at worst it's "empty")!

Solutions: Tasks 2–3, Task 4

In-Class Practice Exercise Set II

  1. Create a Document class with the following members:

    • title (string)
    • content (string, default value: "")
    • author (string, default value: "anonym")
    • opens (unsigned, initial value: 0)
  2. Implement getters for each member as we learned!

    • Every getter should increment the opens counter by one — except the getter that returns opens itself! (This requires the use of mutable.)
  3. Implement the pre- and post-increment operators!

    • They should increase the opens count by one (as if the document was simply "touched").
  4. Implement the += operator that appends a string to the content!

  5. Implement the + operator that appends a string to the content, but only stores it in a new document named saved_as (save as... functionality).

  6. Make the Document convertible to unsigned, where the conversion result is the length of the content!

    • The conversion should not be implicit!
  7. Implement the negation of a Document (operator!) with an unsigned return type!

    • If the document is empty, insert the text "Lorem ipsum...".
    • If it contains any content, clear it.
    • Return the length of the new content as the result.

Solution

At-Home Practice Exercises I

  • Create a Meme class! It should have two private string members: text and image. Provide a public constructor with two parameters to initialize these members. Also write appropriate getter methods!

  • Create a void print_meme() function, which takes a const reference to a Meme as a parameter! It should print the meme's text in one line and the image on the following line. How should you modify the previously written getters to make this possible?

  • Create a MemeCollection class that can store up to ten Meme objects in an array! What changes are needed in the Meme class to make the array creation work?

  • Overload the & and &= operators in the MemeCollection class, where the left-hand operand is a MemeCollection, and the right-hand operand is a Meme. The new meme should be inserted into the first available slot in the array! Update the MemeCollection class so that it keeps track of the current number of stored memes:

    • In the case of the MemeCollection & Meme operation, neither operand should be modified; the result should be a copied MemeCollection with the new meme added.
    • In the case of the MemeCollection &= Meme operation, the left-hand side MemeCollection should be modified by adding the new meme. The return value should be a reference to the modified collection.
    • If the collection is already full, no addition should occur — only return the appropriate types (either an unmodified copy or the original unmodified operand).
  • Overload the index operator [] in the MemeCollection class to return the i-th stored meme. There should be both const and non-const versions! If an invalid index is accessed, print "Error" to cout and return the meme at index 0 instead.

    • Outside the classes, create a void print_collection() function that takes a const MemeCollection parameter and uses a for loop to print all stored memes using print_meme. Only write a getter to retrieve the current meme count!
Full Solution

At-Home Practice Exercise Set II

  1. Create a Music class! Its data members are: length (integer, in seconds) and name. The default value of name should be "a-minor".
    The class should have a print() method that outputs its values to cout.
    Provide a two-parameter constructor to initialize both data members, and a one-parameter constructor to initialize only length (with name set to the default value).
    Make sure implicit conversion from int to Music is not allowed!

  2. Implement the Music + int operator, which returns a new Music object based on the original but with increased length. The added value should come from the parameter.

  3. Implement the int + Music operator, which performs the same operation as Music + int.

  4. Implement the Music += int operator, which increases the music’s length by the given value. The current Music object should be modified, and the return value should be a reference to the modified object.

  5. Implement both the prefix and postfix ++ operators for the Music class, which increase the length by 1 second.

Full Solution

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