Inheritance Part 2
Inheritance Part 2
INHERITA NCE
C++ Programming: From Problem Analysis to Program Design,
2
Fourth Edition
Inheritance
• Inheritance is an “is-a” relationship
• Example: “every employee is a person”
• Inheritance lets us create new classes from existing
classes
• New classes are called the derived classes
• Existing classes are called the base classes
• Derived classes inherit the properties of the base classes
Examples
Base class Derived classes
• Circle
• Shape
• Rectangle
• Faculty Member
• Employee • Staff member
C++ Programming: From Problem Analysis to Program Design,
4
Fourth Edition
Inheritance (continued)
• Inheritance can be viewed as a tree-like, or hierarchical,
structure wherein a base class is shown with its derived
classes
Inheritance (continued)
• Single inheritance: derived class has a single base class
• Ex. Circle class from Shape class
The left diagram illustrates a “single inheritance”, and the right one a
“multiple inheritance” or “multi-inheritance”.
We’ll show you some examples of both types of inheritance.
We can also write about super classes as base classes, and subclasses
as derived classes.
refercence: www.netacad.com
refercence: www.netacad.com
101
C++ Programming: From Problem Analysis to Program Design,
9
Fourth Edition
Inheritance (continued)
• General syntax of a derived class:
The difference from the notation we used before is in the fact that we have to:
place a colon after the subclass class name
optionally place a so-called visibility specifier (we’ll return to this soon)
add a superclass name
If there’s more than one superclass, we have to enlist them all using commas as
separators, like this:
class A : X, Y, Z { … };
Let’s start with the simplest possible case.
refercence: www.netacad.com
z
printA
setA
var1
objB
derived
var2
printB
setB
A
x
y
printA
setA
x objA
y
z
printA
setA
var1 objB
Can not access x, y
var2
printB
setB
printA
setA
x objA
y
z
printA
setA
var1 objB
var2
printB
setB
printA
setA
var1 objB
var2
printB
setB
printA
setA
refercence: www.netacad.com
Inheritance (continued)
• public members of base class can be inherited as
public or private members
var2
print
setB
A::print
setA
C++ Programming: From Problem Analysis to Program Design,
27
Fourth Edition
objB
3 x
4 y
• If memberAccessSpecifier is public:
• public members of A are public members of B and can be
directly accessed in class B
• protected members of A are protected members of B and can
be directly accessed by member functions (and friend functions) of
B
• private members of A are hidden in B and can be accessed by
member functions of B through public or protected members of
A
C++ Programming: From Problem Analysis to Program Design,
34
Fourth Edition
• If memberAccessSpecifier is protected:
• public members of A are protected members of B and can
be accessed by the member functions (and friend functions) of
B
• protected members of A are protected members of B and
can be accessed by the member functions (and friend
functions) of B
• private members of A are hidden in B and can be accessed
by member functions of B through public or protected
members of A
C++ Programming: From Problem Analysis to Program Design,
35
Fourth Edition
• If memberAccessSpecifier is private:
• public members of A are private members of B and can be
accessed by member functions of B
• protected members of A are private members of B and
can be accessed by member functions (and friend functions) of
B
• private members of A are hidden in B and can be accessed
by member functions of B through public/protected
members of A
refercence: www.netacad.com
storage = 3
safe = 5