UMBC CMSC 202 Computer Science II

Lab 5: Operator Overloading

Source: Old Orchard Recipes


In this lab you will practice overloading operators in two ways: as a member function and as a non-member function.

Assignment: He's a Smoothie Operator

Your assignment is to add two operators + and == to a pre-defined C++ class: Smoothie. This class models a blended fruit drink. It has two private data members: ingredients which records the fruits that went into the drink, and amount which records the number of ounces in the drink.

There are two constructors in Smoothie. (See header file Smoothie.h.) The default constructor creates an empty glass. The alternate constructor creates a drink with the specified fruit and ounces, for example:

Smoothie DrinkMe("orange", 25) ;

Member functions in the Smoothie class must make sure that the amount member never exceeds the constant GLASS_LIMIT. If GLASS_LIMIT is exceeded, an overflow message is printed to the console. The program does not terminate when an overflow condition is detected: you've made a mess, but you still have a full glass of fruit drink. The constant GLASS_LIMIT is defined in Smoothie.h and is set to 30,000 ounces.

Finally, the member function Describe() returns a string value that is a description of the current drink. For example,

DrinkMe.Describe() ; returns the string value "40 oz orange kiwi smoothie". The use of an output string stream in Describe() might be interesting. This is the C++ analog of sprintf() in C.

Step 1: Get the files

Here are the files you need:

Step 2: Overload the == operator

The == operator for the Smoothie class should return true if the ingredients strings of the two Smoothies are the same.

Follow these steps:

  1. Edit Smoothie.cpp. You will add a function at the end to implement the overloaded == operator for the Smoothie class.

  2. Note the function prototype of the == operator in Smoothie.h: bool operator ==(const Smoothie& lhs, const Smoothie& rhs) ;

  3. When two Smoothies are compared as in: DrinkA == DrinkB DrinkA is passed as lhs and DrinkB as rhs.

  4. Note that the == operator is not declared as a member function. However, the == operator is declared a friend function of the Smoothie class, so your function can access the ingredients data member.

  5. Note that the string class supports the == operator for string comparison.

Step 3: Overload the + operator

When two Smoothies are added, the ingredient strings are concatenated and the amounts are summed. The GLASS_LIMIT condition should be checked.

In real life, you would pour one glass into another. Here our modeling is a bit unrealistic. When you add DrinkA and DrinkB, you get a third drink, but DrinkA and DrinkB remain intact.

Follow these steps:

  1. Edit Smoothie.cpp. You will add a function at the end to implement the overloaded + operator for the Smoothie class.

  2. Note the function prototype of the == operator in Smoothie.h: const Smoothie operator+(const Smoothie& rhs) const ; This is a const member function that returns a const Smoothie.

  3. Unlike the == operator, the + operator has only one parameter. Why? It is because + is declared as a Smoothie member function.

  4. When two Smoothies are added as in: DrinkA + DrinkB DrinkA becomes the host object (a.k.a. calling object) and DrinkB is passed as rhs.

  5. You will need to declare a local Smoothie object and return it by value.

  6. Recall that the string class supports the + operator for concatenation.

  7. Don't forget the "Smoothie::" in front of "operator+".

  8. When you add a "strawberry" Smoothie to another "strawberry" Smoothie, you get a "strawberry strawberry" Smoothie. We could fix that with a vector of strings, but we didn't want you to have to write too much code.

Step 4: Compile and Debug

  1. To compile, use: g++ -Wall -ansi Smoothie.cpp stest.cpp

  2. Bugs? Try to figure it out yourself first. If that doesn't work, as the TA for help.

  3. Did you forget the "Smoothie::" in front of "operator+"?

Extra Credit: Overload << (2 points)

When we want to print out a Smoothie to the console, we have to use something like: cout << DrinkA.Describe() << endl ; It would be nice if we could omit the call to Describe and just say: cout << DrinkA << endl ; This can be accomplished by overloading the << operator.

  1. The function prototype of the overloaded << operator is: ostream& operator<< (ostream& out, const Smoothie& drink); This must be a non-member function.

  2. The value returned by operator<< should simply be the parameter out. This is so that the syntax: cout << "A " << DrinkA << " tastes great\n" ; works out. Note that << associates left.

  3. Modify Smoothie.h. Append the function prototype of the overloaded << operator for Smoothie.

  4. You do not need to change the Smoothie class, since you can use the public member function Describe().

  5. Modify the main function to test your overloaded operator.

P.S.: Some lyrics.