UMBC CMSC 202 Computer Science II
Lab 5: Operator Overloading
Source:
Old Orchard Recipes
Objective
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:
- Smoothie.h: the header file for the
Smoothie class. You will not need to change this file.
- Smoothie.cpp: the implementation
file for the Smoothie class. Most of the member functions have been
implemented for you. You will add the implementation of overloaded
+ and == operators to the end of this file.
- stest.cpp: a file with a main
function that exercises the Smoothie class.
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:
- Edit Smoothie.cpp. You will add a
function at the end to implement the overloaded == operator for
the Smoothie class.
- Note the function prototype of the == operator in
Smoothie.h:
bool operator ==(const Smoothie& lhs, const Smoothie& rhs) ;
-
When two Smoothies are compared as in:
DrinkA == DrinkB
DrinkA is passed as lhs and DrinkB
as rhs.
- 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.
- 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:
- Edit Smoothie.cpp. You will add a
function at the end to implement the overloaded + operator for
the Smoothie class.
- 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.
- Unlike the == operator, the + operator has only one parameter.
Why? It is because + is declared as a Smoothie member function.
-
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.
- You will need to declare a local Smoothie object and return it by
value.
- Recall that the string class supports the + operator for
concatenation.
- Don't forget the "Smoothie::" in front of
"operator+".
- 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
-
To compile, use:
g++ -Wall -ansi Smoothie.cpp stest.cpp
-
Bugs? Try to figure it out yourself first. If that doesn't work, as the TA
for help.
- 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.
-
The function prototype of the overloaded << operator is:
ostream& operator<< (ostream& out, const Smoothie& drink);
This must be a non-member function.
- 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.
- Modify Smoothie.h. Append the function prototype of
the overloaded << operator for Smoothie.
- You do not need to change the Smoothie class, since you can use the
public member function Describe().
- Modify the main function to test your overloaded operator.
P.S.:
Some lyrics.