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:

  1. + add two Distances and return the sum
  2. + add a Distance and a parm representing inches and return the sum
  3. - subtract the parm from the invoking object and return the difference
  4. = copy the parm into the invoking object and return the invoking object
  5. == compare two Distances and return true if they are the same
  6. < compare two Distances and return true if the invoking object is less than the parm
  7. > compare two Distances and return true if the invoking object is greater than the parm
  8. != compare two Distances and return true if the invoking object is not equal to the parm
  9. += add the Distance parm to the invoking object
  10. += add the inches parm to the invoking object
  11. << print out a Distance
  12. >> read in a Distance

Email Me | Office Hours | My Home Page | Department Home | MCC Home Page

© Copyright Emmi Schatz 2003