Course 5 – Inheritance and access modifiers

In this course, we’ll continue our adventure with classes.
Let’s go to the example i gave last course:

The keyword public refers to the fact that any functions and variables that comes after it will have public visibility.
This means that these functions and variables can be used from any place we have access to an instance of such class, for example, in the main function.

Class instantiation

In main, we can instantiate an object of that class.
SomeName someInstance;

This is possible because a constructor and destructor is generated by the compiler.
The final class looks like this:

As you can see, both of them have the name of the class, and they are used by the compiler to create an instance of our object and to destroy it.

If we don’t want others to have access to our members and functions, we can use the private modifier.

We cannot access myInt directly, but we can do so with a getter (getInt) and a setter (setInt) – which can also validate some prerequisites.

In case we want to do some things (such as setting a value to our members) when object is created, we implement our own constructor.

Now, we can create an object of our class by providing it the int value it expects.

Inheritance

Let’s talk about inheritance.
We want to reuse the code we have, and that means placing code into functions, or creating base classes we can inherit variables and functions.

Let’s assume we have a class called Human.

Now we added a new access modifier: protected.

If we have public access, everyone can use the functions and variables – if they have access to our object.
If we have protected access, we can access the functions and variables only from inside the functions from our class – and from derived classes (will get back to this).
If we have private access, we can access the functions and variables only from inside the functions from our class – the derived classes do not have access to this.

Now let’s create a class called Worker, which inherit from our base class Human.

Although the Worker class is empty, it inherits all the public and protected functions and variables from the base class.

For example, we can do the following:

The inheritance is performed as public, so it is called ‘is-a relationship’.
This means, all workers are also humans – and everyone knows that.

We can also use other access modifiers there, and most common are the public and private one.
the private inheritance means that only that class know it’s the type of the base class, but noone else knows that.
This is called a ‘has-a relationship’.

Everything that’s inherited from Engine (public, or protected) will be private in our class.
So it would look like this :

We see that we cannot call the start function inherited from engine, because it is private.
When we inherited from it publicly, the access would’ve been public, so it would have been possible to call it from outside.

We can use composition to achieve the same result:

Back to the inheritance:
Assuming we have the base and the derived classes:

If we create an object of type Derived, the following lines will be displayed:

When the object is destroyed, the following lines will be displayed:

So, when an object is constructed, is starts from the base to the end (imagine a building being constructed) and when it is destroyed it’s destroyed from the top to base (imagine a building being destroyed, starting from higher floors to the ground).

An overloaded function implies having a function with the same return type and same name, but with different parameter types.

Homework

Create a base class and a derived class, which would work as public inheritance. (eg. Human – Worker)
Do the same for a private inheritance. (eg. Car – Engine)

Add constructor and destructor for each of them.
In the base class, create variables and functions for all access modifier levels (public, protected, private).
Create an overload function in the derived class.

You may also like...