Topical Information

This lab will help you practice with operator overloading (mixed with classes).

Program Information

Write a class that simply harbors a long integer (that is its only data member). Overload the stream operators (input and output) for this type. Also overload the subscript operator to allow a programmer to retrieve a particular digit (if present). For example:

   Object's Data Value   Operator Call     Result  Commentary
           1234            obj[10]           3
           1234            obj[1000]         1
           1234            obj[10000]       -1    (doesn't have that digit)

Finally, overload the function call operator to return a range of digits. For example:

   Object's Data Value   Operator Call        Result  Commentary
           1234            obj(10,1000)         123
           1234            obj(1000,10)         123
           1234            obj(1,10000)        1234
           1234            obj(1,1)               4
           1234            obj(1,100)           234
           1234            obj(10,100)           23
           1234            obj(10000,100000)     -1  (doesn't have those digits)

Hint: Think simply! You could do these things before you even knew what a function was. to extract individual digits...? Try not to resort to branching/looping to accomplish either of these operations.

Note: Since this class's entire purpose is to extend the basic functionality associated with a long, it should have a non-explicit initialization constructor. That way, if someone tries to do one of your operations to a simple long variable, they'll get the right answers. (Although this may or may not work with our compiler. It is poor at type matching in certain situations.) Also, since your [] and () operators will be returning class objects (not simple longs), you should provide a typecast operator for long so that you don't have to provide operators for other common long operations (+, -, etc.). (A typecast operator is of the form:

   operator TYPE ();
It doesn't have a return type stated because it will be returning the type specified in its name. It should simply return the object's data value.)

Don't forget to write a test application for your class. Remember that some of the mixing tests may fail under our compiler (as well as under Visual compilers). Try them with the g++ compiler (either here or at home). If they fail, comment them out before making your final script. Make sure to note in the comments which compiler they failed with.

Thought Provoking Questions

  1. Which operators are friends and which are members? Do any have to be members?

  2. Which operators should be const? What other methods might well be const?

  3. Does this class serve any useful purpose? Why/Why not?

  4. What use is a typecast operator? When would it be called? Why would you want your objects to be cast back to a simpler type?

This assignment is (Level 2.5).