Scope of instance, local, and parameter variables

The goal for this exercise is to understand how instance variables, local variables, and parameters differ in their scope – when they start to exist, how long they exist for, and when they cease to exist.

What you need to do to prepare for this exercise:


What you need to do for this exercise

Within the provided starter project, there is already a class named Scope_Of_Variables.

  1. You should create a new class, named NumberPrinter below it.

    1. This class should have, as data members (instance variables), the lowest and highest numbers to print.

    2. It should also have a method, named Print, which prints out all the (integer) numbers between lowest and highest. The method should have a single, bool, parameter, and if that parameter is true, then the method should print out only the even numbers. If the parameter is false, then you should print out all the numbers in the specified range.

    3. Additionally, it should have a default constructor that sets both the lowest and highest to be 0.

  2. The following code:

    NumberPrinter np = new NumberPrinter();
    np.SetLowest( 3.14159 );
    np.SetHighest( 12 );
    np.Print(true);
    np. SetHighest( 17.1 );
    np.Print(false);
    np.SetLowest(17); // note that lo == hi
    np.Print(false); // no output
    np.SetLowest(22); // note that lo > hi
    np.Print(false); // no output

    should produce the following results:

     4
     6
     8
     10
     12
     4
     5
     6
     7
     8
     9
     10
     11
     12
     13
     14
     15
     16
     17
    
    1. The above sample code has been placed into the Scope_Of_Variables.RunExercise, and you are encouraged to run it, in order to double-check your work.

  3. After having done all of the above, put a short comment into the Scope_Of_Variables class, explaining the differences between instance variables, local variables, and parameters, in terms of when they're created, how long they exist for, and when they're destroyed. Feel free to research this via means beyond your textbook, if you need to.

    In order to make your comment easy to find, please put it right underneath line that reads // Put your comment here:, in the Scope_Of_Variables class