In this video, we'll learn some of the terminology and
notation used with inheritance.
The terminology we use is pretty straightforward, but it's
important that it's clear since you'll be using it throughout
your career as a c++ developer.
First, let's review inheritance.
As we've already seen, inheritance is the process of creating new
classes from existing classes.
It's a mechanism that allows us to reuse existing code.
When we use inheritance to create a new class, there may
be several classes involved.
In this course, we'll mainly talk about single inheritance that
is when we create a new class based on another single class.
Multiple inheritance is when we create a new class from
two or more other classes.
The classes that are involved in the inheritance relationship also
have terms associated with them.
The base class is the class being extended or inherited from.
This class is also referred to as the parent class or super class
in the inheritance relationship.
The derived class is the class that's being created from the base class.
This class will inherit the attributes and operations from the base class.
And this class is also referred to as the child class or the subclass.
You can see in the diagram, the commonly used notation used to
express this inheritance relationship.
We draw a solid line between the base class and the derived class
with a closed unfilled arrowhead or triangle pointing to the base class.
This is a uml class diagram and can also show much more information
about the classes we're modeling.
A very important concept to understand is that of an is a
relationship with object-oriented programming and inheritance.
When we use classic or public inheritance, our derived
classes are generally subtypes of their base classes.
This implies an is a relationship.
So a circle is a shape, a savings account is an account.
This very simple concept will provide us with a great deal of power.
We'll learn how to leverage this is a relationship in the next
section of the course when we learn about polymorphic functions.
Generalization is about combining similar classes into a single,
more general class based on the class's common attributes.
The more general class is obviously more abstract and can therefore
potentially be reused more easily.
Specialization is the opposite of generalization.
This is about creating new classes from existing classes.
And these new classes generally add attributes and operations
to the already inherited ones.
As programmers and designers, we use both generalization and specialization
to create inheritance hierarchies.
Inheritance hierarchies is how we organize our inheritance relationships
so that we can more effectively organize and reuse our code.
Let's see a simple example.
In this example, we have classes a b c d, and e and we have public
inheritance that is is a relationship between derived and based classes.
You can see that class a is not being derived from any other class.
This class is often referred to as the root class in the hierarchy.
B is derived from a, c is also derived from a, d is derived
from c and e is derived from d.
You can see the class hierarchy in the diagram.
Notice that the higher up we go in the hierarchy, the
more general our classes are.
And the lower we go in our hierarchy, the more specialized they are.
Also notice the is a relationships.
In this example, b is an a.
C is also an a since they're both derived from the class a, and e is a
d because e is derived from class d.
Now notice that e is also a c since inheritance is transitive.
In fact, in this example, e is also an a.
However, class b is not a c.
There is no inheritance relationship directly between b and c.
Let's look at a more concrete example.
In this example, we have a root class person that's
the most general or abstract class in this class hierarchy.
We can see that student is derived from person.
So a student is a person and inherits the attributes and operations
of the person class.
We also see that employee is a person, and faculty staff
and administrators are derived from employee, so they are employees.
In fact, they're also persons.
But notice that a student is not an employee in this class hierarchy.
Also notice that the is a relationships are not bi-directional.
For example, a person is not necessarily an employee because
they could be a student.
You can see how useful a class diagram can be since it provides a
high level structural overview of the classes and the relationships.
In the next video, we'll look at another way of reusing classes
in c++ called composition and see how it differs from inheritance.