What is the difference between public, private, and protected inheritance in C++?

Matheus Mello
Matheus Mello
September 2, 2023
Cover Image for What is the difference between public, private, and protected inheritance in C++?

Understanding Public, Private, and Protected Inheritance in C++ 🧑‍💻

So, you're delving into the fascinating world of C++ inheritance and find yourself pondering the perplexing question: what in the world is the difference between public, private, and protected inheritance? 🤔

Fear not, fellow programmer! In this blog post, we'll break down the definitions, differences, and use cases of each type of inheritance, making it crystal clear for you. Let's dive in! 💪

Public Inheritance 🌟

When you derive a class from a base class using public inheritance, all the public members of the base class become public members of the derived class. This means that the derived class can access these public members. Let's take an example to make things clearer:

class Base {
public:
    void publicMethod() {
        // Some code here
    }
};

class Derived : public Base {
    // Can access public members of Base
};

In this case, the publicMethod() defined in the Base class is accessible in the Derived class. Public inheritance allows the derived class to retain the interface of the base class.

Private Inheritance 🔒

Private inheritance, on the other hand, keeps all the public and protected members of the base class private in the derived class. This means that these members are not directly accessible through the derived class. Let's see an example:

class Base {
public:
    void publicMethod() {
        // Some code here
    }
};

class Derived : private Base {
    // Cannot directly access public members of Base
};

In this case, the publicMethod() defined in the Base class is not directly accessible in the Derived class. However, the Derived class can still use the public members of the Base class indirectly, for example, through member functions or friend functions.

Protected Inheritance 🛡️

Protected inheritance is similar to private inheritance. It also keeps all the public and protected members of the base class private in the derived class. However, there is a difference when it comes to explicit casting and friend relationships. Let's check an example:

class Base {
public:
    void publicMethod() {
        // Some code here
    }
};

class Derived : protected Base {
    // Cannot directly access public members of Base
};

In this case, the publicMethod() defined in the Base class is not directly accessible in the Derived class. However, the Derived class can still use the public members of the Base class indirectly, similar to private inheritance.

Case Closed! 🔍

To summarize, the main differences between public, private, and protected inheritance in C++ are:

  • Public inheritance: The derived class can access the public members of the base class directly.

  • Private inheritance: The derived class cannot access the public members of the base class directly, but can still use them indirectly.

  • Protected inheritance: Similar to private inheritance, the derived class cannot access the public members of the base class directly, but can still use them indirectly.

Now that you've unraveled the mysteries of inheritance types, go forth and apply this knowledge to create clean and well-structured code! 😄

Have Questions or Feedback? 📝

If you have any questions or want to share your thoughts, feel free to leave a comment below. Let's keep the discussion going! And don't forget to share this post with your fellow programmers who might find it useful. Happy coding! 🚀

Take Your Tech Career to the Next Level

Our application tracking tool helps you manage your job search effectively. Stay organized, track your progress, and land your dream tech job faster.

Your Product
Product promotion

Share this article

More Articles You Might Like

Latest Articles

Cover Image for How can I echo a newline in a batch file?
batch-filenewlinewindows

How can I echo a newline in a batch file?

Published on March 20, 2060

🔥 💻 🆒 Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Cover Image for How do I run Redis on Windows?
rediswindows

How do I run Redis on Windows?

Published on March 19, 2060

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! 🚀 Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Cover Image for Best way to strip punctuation from a string
punctuationpythonstring

Best way to strip punctuation from a string

Published on November 1, 2057

# The Art of Stripping Punctuation: Simplifying Your Strings 💥✂️ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Cover Image for Purge or recreate a Ruby on Rails database
rakeruby-on-railsruby-on-rails-3

Purge or recreate a Ruby on Rails database

Published on November 27, 2032

# Purge or Recreate a Ruby on Rails Database: A Simple Guide 🚀 So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? 🤔 Well, my