Extra Exercises
In-Class Practice Exercises I¶
-
In the previous session, we completed the
Course
class. The implementation of the class can be found in thestudent-course-1.cpp
file, which also includes aStudent
class.- Let's examine the types of parameter passing used (
const string&
)! - Check what modifiers the methods have (
const
)!
- Let's examine the types of parameter passing used (
-
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!
- Use the
-
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 forCourse
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!
- It should be possible to add two courses together, where the students from the second course are added to the first one (using the
-
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")!
In-Class Practice Exercise Set II¶
-
Create a
Document
class with the following members:title
(string)content
(string, default value:""
)author
(string, default value:"anonym"
)opens
(unsigned, initial value:0
)
-
Implement getters for each member as we learned!
- Every getter should increment the
opens
counter by one — except the getter that returnsopens
itself! (This requires the use ofmutable
.)
- Every getter should increment the
-
Implement the pre- and post-increment operators!
- They should increase the
opens
count by one (as if the document was simply "touched").
- They should increase the
-
Implement the
+=
operator that appends a string to the content! -
Implement the
+
operator that appends a string to the content, but only stores it in a new document namedsaved_as
(save as... functionality). -
Make the
Document
convertible tounsigned
, where the conversion result is the length of the content!- The conversion should not be implicit!
-
Implement the negation of a
Document
(operator!
) with anunsigned
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.
- If the document is empty, insert the text
At-Home Practice Exercises I¶
-
Create a
Meme
class! It should have two private string members:text
andimage
. 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 aMeme
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 tenMeme
objects in an array! What changes are needed in theMeme
class to make the array creation work? -
Overload the
&
and&=
operators in theMemeCollection
class, where the left-hand operand is aMemeCollection
, and the right-hand operand is aMeme
. The new meme should be inserted into the first available slot in the array! Update theMemeCollection
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 copiedMemeCollection
with the new meme added. - In the case of the
MemeCollection &= Meme
operation, the left-hand sideMemeCollection
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).
- In the case of the
-
Overload the index operator
[]
in theMemeCollection
class to return the i-th stored meme. There should be bothconst
and non-const
versions! If an invalid index is accessed, print"Error"
tocout
and return the meme at index 0 instead.- Outside the classes, create a
void print_collection()
function that takes aconst MemeCollection
parameter and uses afor
loop to print all stored memes usingprint_meme
. Only write a getter to retrieve the current meme count!
- Outside the classes, create a
Full Solution
- Code: Practice Exercise Set
At-Home Practice Exercise Set II¶
-
Create a
Music
class! Its data members are:length
(integer, in seconds) andname
. The default value ofname
should be"a-minor"
.
The class should have aprint()
method that outputs its values tocout
.
Provide a two-parameter constructor to initialize both data members, and a one-parameter constructor to initialize onlylength
(withname
set to the default value).
Make sure implicit conversion fromint
toMusic
is not allowed! -
Implement the
Music + int
operator, which returns a newMusic
object based on the original but with increased length. The added value should come from the parameter. -
Implement the
int + Music
operator, which performs the same operation asMusic + int
. -
Implement the
Music += int
operator, which increases the music’s length by the given value. The currentMusic
object should be modified, and the return value should be a reference to the modified object. -
Implement both the prefix and postfix
++
operators for theMusic
class, which increase the length by 1 second.
Full Solution
- Code: Practice Exercise Set
Created: 2025-09-04