Exercise index of this lecture   Alphabetic index   Course home   

Exercises
Abstraction Mechanisms, Part 2


5.1   Slicing an object during parameter passing.  

In both this the program (and another) 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.

 

Solution


5.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.

 

Solution


5.3   Study the examples of nested classes  

We have provided a number of examples of nested classes: Example 1, example 2, and example 3 on the accompanying slide. Be sure to understand the programs, and explain the findings (as reported in the programs).

 


5.4   Ignoring constructors in virtual base class  

Take a look at the class hierarchy on this slide and in this program - in particular how the class D constructor relies on the default constructors in class B and C. Which constructor is used in the A class?

In this program we show a way out of the problem. Please be sure that you both understand the problem - and the solution.

 

Solution


5.5   Friends and 'enemies' of a class with private and public bases  

In this program from the accompanying slide arrange that the function void frD(D &ad) becomes a friend of class D. (You can just rename the function f to frD and arrange the friendship).

From frD , please access b, c, d in ad. Also from frD , please also 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 (and the errors) you expect?

Can you arrange that frD can access/call all of b, c, d, Bop, Cop, and Dop?

 

Solution


5.6   No single most general class in C++  

Discuss the question asked on this slide.
 

Solution


5.7   A template class with friends  

Arrange that operator<< becomes a friend of the template class Point<C>. It may be useful to consult §C.13.2, page 854-855 in The C++ Prog. Lang. (3. edition). Or Section 23.4.7 in The C++ Prog. Lang. (4. edition), page 682-684.
 

Solution


Generated: Tuesday August 1, 2017, 13:31:10