- a
- b
- b
- c
- b
- a
- b; d
- d
- b
- a
- a
- (b) foo foo::operator+(foo f) { . . . . }
- (d) foo operator+(foo f1, foo f2) { . . . . }
- (c) friend foo operator+(foo f1, foo f2) { . . . . }
- (a) foo operator+(foo f) { . . . . }
- friend void harry(george g);
- a
- b
- b
- c
- a - The second call is not very useful and not used in practice, but it is legal.
- 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).
- 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.
- a, b - Any constructor that can be called without arguments is a default constructor.
- 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.
- e
- b - Satisfying all three calls requires (1) a conversion constructor, and (2) a friend function that takes two arguments of the class type.
- 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.
friend ostream& operator<<(ostream& out, room& me)
{
out << me.width << " x " << me.length << endl;
return out;
}
friend istream& operator>>(istream& in, room& me)
{
in >> me.width;
in >> me.length;
return in;
}
- The functions are the same but without the friend keyword.
friend ostream& operator<<(ostream& in, Beta& me)
{
out << (Alpha &)me << " " << me.id << endl;
return out;
}
-
- count(a_count), balance(a_balance)
- Bar(count, balance), name(a_name)
- (Bar &)f << " " << f.name
-
- f.name
- f.my_bar
-
- f.name
- if (me.my_bar != nullptr)
- *me.my_bar