Practice Exercise #8

The following source file, named, uses a parameterized type, Range, that implements an integer type restricted to some consecutive interval (1 to 1000 in this example): When run, the following output is produced:

bash$ g++ -Wall -std=gnu++0x -g -o ex8
bash$ ./ex8
x = 911
y = 44
z = 120
x+y+z = NaN
Your job is to implement Range so that the code above compiles cleanly and produces the output shown when run.


  • If a value falls outside the interval of the Range type, the result is Nan (not a number). Any operation involving a NaN value produces a Nan result.
  • If you have a constructor that takes a single integer argument, then C++ will automatically promote integer literals to Range objects if needed. (For example, (x-z) * 4 multiplies a Range by an int. The solution code does not define a method for that, but does define Range times Range. C++ creates a Range object from the 4 literal, then invokes the Range multiplication operator.)
  • The result of the to_string method is a C++ string object.
  • Put all your code in a single file, Range.h. (That is, put your code directly in the .h file, not in a separate .cc file.)
  • We will grade your code by linking it with a grading mainline and examining the results it produces. That mainline may use arbitrary arithmetic formulas, but restricted to the four operators shown in the example mainline.