4.1 Slicing an object during parameter passing.
On the program that belongs to the accompanying slide the parameter of the function f is passed by C++ reference.
Now assume that the parameter is passed by value:
int f(A x){ A y = x, *z = &x, &w = x; cout << y.op() << endl; cout << z->op() << endl; cout << w.op() << endl; }
What will be the output of the 3 output statements at the bottom of f. Please predict the result before you run the program.
4.2 Hiding inherited names
This is an exercise about an apparently weird happening in a C++ program with overloaded functions. It is good for you to wonder - and realize what happens. The solution is not not necessarily easy to find 'without cheating'...
The program shown together with this slide illustrates a simple situation when the derived class B inherits two virtual functions from a base class A. Everething is fine.
Redefine (override) vf in the derived class B, and keep the main function unchanged? Is everything still fine?
Discuss potential problems. Can you explain your experiences. Can you find a way to solve the problems you may have encountered?
If you have the book Effective C++, Third edition you are encouraged to read item 33.
4.3 Friends and 'enemies' of a class with private and public bases
In this program from the accompanying slide introduce a friend function void frD(D &ad) of class D. (You can just delete the function f).
From frD access b, c, d in ad. Also from frD call the member functions Bop, Cop, and Dop on ad.
From an identical non-friend function (let us just call it an enemy) do exactly the same as in FrD.
Do you get the results you expect?
Can you arrange that frD can access/call all of b, c, d, Bop, Cop, and Dop?
4.4 No single most general class in C++
Generated: Tuesday March 26, 2013, 13:03:43