Homework Solution

Object-Oriented Programming

Problems to be Submitted (20 points)

  1. (6 points)
    original after
    *f = 'W';
    after
    c = g;
    after
    e = b;
  2. (6 points)

    The three functions f( ), g( ), h( ) demonstrate those three respective styles of parameter passing.

    As the parameter is passed by value, f() has the effect of incrementing the formal parameter x before it is outputted. However, the actual parameter of the caller will not be effected. The effect can be observed if we execute the following code

        int j(5);
        f(j);        // outputs 6
        cout << j;   // outputs 5 (unchanged by f)
        

    As the parameter is passed by reference, g() has the effect of incrementing the formal parameter, and thus indirectly the actual parameter as well. We observe this effect as

        int k(5);
        g(k);        // outputs 6
        cout << k;   // outputs 6 (changed by g)
        

    Function h() is illegal, and would be rejected by the compiler. It is similar to g(), however since the parameter is passed by constant reference, the body of the function is not allowed to alter the parameter.

  3. (8 points)

    To best understand the initial block of code, we walk through the commands one at a time, drawing a sample diagram for each stage.
    Thing a; Thing b("Snow"); Thing c(b); Thing *d = &c; Thing *e = new Thing(); Thing *f = new Thing(b);

    Note well that when the copy constructor was used for c as well as for f, a new instance was created each time; b was simply used as the model for the state, at that time.

    1. Based upon the final configuration shown above, the output of the six statements is as follows.

      Cat In the Hat
      Snow
      Snow
      Snow
      Cat In the Hat
      Snow
    2. The command

      b.setBook("Hop on Pop");
      changes the state of that object. Note well, however, that this has no effect on c or f which were truly independent objects from b. The resulting picture is portrayed as

      and the displayed output is
      Cat In the Hat
      Hop on Pop
      Snow
      Snow
      Cat In the Hat
      Snow

    3. The command

      c.setBook("Hop on Pop");
      changes the state of that object. Note well that d is truly a pointer to the same underlying object, and therefore this implicitly changes *d. The resulting picture is portrayed as

      and the displayed output is
      Cat In the Hat
      Snow
      Hop on Pop
      Hop on Pop
      Cat In the Hat
      Snow

    4. The command

      a = b;
      assigns the state of object a to be equal to the current state of b. The resulting picture is portrayed as

      and the displayed output is
      Snow
      Snow
      Snow
      Snow
      Cat In the Hat
      Snow

    5. The command

      b = a;
      assigns the state of object b to be equal to the current state of a. The resulting picture is portrayed as

      and the displayed output is
      Cat In the Hat
      Cat In the Hat
      Snow
      Snow
      Cat In the Hat
      Snow

    6. The commands

      b=a;
      a.setBook("Hop on Pop");
      first assigns the state of b equal to the current state of a. The second command changes the state of a (yet has no effect on b). The resulting picture is portrayed as

      and the displayed output is
      Hop on Pop
      Cat in the Hat
      Snow
      Snow
      Cat In the Hat
      Snow

    7. The first command

      e=d;
      assigns the value of pointer e to be equal to the current value of pointer d (i.e., 239). After this, the subsequent command
      d->setBook("Hop on Pop");
      is changing the state of the object at address 239. Therefore, this has the apparent effect of changing c, *d and *e, all of which refer to the same object. The resulting picture is portrayed as

      and the displayed output is
      Cat In the Hat
      Snow
      Hop on Pop
      Hop on Pop
      Hop on Pop
      Snow

    8. The first command

      *e=*d;
      assigns the value of the object pointed to by e (i.e., that at address 246) to be equal to the object pointed to by d. This first change is made moot by the subsequent
      d->setBook("Hop on Pop");
      changes the object at address 239. The resulting picture is portrayed as

      and the displayed output is
      Cat In the Hat
      Snow
      Hop on Pop
      Hop on Pop
      Snow
      Snow


Last modified: Sunday, 12 February 2012