MC logo

Bits and Pieces (Answer)

  Practice Questions

  1. The main advantage of iostream over printf is that it avoids errors caused when the type and number of items implied by the printf format does not agree with the type and number actually sent. This makes the code compile fine, but behave oddly. With iostream, the items are printed according to their actual type and number, and there is no format string to mess up.

  2. The const in void f(const Fred &z) indicates the f will not change the object it is sent (the object z is aliased to).

  3. The const in const Fred & f(int n) is part of the return type. The function returns a reference to Fred, and the caller may not change the object referred to.

  4. The const in void f(Fred &z) const { ... } is allowed only when f is a member function. It means that f does not change the object to which it belongs. For instance, the call a.f(b) could never change a.

  5. The declaration int n makes n an integer variable and, &n is it's address. The expression &n is a pointer to int which points to n.

  6. The declaration int & n declares n to be a reference to int. It is probably a parameter declaration.

  7. The :: is called the Scope Resolution Operator, and it is used to indicate that a name belongs to a particular scope, usually a class. The most common use is to define member functions when the member function definition is not physically contained in the class definition.

Question Access Control>>