Deborah R. Fowler
OOP Inheritance for
C++
Posted on Oct 19 2019Updated Oct 20 2019
This section assumes you have read the basics of OOP for C++ and pointers.
INHERITANCE
Inheritance is similar to a linux
directory structure and is often drawn as a tree. A class can
inherit properties from another. The child class (derived)
inherits the properties of the parent (base) class.
Suppose that we have a class called Phone, now we want to have a
child class of Phone that has all the properties of the parent but
additional properties. For example:
A phone allows us to communicate and has a provider however there
can be different types of phones.
What does this looks like in code?
To indicate inheritance the
syntax is class Child : accesstype Parent
Access Level
Let's talk about accesstype for data.There are three choices for access level
- public - everything can see it, which is what we have been using
- private - only members of the class objects can see it
- protected - instances of the class and instances of children of the class can see it
Now what about protected?
You can also have multiple inheritance - click on the image for an example file.
Next up - Splitting into multiple files