codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
⚡

C++ Programming

49 / 87 topics
48Inheritance Basics49Public, Protected, and Private Inheritance50Types of Inheritance51Polymorphism52Function Overriding53Virtual Functions54Abstract Class and Pure Virtual Function
Tutorials/C++ Programming/Public, Protected, and Private Inheritance
⚡C++ Programming

Public, Protected, and Private Inheritance

Updated 2026-05-12
20 min read

Public, Protected, and Private Inheritance

Inheritance is a fundamental concept in object-oriented programming (OOP) that allows one class to inherit the properties and behaviors of another. Understanding how access specifiers affect inheritance is crucial for designing robust and maintainable C++ programs. This tutorial will explore public, protected, and private inheritance, their implications on member accessibility, and when to use each type.

Introduction

Inheritance in C++ can be modified using three access specifiers: public, protected, and private. These specifiers determine how the members of a base class are accessible in derived classes. Properly using these specifiers helps maintain encapsulation and control over class interfaces, which is essential for effective software design.

Core Content

Public Inheritance

Public inheritance is the most common type of inheritance. When a class inherits from another class publicly, all public and protected members of the base class become public and protected members of the derived class, respectively. Private members remain inaccessible to the derived class.

Example

C++
1#include <iostream>
2
3class Base {
4public:
5 int publicVar;
6protected:
7 int protectedVar;
8private:
9 int privateVar;
10};
11
12class Derived : public Base {
13public:
14 void display() {
15 std::cout << "Public: " << publicVar << std::endl; // Accessible
16 std::cout << "Protected: " << protectedVar << std::endl; // Accessible
17 // std::cout << "Private: " << privateVar << std::endl; // Error: inaccessible
18 }
19};
20
21int main() {
22 Derived d;
23 d.publicVar = 10;
24 // d.protectedVar = 20; // Error: protected in Base, not accessible here
25 // d.privateVar = 30; // Error: private in Base, not accessible here
26 d.display();
27 return 0;
28}
Output

Note

Protected inheritance is useful when you want to hide the base class interface from external users but still allow derived classes to access its members.

Private Inheritance

In private inheritance, all public and protected members of the base class become private members of the derived class. Private members remain inaccessible.

Example

C++
1#include <iostream>
2
3class Base {
4public:
5 int publicVar;
6protected:
7 int protectedVar;
8private:
9 int privateVar;
10};
11
12class Derived : private Base {
13public:
14 void display() {
15 std::cout << "Public: " << publicVar << std::endl; // Accessible, but now private
16 std::cout << "Protected: " << protectedVar << std::endl; // Accessible, still private
17 // std::cout << "Private: " << privateVar << std::endl; // Error: inaccessible
18 }
19};
20
21int main() {
22 Derived d;
23 // d.publicVar = 10; // Error: now private in Derived
24 // d.protectedVar = 20; // Error: private in Derived
25 // d.privateVar = 30; // Error: private in Base, not accessible here
26 d.display();
27 return 0;
28}
Output

In this example:

  • Account is a base class with public and protected members.
  • SavingsAccount inherits from Account publicly, allowing it to use the deposit method and access the balance.
  • CheckingAccount inherits from Account privately, making its methods and variables inaccessible outside of CheckingAccount.

Summary

Type of InheritanceAccessibility in Derived Class
PublicPublic members: public
Protected members: protected
Private members: not accessible
ProtectedPublic members: protected
Protected members: protected
Private members: not accessible
PrivatePublic members: private
Protected members: private
Private members: not accessible
  • Public Inheritance: Use when you want to extend the functionality of a base class while maintaining its interface.
  • Protected Inheritance: Use when you want to hide the base class interface from external users but still allow derived classes to access its members.
  • Private Inheritance: Use for implementing an "is-implemented-in-terms-of" relationship, where a class uses another class internally without exposing its interface.

What's Next?

Now that you understand public, protected, and private inheritance, the next step is to explore different types of inheritance such as single inheritance, multiple inheritance, and hierarchical inheritance. These concepts will further enhance your ability to design complex and flexible C++ programs.

Stay tuned for the next tutorial on "Types of Inheritance" where we'll dive deeper into these topics!


PreviousInheritance BasicsNext Types of Inheritance

Recommended Gear

Inheritance BasicsTypes of Inheritance