COM1204, Summer 1998, Prof. Futrelle

Final Exam, Thursday, September 3rd

This is a closed-book, closed-notes exam, two hours in length. "PS" refers to our phone system.

1. (10 points). For the following assignment statement, the array reference on the left-hand side (an lvalue) is dealt with at run time quite differently than the right-hand side (an rvalue). Explain this difference. One possibility is to describe the difference as two different aspects of the same object.


2. (15 points). Below are two different equality tests (==), based on the PS. Explain how the two tests differ because of the types of the items being compared.




3. (15 points). The definition for a timer used in the PS is the following,

class Timer {
    bool on; // timer's on
    int set_for; // sys->time that "timer alarm sounds"

    void start(int duration); // will go off after duration steps
    int start(int min_duration, int max_duration); // chooses random duration
    void shut_off(); // shuts it off completely (not a pause)
    int remaining(); // how much time left?
    bool is_on(); // is it running?
    bool rang(); // has it rung?



Discuss how the Timer class could be altered to allow a Timer instance to be created by,

where sys_n is (a pointer to) some System instance. Discuss how the functions in the Timer class could be altered slightly to refer to the time value for the particular system passed at construction time. Hint: It is not necessary to change any of the existing non-constructor member function argument lists in order to do this.


4. (15 points). Assume that you have a class Hand_Tool that can be created without any specific initialization, or can be created with a floating point value indicating its weight (ounces, a float value). Describe the two constructors in detail and discuss how they are kept distinct and why it is legal to have more than one constructor for a class.


5. (20 points). In the PS, if there is no packet currently in a wire, it is tempting just to set the packet slot of Wire to NULL.

5a. Describe the type of extra code that is required to deal with the presence of NULLs, e.g., how would you check the value of the control string for such a packet?

5b. Describe how you could construct a method for Wire called put_empty() that would place a packet in the wire containing a control string empty (an additional static Keyword slot). Also describe how you could create a method is_empty() that would check a wire to see if it contained such a packet. Try to write out actual code to answer this question in addition to your explanation (not instead of your explanation).



Hint: The packet constructor has the argument structure,

and we're only interested in the control argument in this question.

6. (25 points). This question deals with inheritance. A base class animal and a derived class bear are shown, along with constructors and method definitions.

// Abstract base class, animal:

class animal {
    float height; // cm.
    float weight; // kg.
    void print_base();
    void print_specs();

    animal(float h, float w);

animal::animal(float h, float w) : height(h), weight(w) {}

void animal::print_base() {
    cout << "I'm an animal." << endl;

void animal::print_specs() {
  cout << "Height: " << height << " cm.,
    weight: " << weight << " Kg." << endl;

class bear : public animal {
    string *my_descriptor;
    void print1();
    bear(string* descrip, float height, float weight);};

// This constructor is a bit tricky, because you have to know
// that the constructor for the base class can be called
// in the member initialization list.

bear::bear(string* s, float h, float w) :my_descriptor(s), animal(h, w) {}

void bear::print1() {
  cout << "Specifically, I'm " << *my_descriptor << endl;

What you are to do:

6a. Write the line of code that will assign to the variable growler (pointer to bear) a new bear with description, "a tough old grizzly named Growler." and height 202.3 and weight 254.

6b. Write an expression that will call the print1() function for growler.

6c. Write the output that the print1() function will produce.

6d. Explain how inheritance operates to make the things you have described happen. In particular, how do the slots height and weight work under inheritance? Also, how can methods in animal be accessed from a bear method? Discuss any other aspects of inheritance that you feel are useful in understanding this example.