MC logo

Access Control (Answer)

  Practice Questions

  1. *cip = m;

    This is illegal because cip is a pointer to constant integer, hence what it points to may not be changed.

  2. ip = cip;

    This is also illegal, because ip is not prevented from changing what it points to, so letting ip point to where cip points creates a risk. Think of it like this: You gave cip a key to your house on the promise that he would take good care of it. If cip gives the key to his no-good brother-in-law ip, who needs a place to stay because he trashed his last one, cip has not really kept his promise, even though he has done no damage himself. This assignment is illegal for the same reason. If cip promised not to change what it points to, it had better not pass the pointer to a variable not bound by that promise.

  3. cip = &m;

    This is legal; cip promises not to change m, but adding a restriction does not break any previous restrictions.

  4. m = f.Size();

    This is legal.

  5. f.Size() = m;

    This is legal, too. Remember that Size() returns a reference.

  6. f.size = m;

    This is illegal because the size field is private.

  7. Fred f; This is illegal because there is no constructor that takes zero arguments. (Such a constructor is called a default constructor.)

<<Bits and Pieces Question Function Overloading>>