Programmers seem to hate coding something from scratch; why code the same thing over and over again? Fortunately, it is possible to reuse code, thanks to one of the key concepts of object-oriented programming. That is inheritance, which is the subject of this article. So if you're looking for a clear explanation of how inheritance works in C++, keep reading.
Inheritance in C++ - Inheritance Types (Page 2 of 5 )
Just like the member functions, variables can be of three types; inheritance can also be made in three different ways. The effect of the type of the inheritance you use will reflect on the inherited type’s traits.
The table below shows how this works.
In order to explicitly specify the type of inheritance, you are going to need to add the public/private/protected keyword in front of the derived class. By default, this is private. Therefore, the correct way to inherit from the GeEntity is by using the public keyword:
class GeCircle : public GeEntity
// write the class specifics };
In this way we can use all of the base’s members, just like we would if we own the class, unless that was private. Private members are not inherited regardless of the inheritance type because that would violate the private statement. It would not be private, as a derived class can access it freely. Specify protected access when you want to hide some functions from the user but not from its base classes. This lets you access any internal data/functions of the base class by the derived class and its friends.
Here it is important for you to understand the “is a” and “has a” relationship between classes. When an object inherits a class that is a type of the base class, it is that class; for example, a Circle is a Geometric Entity. But when you declare, as a member variable, a class inside a new class, you're looking at a "has a" relationship. A car has a steering wheel, one engine, and so forth. Private inheritance essentially provides another way to create the composition.