Pages

Tuesday, 26 May 2020

Chapter 14 // Drill 1, 2, 3, 4, 5, 6, 7 - Principles & Practice Using C++

In this exercise I am using Visual Studio 2017 and header file std_lib_facilities.h:
http://stroustrup.com/Programming/PPP2code/std_lib_facilities.h

Chapter 14 // Drill 1

Define a class B1 with a virtual function vf() and a non-virtual function f(). Define both of these functions within class B1. Implement each function to output its name (e.g., B1::vf()). Make the functions public. Make a B1 object and call each function.

Github: https://github.com/l-paz91/principles-practice/blob/master/Chapter%2014/Drills/Drill1


Chapter 14 // Drill 2

Derive a class D1 from B1 and override vf(). Make a D1 object and call vf() and f() for it.


Chapter 14 // Drill 3

Define a reference to B1 (a B1&) and intialise that to the D1 object you just defined. Call vf() and f() for that reference.

Github: https://github.com/l-paz91/principles-practice/blob/master/Chapter%2014/Drills/Drill3


Chapter 14 // Drill 4

Now define a function called f() for D1 and repeat 1-3. Explain the results.

Github: https://github.com/l-paz91/principles-practice/blob/master/Chapter%2014/Drills/Drill4


Chapter 14 // Drill 5

Add a pure virtual function called pvf() to B1 and try to repeat 1-4. Explain the result.

Github: N/A

The compiler will fail to build on this unless you delete the B1 object creation. If any functions in a class are pure virtual then the class cannot be an object by itself. 

Chapter 14 // Drill 6

Define a class D2 derived from D1 and override pvf() in D2. Make an object of class D2 and invoke f(), vf(), and pvf() for it.

Github: https://github.com/l-paz91/principles-practice/blob/master/Chapter%2014/Drills/Drill6

Chapter 14 // Drill 7

Define a class B2 with a pure virtual function pvf(). Define a class D21 with a string data member and a function that overrides pvf(); D21::pvf() should output the value of the string. Define a class D22 that is just like D21 except that its data member is an int. Define a function f() that takes a B2& argument and calls pvf() for its argument. Call f() with a D21 and a D22.

Github: https://github.com/l-paz91/principles-practice/blob/master/Chapter%2014/Drills/Drill7

The wording of this drill reminded me of this scene from friends:

No comments:

Post a Comment