C++ Programming: From Problem Analysis To Program Design, Fifth Edition
C++ Programming: From Problem Analysis To Program Design, Fifth Edition
C++ Programming: From Problem Analysis To Program Design, Fifth Edition
Objectives
In this chapter, you will: Learn about inheritance Learn about derived and base classes Explore how to redefine the member functions of a base class Examine how the constructors of base and derived classes work Learn how to construct the header file of a derived class
C++ Programming: From Problem Analysis to Program Design, Fifth Edition
2
Objectives (cont'd.)
Become familiar with the C++ stream hierarchy Explore three types of inheritance: public, protected, and private Learn about composition (aggregation) Become familiar with the three basic principles of object-oriented design
Introduction
The two common ways to relate two classes in a meaningful way are:
Inheritance (is-a relationship) Composition (has-a relationship)
Inheritance
Inheritance is an is-a relationship
Example: every employee is a person
Inheritance (cont'd.)
Single inheritance: derived class has a single base class Multiple inheritance: derived class has more than one base class Public inheritance: all public members of base class are inherited as public members by derived class
Inheritance (cont'd.)
Inheritance can be viewed as a tree-like, or hierarchical, structure wherein a base class is shown with its derived classes
Inheritance (cont'd.)
General syntax of a derived class:
The private members of a base class are private to the base class
Derived class cannot directly access them
C++ Programming: From Problem Analysis to Program Design, Fifth Edition
8
Inheritance (cont'd.)
public members of base class can be inherited as public or private members The derived class can include additional members--data and/or functions The derived class can redefine the public member functions of the base class
Applies only to the objects of the derived class
All members of the base class are also member variables of the derived class
C++ Programming: From Problem Analysis to Program Design, Fifth Edition
9
If derived class overrides a public member function of the base class, then to call the base class function, specify:
Name of the base class Scope resolution operator (::) Function name with appropriate parameter list
C++ Programming: From Problem Analysis to Program Design, Fifth Edition
10
11
12
13
Call to base class constructor is specified in heading of derived class constructor definition
15
16
17
18
21
22
23
25
Composition (Aggregation)
In composition, one or more member(s) of a class are objects of another class type Composition is a has-a relation Arguments to the constructor of a member-object are specified in the heading part of the definition of the constructor
29
30
31
32
33
34
35
36
39
Suppose we want to write a program that calculates and prints the volume and surface area of a cylinder
C++ Programming: From Problem Analysis to Program Design, Fifth Edition
40
After identifying a class, determine three pieces of information about its objects:
Operations that an object can perform Operations that can be performed on an object Information that an object must maintain
C++ Programming: From Problem Analysis to Program Design, Fifth Edition
42
43
Print: display the volume and the surface area on an output device
C++ Programming: From Problem Analysis to Program Design, Fifth Edition
44
45
46
48
50
51
Student
Main characteristics of a student are: student name, student ID, number of courses enrolled, name courses, and grade for each course
C++ Programming: From Problem Analysis to Program Design, Fifth Edition
52
53
54
55
56
58
59
60
Summary
Inheritance and composition are meaningful ways to relate two or more classes Inheritance is an is-a relation
Single inheritance: a derived class is derived from one class, called the base class Multiple inheritance: a derived class is derived from more than one base class
Summary (cont'd.)
Private members of a base class are private to the base class Public members of a base class can be inherited either as public or private Derived class can redefine function members of a base class
Redefinition applies only to objects of derived class
C++ Programming: From Problem Analysis to Program Design, Fifth Edition
63
Summary (cont'd.)
A call to a base class constructor (with parameters) is specified in the heading of the definition of the derived class constructor When initializing object of a derived class, the base class constructor is executed first In composition
Class member is an object of another class Call to constructor of member objects is specified in heading of the definition of classs constructor
C++ Programming: From Problem Analysis to Program Design, Fifth Edition
64
Summary (cont'd.)
Three basic principles of OOD are:
Encapsulation Inheritance Polymorphism
Finding classes:
Describe the problem Choose classes from the list of nouns Choose operations from the list of verbs
C++ Programming: From Problem Analysis to Program Design, Fifth Edition
65