Extra Exercises
In-Class Practice Exercises I¶
-
In the previous session, we completed the
Courseclass. The implementation of the class can be found in thestudent-course-1.cppfile, which also includes aStudentclass.- 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 forCourseobjects! - 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
Courseclass!- It should be possible to modify the given element!
- It should also work for
constobjects 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
Documentclass 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
openscounter by one — except the getter that returnsopensitself! (This requires the use ofmutable.)
- Every getter should increment the
-
Implement the pre- and post-increment operators!
- They should increase the
openscount 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
Documentconvertible 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 anunsignedreturn 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
Memeclass! It should have two private string members:textandimage. 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 aMemeas 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
MemeCollectionclass that can store up to tenMemeobjects in an array! What changes are needed in theMemeclass to make the array creation work? -
Overload the
&and&=operators in theMemeCollectionclass, 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 theMemeCollectionclass so that it keeps track of the current number of stored memes:- In the case of the
MemeCollection & Memeoperation, neither operand should be modified; the result should be a copiedMemeCollectionwith the new meme added. - In the case of the
MemeCollection &= Memeoperation, the left-hand sideMemeCollectionshould 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 theMemeCollectionclass to return the i-th stored meme. There should be bothconstand non-constversions! If an invalid index is accessed, print"Error"tocoutand return the meme at index 0 instead.- Outside the classes, create a
void print_collection()function that takes aconst MemeCollectionparameter and uses aforloop 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
Musicclass! Its data members are:length(integer, in seconds) andname. The default value ofnameshould 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(withnameset to the default value).
Make sure implicit conversion frominttoMusicis not allowed! -
Implement the
Music + intoperator, which returns a newMusicobject based on the original but with increased length. The added value should come from the parameter. -
Implement the
int + Musicoperator, which performs the same operation asMusic + int. -
Implement the
Music += intoperator, which increases the music’s length by the given value. The currentMusicobject should be modified, and the return value should be a reference to the modified object. -
Implement both the prefix and postfix
++operators for theMusicclass, which increase the length by 1 second.
Full Solution
- Code: Practice Exercise Set
Created: 2025-11-27