Professional Documents
Culture Documents
Single & Multiple Inheritance in C++
Single & Multiple Inheritance in C++
Background
Object-oriented programming is often defined as the combination of
Abstract Data Types (ADTs) with Inheritance & Dynamic Binding
Single & Multiple Inheritance in C++
Each concept addresses a different aspect of system
decomposition:
Douglas C. Schmidt
1. ADTs decompose systems into two-dimensional grids of modules
Professor Department of EECS – Each module has public & private interfaces
d.schmidt@vanderbilt.edu Vanderbilt University 2. Inheritance decomposes systems into three-dimensional
www.dre.vanderbilt.edu/schmidt/ (615) 343-8197 hierarchies of modules
– Inheritance relationships form a lattice
3. Dynamic binding enhances inheritance
– e.g., defer implementation decisions until late in the design
phase or even until run-time!
Copyright c 1997-2006 Vanderbilt University 1
Douglas C. Schmidt OO Programming with C++ Douglas C. Schmidt OO Programming with C++
Copyright c 1997-2006 Vanderbilt University 2
Copyright c 1997-2006 Vanderbilt University 3
Douglas C. Schmidt OO Programming with C++ Douglas C. Schmidt OO Programming with C++
Copyright c 1997-2006 Vanderbilt University 4
Copyright c 1997-2006 Vanderbilt University 5
Douglas C. Schmidt OO Programming with C++ Douglas C. Schmidt OO Programming with C++
Copyright c 1997-2006 Vanderbilt University 6
Copyright c 1997-2006 Vanderbilt University 7
Douglas C. Schmidt OO Programming with C++ Douglas C. Schmidt OO Programming with C++
Copyright c 1997-2006 Vanderbilt University 8
Copyright c 1997-2006 Vanderbilt University 9
Douglas C. Schmidt OO Programming with C++ Douglas C. Schmidt OO Programming with C++
Copyright c 1997-2006 Vanderbilt University 10
Copyright c 1997-2006 Vanderbilt University 11
Douglas C. Schmidt OO Programming with C++ Douglas C. Schmidt OO Programming with C++
Copyright c 1997-2006 Vanderbilt University 12
Copyright c 1997-2006 Vanderbilt University 13
Douglas C. Schmidt OO Programming with C++ Douglas C. Schmidt OO Programming with C++
Copyright c 1997-2006 Vanderbilt University 14
Copyright c 1997-2006 Vanderbilt University 15
Douglas C. Schmidt OO Programming with C++ Douglas C. Schmidt OO Programming with C++
Copyright c 1997-2006 Vanderbilt University 16
Copyright c 1997-2006 Vanderbilt University 17
Douglas C. Schmidt OO Programming with C++ Douglas C. Schmidt OO Programming with C++
Copyright c 1997-2006 Vanderbilt University 18
Copyright c 1997-2006 Vanderbilt University 19
Douglas C. Schmidt OO Programming with C++ Douglas C. Schmidt OO Programming with C++
Copyright c 1997-2006 Vanderbilt University 20
Copyright c 1997-2006 Vanderbilt University 21
Douglas C. Schmidt OO Programming with C++ Douglas C. Schmidt OO Programming with C++
Copyright c 1997-2006 Vanderbilt University 22
Copyright c 1997-2006 Vanderbilt University 23
Douglas C. Schmidt OO Programming with C++ Douglas C. Schmidt OO Programming with C++
Copyright c 1997-2006 Vanderbilt University 24
Copyright c 1997-2006 Vanderbilt University 25
Douglas C. Schmidt OO Programming with C++ Douglas C. Schmidt OO Programming with C++
Animal
Derived classes add state variables and/or operations to the
Advantages properties and operations associated with the base class
Vertebrate Invertebrate
– Share code & – Note, the interface is generally widened!
set-up dynamic – Data member & method access privileges may also be modified
Reptile Mammal Bird Fish
binding
Extension/generalization is often used to faciliate reuse of
Rodents Primates Pachyderms
– Model & classify implementations, rather than interface
external objects
Apes Humans Sloths – However, it is not always necessary or correct to export interfaces
with design &
from a base class to derived classes
Males Females implementation
Bob Carol
Ted Alice
Copyright c 1997-2006 Vanderbilt University 26
Copyright c 1997-2006 Vanderbilt University 27
OO Progr
Douglas C. Schmidt OO Programming with C++
RELATIONSHIP
DESCENDANT
Vector
Using class Vector as a private base class for derived class
Ada
Vanderbilt University
Stack:
Checked
– class Stack : private Vector { /* . . . */ };
Vector
descendant/Is-A relationship . . .
composition/Has-A rather than a
In this case, Vector’s operator[] may be reused as an
Vector
(cont’d)
implementation for the Stack push & pop methods
– Note that using private inheritance ensures that operator[]
RELATIONSHIP
does not appear in class Stack’s interface!
Vector
CONSUMER
Copyright c 1997-2006
Douglas C. Schmidt
Stack
Copyright c 1997-2006 Vanderbilt University 28
Douglas C. Schmidt OO Programming with C++ Douglas C. Schmidt OO Programming with C++
Copyright c 1997-2006 Vanderbilt University 30 Copyright c 1997-2006
Vanderbilt University 31
Douglas C. Schmidt OO Programming with C++ Douglas C. Schmidt OO Programming with C++
Copyright c 1997-2006 Vanderbilt University 32
Copyright c 1997-2006 Vanderbilt University 33
Douglas C. Schmidt OO Programming with C++ Douglas C. Schmidt OO Programming with C++
Copyright c 1997-2006 Vanderbilt University 34
Copyright c 1997-2006 Vanderbilt University 35
Douglas C. Schmidt OO Programming with C++ Douglas C. Schmidt OO Programming with C++
Copyright c 1997-2006 Vanderbilt University 36
Copyright c 1997-2006 Vanderbilt University 37
Douglas C. Schmidt OO Programming with C++ Douglas C. Schmidt OO Programming with C++
Copyright c 1997-2006 Vanderbilt University 38
Copyright c 1997-2006 Vanderbilt University 39
Douglas C. Schmidt OO Programming with C++ Douglas C. Schmidt OO Programming with C++
Copyright c 1997-2006 Vanderbilt University 40
Copyright c 1997-2006 Vanderbilt University 41
Douglas C. Schmidt OO Programming with C++ Douglas C. Schmidt OO Programming with C++
Copyright c 1997-2006 Vanderbilt University 42
Copyright c 1997-2006 Vanderbilt University 43
Douglas C. Schmidt OO Programming with C++ Douglas C. Schmidt OO Programming with C++
Derived Class Access to Base Class Members Other Uses of Access Control Specifiers
Base Class Inheritance mode Selectively redefine visibility of individual methods inherited from
Access Control public protected private base classes. NOTE: the redifinition can only be to the visibility of
public public protected private the base class. Selective redefinition can only override the
protected protected protected private additional control imposed by inheritance.
private none none none
class A { class B : private A {
public: public:
The vertical axis represents the access rights specified in the base
int f (void); A::f; // Make public
class
int g_; protected:
The horizontal access represents the mode of inheritance used by . . . A::g_; // Make protected
the derived class private: };
int p_;
Note that the resulting access is always the most restrictive of the };
two
Copyright c 1997-2006 Vanderbilt University 44
Copyright c 1997-2006 Vanderbilt University 45
Douglas C. Schmidt OO Programming with C++ Douglas C. Schmidt OO Programming with C++
Common Issues with Access Control Specifiers General Rules for Access Control Specifiers
It is an error to increase the access of an inherited method above Private methods of the base class are not accessible to a derived
the level given in the base class class (unless the derived class is a friend of the base class)
Deriving publicly & then selectively decreasing the visibility of base If the subclass is derived publicly then:
class methods in the derived class should be used with caution:
1. Public methods of the base class are accessible to the derived
removes methods from the public interface at lower scopes in the
class
inheritance hierarchy.
2. Protected methods of the base class are accessible to derived
classes & friends only
// Error if p_ is class B : public A {
// protected in A! private:
class B : private A { A::f; // hides A::f
public: };
A::p_;
};
Copyright c 1997-2006 Vanderbilt University 46
Copyright c 1997-2006 Vanderbilt University 47
Douglas C. Schmidt OO Programming with C++ Douglas C. Schmidt OO Programming with C++
Copyright c 1997-2006 Vanderbilt University 48
Copyright c 1997-2006 Vanderbilt University 49
Douglas C. Schmidt OO Programming with C++ Douglas C. Schmidt OO Programming with C++
Copyright c 1997-2006 Vanderbilt University 50
Copyright c 1997-2006 Vanderbilt University 51
Douglas C. Schmidt OO Programming with C++ Douglas C. Schmidt OO Programming with C++
Copyright c 1997-2006 Vanderbilt University 52
Copyright c 1997-2006 Vanderbilt University 53
Douglas C. Schmidt OO Programming with C++ Douglas C. Schmidt OO Programming with C++
Copyright c 1997-2006 Vanderbilt University 54
Copyright c 1997-2006 Vanderbilt University 55
Douglas C. Schmidt OO Programming with C++ Douglas C. Schmidt OO Programming with C++
Virtual Base Class Initialization Example Virtual Base Class Initialization Example, (cont’d)
#include <iostream.h> class Derived : public Derived1, public Derived2 {
class Base { public:
public:
// The Derived constructor _must_ call the Base
Base (int i) { cout << "Base::Base (" << i << ")" << endl; }
// constructor explicitly, because Base doesn’t
};
// have a default constructor.
class Derived1 : public virtual Base { Derived (void) : Base (3) {
public: cout << "Derived (void)" << endl;
Derived1 (void) : Base (1) { cout << "Derived1 (void)" << endl; } }
}; };
class Derived2 : public virtual Base {
public:
Derived2 (void) : Base (2) { cout << "Derived2 (void)" << endl; }
};
Copyright c 1997-2006 Vanderbilt University 56
Copyright c 1997-2006 Vanderbilt University 57
Douglas C. Schmidt OO Programming with C++ Douglas C. Schmidt OO Programming with C++
Copyright c 1997-2006 Vanderbilt University 58
Copyright c 1997-2006 Vanderbilt University 59
Douglas C. Schmidt OO Programming with C++ Douglas C. Schmidt OO Programming with C++
Copyright c 1997-2006 Vanderbilt University 60
Copyright c 1997-2006 Vanderbilt University 61
Douglas C. Schmidt OO Programming with C++ Douglas C. Schmidt OO Programming with C++
Copyright c 1997-2006 Vanderbilt University 62
Copyright c 1997-2006 Vanderbilt University 63
Douglas C. Schmidt OO Programming with C++ Douglas C. Schmidt OO Programming with C++
Copyright c 1997-2006 Vanderbilt University 64
Copyright c 1997-2006 Vanderbilt University 65
Summary
Inheritance supports evolutionary, incremental development of
reusable components by specializing and/or extending a general
interface/implementation
Inheritance adds a new dimension to data abstraction, e.g.,
– Classes (ADTs) support the expression of commonality where the
general aspects of an application are encapsulated in a few base
classes
– Inheritance supports the development of the application by
extension and specialization without affecting existing code . . .
Without browser support, navigating through complex inheritance
hierarchies is difficult . . . tools can help.
Copyright c 1997-2006 Vanderbilt University 66