Institute of Telematics | University Lübeck
C++ Program
6. Inheritance
Dr.-Ing. Dennis Pfisterer
http://www.itm.uni-luebeck.de/users/pfisterer/
C++ Inheritance 2
C++ central abstraction is a class
Efficient application development by re-using existing, debugged
classes
Two major types of re-use
Composition
Inheritance
Composition expresses a “has a”-relationship
E.g., a house has a lamp
Inheritance expresses an “is a”-relationship
E.g., a BMW is a car
Examples for compositions 3
Simplest form of composition using basic data types
class Lamp
{
private:
int brightness_;
};
Composition using other objects
class House
{
public:
Lamp lamps[100];
};
House h;
h.lamps[0].on();
Examples for compositions 4
Composition providing another interface
class House
{
public:
void switch_off_lamps();
private:
Lamp lamps[100];
};
House h;
h.switch_off_lamps();
C++ Inheritance 5
Inheritance expresses an “is-a“ relation
Creates a relation between different classes
Types: Only a concept (Vehicle) or real object (BMW)
Derived classes
Inherit all members of the base class (data and methods)
Can be used instead of the base class
Example
Vehicle
Car Lorry Bike
BMW VW
C++ Inheritance: Syntax 6
Syntax
class Child : [access-control] Parent
{…};
Example
class Parent { … };
class ChildPublic : public Parent { … };
class ChildProtected : protected Parent { … };
class ChildPrivate : private Parent { … };
Example: intro.cpp
C++ Inheritance: Access-control 7
class Child : public Parent {
// Access to pub und prot
class Parent // External access to pub
{ // Subclasses: Access to pub und prot
private: };
int priv;
class Child : protected Parent {
protected: // Access to pub und prot
int prot; // No external access to any variable
// Subclasses: Access to pub und prot
public: };
int pub;
}; class Child : private Parent {
// Access to pub und prot
// No external access to any variable
// Subclasses: No access to any variable
};
Example: accesscontrol.cpp
C++ Inheritance: Invoke parent constructors 8
Constructors of base classes must be supplied with parameters
Base classes must be constructed before the sub-class
Example
class Parent
{
Parent(int i) { … }
};
class Child : public Parent
{
Child() { … }
};
How to supply the int value to Parent(int i)?
Not possible in the construction implementation
C++ Inheritance: Invoke parent constructors 9
Constructors of base classes are parameterized using member
initialization lists
Example
class Parent
{
Parent(int i) { … }
};
class Child : public Parent
{
Child() : Parent(0) { … }
Child(int i) : Parent(i) { … }
};
C++ Inheritance: Invoke member constructors 10
Same solution for members having constructors with parameters
Example
class Lamp
{
Lamp(int brightness) { … }
};
class Parent
{
Parent(int i) { … }
};
class Child : public Parent
{
Lamp lamp;
Child() : Parent(0), lamp(0) { … }
Child(int i, int brightness) : Parent(i), lamp(brightness) { … }
};
11
C++ Inheritance: Order of constructors and destructors
Demo: initialization.cpp
C++ Inheritance: Name hiding 12
When inheriting from another class, the same member signatures
may by re-used
This process is called redefinition
Different return type and argument list possible
Example
class Base
{
public: void f(int i);
};
class Derived : public Base
{
public: void f(int i);
};
Derived d;
d.f();
Example: namehiding.cpp
Example: namehiding2.cpp
C++ Inheritance: Multiple Inheritance 13
A class may inherit from several classes
Example A B C
class D : public A, public B, public C {};
class B : public A {}; D
A,B,C
class C : public A {};
class D : public B, public C {};
A A
Problem: Ambiguities
Imagine all classes have public member int x B C
- d.x = 1; //Error: Which x?
Solution: Use qualified name
D
B::A, C::A, B, C
- d.C::x = 1; //OK
- d.B::A::x = 1; //OK
Example: multipleinheritance.cpp
C++ Inheritance: Virtual inheritance 14
Multiple Inheritance
Detailed Knowledge required to access parent members
Often there is no need for a duplicate set of members
Virtual inheritance
Only one set of members available, no matter how often a base
class is sub-classed
No ambiguities when using members from the base class
(Solvable) problems when passing parameters to base constructors
B: public virtual A C : public virtual A
D
A, B, C
Example: virtualinheritance.cpp
15
C++ Inheritance: Interchange base and derived Classes
Implicit conversion of pointers possible
Type of pointer determines which method is called
Base* b = new Base; Base
Derived* d = new Derived;
Derived Implicit
Base* base;
Derived* derived;
Base* Derived*
base = d; Base& Derived&
OK
derived = b;
Error Explicit
derived = (Derived*)(b);
Dangerous
Example: pointers.cpp
C++ Inheritance: Pointers to classes 16
Its safe to use instances of sub-classes for pointers of base type
Base *base_ptr = &derived_instance;
Called “upcasting”
The compiler only knows that is deals with a Base* pointer
Type information of the pointed-at object is lost
The type of the pointer decides which function is called,
not the type of the pointed-at instance
Derived
Memory address
Base Base
Base* Derived* Base*
C++ Inheritance: Static Binding 17
Compiler decides at compile-time which method is invoked
Depending on the type of the variable, pointer or reference
Example
class A {
void f () { ... } // A::f
};
class B : public A {
void f () { ... } //Redefinition: B::f redefines A::f
};
A *a;
B *b;
a = new A;
a->f(x); //Invokes A::f
b = new B;
b->f(x); //Invokes B::f, A::f is hidden
a = new B; //UPCASTING !!!
a->f(x); //Invokes A::f, since a is of type A*
C++ Inheritance: Static Binding 18
Static Binding
Compiler decides at compile-time
which method is invoked
The type of the pointer or
reference is important, not the
type of the pointed-at object