Hello again! In this video, we are going to look at classes and inheritance. When we are working with classes
in programs, we often find there are some classes which are related, or represent similar concepts.
For example, with graphical shapes, they will all have some position on the screen.
We can draw them, we can fill them in with different colours.
We can rotate them, and so on. With document elements, such as text, tables and diagrams,
we can format these.
We can move them around, we can zoom in.
With input streams in the C++ library, we can process input from different sources and convert it to data.
And we can use a class hierarchy to express these relationships between different classes, and make
it easier to reuse code between them.
So, for example, with our shapes, we could have a basic shape, and then some more specialized shapes
like circles, square and triangle.
So this is a hierarchy of shape classes.
For input
streams, we can have istream as the basic input stream, and then ifstream is specialized for working with
files, and istringstream is specialized for working with strings.
In computer science, trees have their roots at the top and class hierarchies have their base at the
top.
So the class at the top of the hierarchy - the shape or the istream - is called the base class.
So the base class is the most generic or basic version. The classes which come below this are known as derived
classes, or subclasses of the base class.
We say that they "inherit from" the base class or are "derived" from it.
And these are more specialized versions of the base class, or classes which have more features. Enhanced
versions.
With shape, we have circle, triangle, square, which are derived classes from shape.
So these are all specific kinds of shape.
For istream, we have ifstream and istringstream, and these are more specialized input streams.
The relationship between the classes at the different levels in the hierarchy is called inheritance
in terms of object modelling, this represents an "is-a" or "is-kind-of" relationship between the classes.
So an istringstream is a kind of istream, and a circle is a shape.
Okay, so that is it for this video.
I will see you next time, but until then, keep coding!