Kurt Nørmark
Department of Computer Science, Aalborg University
Abstract Previous lecture Next lecture Index References Contents | In this lecture we explore inheritance in C++, in particular multiple inheritance. |
Inheritance in C++ Slide Annotated slide Contents Index References |
|
|
Inheritance: Constructors and destructors Slide Annotated slide Contents Index References |
|
|
|
|
Program: Constructors and single inheritance. |
|
Program: Constructors and multiple inheritance. |
|
Program: Constructors and multiple inheritance - implicit activation of default constructor in Base. |
|
Program: Constructors and multiple inheritance - order of construction and destruction. |
|
Program: Constructors and multiple inheritance - order of construction and destruction. |
|
Inheritance: Copying and slicing Slide Annotated slide Contents Index References |
|
|
|
Program: Illustration of object slicing. |
|
Program: Program output. |
|
Program: Does this variant change the game?. |
|
Program: Program output. |
|
Exercise 4.2. 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. |
Member access control: private, public and protected Slide Annotated slide Contents Index References |
|
|
|
|
Virtual Functions Slide Annotated slide Contents Index References |
|
|
|
The class B is a specialization of class A |
Program: Virtual versus non-virtual functions in an AB class hierarchy. |
|
Program: Program output. |
|
Destructors and Inheritance - Virtual Destructors Slide Annotated slide Contents Index References |
|
|
Program: Base class A and derived class B with non-virtual destructors - motivation. |
|
Program: Program output. |
|
Program: Base class A and derived class B with virtual destructors. |
|
Program: Program output. |
|
Program: Base class A and derived class B and C with virtual destructors. |
|
Program: Program output. |
|
Hiding inherited names Slide Annotated slide Contents Index References |
|
|
Program: A class B inherits two virtual, overloaded functions from A - straightforward - no problems. |
|
Exercise 4.3. 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. |
Abstract Classes Slide Annotated slide Contents Index References |
|
|
|
Program: A variant were vf is a pure virtual function in class A. |
|
What about interfaces in C++? Slide Annotated slide Contents Index References |
|
|
Program: A sample C# interface. |
|
Program: The C++ counterpart to the C# Interface. |
|
Program: A C++ class that 'implements the interface' and uses the resulting class. |
|
What about nested classes in C++? Slide Annotated slide Contents Index References |
|
|
|
Program: Class Outer that contains class Inner - does not compile. |
|
Program: Class Outer that contains class Inner - friends of each other. |
|
|
Program: A variant where class Inner is private in Outer - does not compile. |
|
Program: Inner attempts to access to non-static variable in Outer - does not compile. |
|
Program: Problems solved - This program compiles. |
|
What about prevention of derivation in C++ Slide Annotated slide Contents Index References |
|
|
Pointer to members Slide Annotated slide Contents Index References |
|
|
Program: Class variant of class Point with several different move functions. |
|
Program: Implementation of class Point and the tree move methods - not important for the example. |
|
Program: A program that illustrates pointer to Point member functions. |
|
|
Multiple inheritance - issues and C++ solutions Slide Annotated slide Contents Index References |
|
Class B is a subclass of class A |
|
|
Multiple inheritance - issues and C++ solutions Slide Annotated slide Contents Index References |
|
Class B is a subclass of class A |
|
|
Multiple inheritance: Ambiguities Slide Annotated slide Contents Index References |
|
Use of the function operator() is ambiguous |
Program: Ambiguity - the compiler locates the problem. |
|
Program: Ambiguity resolution in the client of C. |
|
Program: Involving polomorphism - but there is a problem. |
|
Program: Involving polomorphism - problem solved. |
|
Replicated base class Slide Annotated slide Contents Index References |
|
|
The base class A is replicated |
Program: Illustration of replication of class A. |
|
Program: Program output. |
|
Is repeated inheritance possible? Slide Annotated slide Contents Index References |
|
The base class A is replicated |
Program: Attempting repeated inheritance in C++. |
|
Is repeated inheritance possible? No! Slide Annotated slide Contents Index References |
|
The base class A is replicated |
|
Shared base class: Virtual base Slide Annotated slide Contents Index References |
|
|
|
The base class A is replicated |
Program: Illustration of shared, virtual, base class A. |
|
Program: Program output. |
|
|
Base class access Slide Annotated slide Contents Index References |
|
|
The class B is a specialization of class A |
Base class access Slide Annotated slide Contents Index References |
|
|
The class B is a specialization of class A |
|
Base class access - the C++ rules Slide Annotated slide Contents Index References |
The class B is a specialization of class A |
|
|
|
Base class access - Examples Slide Annotated slide Contents Index References |
|
The base class A is replicated |
Program: Class D inherits privately from B and publically from C. |
|
Program: Same setup: Which variables can access which objects. |
|
Program: Same as above - with answers. |
|
Exercise 4.4. 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? |
Discussion: No single most general class in C++ Slide Annotated slide Contents Index References |
|
|
Exercise 4.5. No single most general class in C++ | Discuss the question asked on this slide. |
Collected references Contents Index |
|
Chapter 4: Abstraction Mechanisms, Part 2
Course home Author home About producing this web Previous lecture (top) Next lecture (top) Previous lecture (bund) Next lecture (bund)
Generated: March 26, 2013, 13:03:41