Study Guide 11 Answers

  1. a
  2. b
  3. b
  4. c
  5. b
  6. a
  7. b; d
  8. d
  9. b
  10. a
  11. a
    1. (b) foo foo::operator+(foo f) { . . . . }
    2. (d) foo operator+(foo f1, foo f2) { . . . . }
    3. (c) friend foo operator+(foo f1, foo f2) { . . . . }
    4. (a) foo operator+(foo f) { . . . . }
  12. friend void harry(george g);
  13. a
  14. b
  15. b
  16. c
  17. a - The second call is not very useful and not used in practice, but it is legal.
  18. b - The first call is legal, but friend functions are not members of a class and cannot be called using the dot (.) operator. The correct syntax for the second call is C = operator+(A,B); (which is not useful nor used in practice).
  19. b - IF operator+ was a member function, the statement could be rewritten as 3.operator(f), which is not possible because '3' is just a plain number, which does not support the dot operator. However, if operator+ is a friend function, then operation can be rewritten as operator+(3,f), which is just fine.
  20. a, b - Any constructor that can be called without arguments is a default constructor.
  21. c, d, e - Broadly, the distinction between a conversion and general constructor is how a program uses them. Nevertheless, conversion constructors frequently have one parameter, which they convert from the parameter type to the defining class type. For example, suppose that class foo has a conversion constructor: foo(bar x);. This constructor converts a bar into a foo. The textbook uses the narrower view as a convenience.
  22. e
  23. b - Satisfying all three calls requires (1) a conversion constructor, and (2) a friend function that takes two arguments of the class type.
  24. d - operator+ is implemented as a friend function (which is not a member) and therefore cannot appear on the left hand side of the dot operator. Choice b calls the constructor.
  25. friend ostream& operator<<(ostream& out, room& me)
    {
        out << me.width << " x " << me.length << endl;
        return out;
    }
  26. friend istream& operator>>(istream& in, room& me)
    {
        in >> me.width;
        in >> me.length;
        return in;
    }
  27. The functions are the same but without the friend keyword.
  28. friend ostream& operator<<(ostream& in, Beta& me)
    {
    	out << (Alpha &)me << " " << me.id << endl;
    	return out;
    }
    1. count(a_count), balance(a_balance)
    2. Bar(count, balance), name(a_name)
    3. (Bar &)f << " " << f.name
    1. f.name
    2. f.my_bar
    1. f.name
    2. if (me.my_bar != nullptr)
    3. *me.my_bar