Introduction to inheritance
1. Why do we need inheritance in C++?
Inheritance allows us to create objects based on the qualities another object has. These ‘child’ objects effectively inherit properties and behaviours belonging to their ‘parent’ object.
2. Give a simple example of objects in the real world that could be modelled in C++ using inheritance.
One real life example could be a Gran Turismo Maserati, which is a Car, which is a Motor vehicle.
A Gran Turismo Maserati inherits properties from a Car, such as 4 wheels and a steering wheel, as well as what a Car inherits from a Motor vehicle, such as an engine.
Basic inheritance in C++
1. What is a superclass and what is a subclass?
A superclass is the class the contains the properties that will be inherited.
A subclass is the class that inherits the properties from the superclass.
2. How would you write a public inheritance in C++?
When defining the class, we type a colon after the class name, followed by the public keywords and the name of the superclass we will be inheriting from.
e.g. class Bird : public Animal {
3. In what ways can a subclass extend the functionality of a superclass?
While a subclass inherits all the functionality and properties of its superclass, it can also be given new properties and functionality on top of those. What’s more, if other properties and functionality is added to the superclass at a later time, they will automatically be inherited by the subclass.