Main
Lab 8
Objective:
Operator Overloading.
Consider a SimpleString
class that simply encapsulates a single, private std::string
type with constructors (including a default constructor) and get and set functions.
Overload the following operators for this class. Refer to this week's lecture notes for detailed examples of how to declare and define overloaded operator functions.
Starter code for this lab can be downloaded from here.
- First, find out which operators cannot be overloaded and write them down in your notes. Check with some of your neighbors to see which ones they determined cannot be overloaded. The actual answer is on page 325 of the C++14 standard (look it up after you come with a list using whatever search methods you usually use).
- Operator= : should be very similar to copy constructor. It returns a
SimpleString
. - Operator+ : it concatenates two
SimpleString
s together and returnsSimpleString
. Refer tostring::append
. - Operator++ : both pre and post increment; it adds a “*” character to the end of current string. Refer to
string::push_back
. - Operator-- : both pre and post decrement; it removes the last character from current string. Refer to
string::erase
orstring::pop_back
(C+11 and newer only). - The ostream (<<) and the istream (>>) operators. Simply returns and initializes the
SimpleString
. Note that you should define these operators asfriend
and implement them as functions. - Exchange your SimpleString.hpp and SimpleString.cpp files with someone else and test your main with the other student's SimpleString implementation. Do this even if the other's code is incomplete or does not work yet. If that is the case, try to fix it, then test. Compare implementations and discuss differences, if any.
Test the operators in the main function.