C++ Inheritance Access Control
In this page:
class Derived : public Base { }; // public stays public
class Derived : protected Base { }; // public becomes protected
class Derived : private Base { }; // public becomes private
public Inheritance
With public inheritance, the base class's public members stay public and its protected members stay protected in the derived class, which is the most common and intuitive form of inheritance.
Example: public Inheritance
// Include std::cout and std::cin
#include <iostream>
// Define the type Base
class Base {
// Members below can be accessed from outside the class
public:
int a = 1;
protected:
int b = 2;
};
// Define the type Derived
class Derived : public Base {
// Members below can be accessed from outside the class
public:
void show() { std::cout << a << " " << b << std::endl; }
};
// Program execution starts in main()
int main() {
Derived d;
// Print to the console with cout
std::cout << d.a << std::endl;
d.show();
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
protected Inheritance
With protected inheritance, both the base class's public and protected members become protected in the derived class, hidden from code outside the class hierarchy but still usable by further subclasses.
Example: protected Inheritance
#include <iostream>
class Base {
public:
int a = 1;
};
class Derived : protected Base {
public:
void show() { std::cout << a << std::endl; }
};
int main() {
Derived d;
// d.a would fail to compile: no longer public
d.show();
return 0;
}
Login to try C/C++/Java/PHP code in the editor
private Inheritance
With private inheritance, both the base class's public and protected members become private in the derived class, meaning even further subclasses can no longer access them directly.
Example: private Inheritance
#include <iostream>
class Base {
public:
int a = 1;
};
class Derived : private Base {
public:
void show() { std::cout << a << std::endl; }
};
int main() {
Derived d;
// d.a would fail to compile: private inheritance hides it entirely
d.show();
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Access Specifier Summary Table
The three inheritance access specifiers each transform the base class's public and protected members differently in the derived class, while private base members are never accessible in the derived class no matter which specifier is used.
Example: Access Specifier Summary Table
// Include std::cout and std::cin
#include <iostream>
// Define the type Base
class Base {
// Members below can be accessed from outside the class
public:
int pub = 1;
protected:
int prot = 2;
// Members below are only accessible inside the class
private:
int priv = 3;
};
// Define the type PublicDerived
class PublicDerived : public Base {};
class ProtectedDerived : protected Base {};
class PrivateDerived : private Base {};
// Program execution starts in main()
int main() {
PublicDerived d;
// Print to the console with cout
std::cout << d.pub << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Choosing the Right Inheritance Access
public inheritance is used for a true is-a relationship where the derived class should expose the base's interface, while protected and especially private inheritance are used less often, mainly to model implementation reuse without exposing it publicly.
Example: Choosing the Right Inheritance Access
#include <iostream>
class Animal {
public:
void eat() { std::cout << "Eating" << std::endl; }
};
class Dog : public Animal {
// public inheritance: a Dog genuinely "is-a" Animal
};
int main() {
Dog d;
d.eat();
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- Expecting private members of a base class to be accessible in the derived class.
- Forgetting
publicwhen inheriting, so the default is private for classes. - Assuming protected members are accessible from outside.
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: