paint-brush
Protected and Private Inheritanceby@drodil
987 reads
987 reads

Protected and Private Inheritance

by Heikki HellgrenSeptember 28th, 2018
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

<em>This is fifth part of my C++ Telltales series where I share some tips and tricks to work with C++. Feel free to check out also other parts of the series </em><a href="https://medium.com/@drodil/c-telltales-series-3a8c74a31629" target="_blank"><em>here</em></a><em>!</em>

People Mentioned

Mention Thumbnail

Company Mentioned

Mention Thumbnail
featured image - Protected and Private Inheritance
Heikki Hellgren HackerNoon profile picture

C++ Telltales part 5:

This is fifth part of my C++ Telltales series where I share some tips and tricks to work with C++. Feel free to check out also other parts of the series here!

If you are working with C++ you must be familiar with class inheritance (or if not, you really should put it on top of your bucket list). It’s very often you use public inheritance without even thinking about it but there is actually two other ways to inherit the parent class: protected and private. But what do they actually mean? Well, let’s try to let the code talk for us:

As explained in the comments of the main function, the public functions of the BaseClass will have the same visibility as the inheritance visibility is. This also applies to the member variables of the classes.

So where should this be used then? Well, if you want to hide some public functions from the caller but still like to have them available for the inheriting classes. Especially with protected inheritance you can create quite interesting designs. Here is a table showing the different inheritance options and how they affect the inheriting class:

And here is the same image from my first part to get some cover photo for this article (still lazy):

If you liked the story, please press the ❤ button below (did you know that you can give more than one clap). Also please feel free to share this story!

About me

I am Heikki Hellgren, Software Expert and technology enthusiast working at Elektrobit Automotive. My interests are in software construction, tools, automatic testing and all the new and cool stuff like AI and autonomous driving. You can follow me on Medium and Twitter and check out my websitefor more information.