Homework: Operator Overloading
Write a class called Distance. The Distance class
will have three data members: yards (an int), feet (an int), and inches
(a float). Remember that there are 12 inches in a foot and 3 feet in a
yard.
Your Distance class should have the following member functions:
- default constructor
- This function initializes the Distance object to zero yards, zero feet,
and zero inches.
- constructor with three parms
- This function has parms for the yards, feet, and inches.
It initializes the Distance object to the values given
by the parms.
- copy constructor
- This function initializes the Distance object to contain the same values
as the parm.
- setDist
- This function has parms for yards, feet, and inches, returns void.
setDist sets the distance to the values given by the parms.
Write the following operators for your Distance class:
- + add two Distances and return the sum
- + add a Distance and a parm representing inches and return the sum
- - subtract the parm from the invoking object and return
the difference
- = copy the parm into the invoking object and return the
invoking object
- == compare two Distances and return true if they are the same
- < compare two Distances and return true if the invoking
object is less than the parm
- > compare two Distances and return true if the invoking
object is greater than the parm
- != compare two Distances and return true if the invoking object
is not equal to the parm
- += add the Distance parm to the invoking object
- += add the inches parm to the invoking object
- << print out a Distance
- >> read in a Distance
Email Me |
Office Hours |
My Home Page |
Department Home |
MCC Home Page
© Copyright Emmi Schatz 2003