1.Why do we need inheritance in C++?
We need inheritance in order to transfer properties between classes. As it is in the real world, we are unable to fit every single description of every object into one classification. Often, there are sub categories of objects that exist, where they might share similar properties, but still have their differences. We can use inheritance to express such a difference.
2.Give a simple example of objects in the real world that could be modelled in C++ using inheritance.
One example could be the animal kingdom. While every living being on the plant is classified under “Life”. We then move down the taxonomic rank and go down to the kingdoms of animals, plants, fungis etc. The are further split down the line. In this case, objects are things like a lion and a tiger, would might be from the same animal kingdom, and from the cat family, but still have their differences.
1.What is a superclass and what is a subclass?
A superclass is like a parent class, where it is a more general and overarching class of objects. Subclasses are derived from a superclass, where they inherit properties belonging to the superclass and might have additional unique properties.
2.How would you write a public inheritance in C++?
A public inheritance can be written by first declaring a new class, naming it, and then putting a colon, the word “public” and the superclass that we wish to inherit properties from. An example would be:
class Rectangle : public FourSidedShapes
3.In what ways can a subclass extend the functionality of a superclass?
A subclass is able to inherit whatever existing properties from the superclass, plus add more functions and variables of its own into the mix.