Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Access Modifiers in Inheritance | Inheritance Overview
C++ OOP

Access Modifiers in InheritanceAccess Modifiers in Inheritance

Access modifiers in inheritance are crucial in C++ object-oriented programming. They dictate how members (attributes and methods) of a base class can be accessed by a derived class. Understanding these modifiers is essential for effective class design and for maintaining encapsulation and integrity of the data.

Access Types of Inheritance

A class can be derived from another class. The derived class inherits members from the base class, but the accessibility of these inherited members depends on both the access modifier used in the base class and the type of inheritance.

Base Class publicBase Class protectedBase Class private
public Inheritance
Public in Derived Class
Protected in Derived Class
Not Accessible
protected Inheritance
Protected in Derived Class
Protected in Derived Class
Not Accessible
private Inheritance
Private in Derived Class
Private in Derived Class
Not Accessible

This table provides a clear overview of how the access level of members from a base class changes in the derived class, depending on the type of inheritance used.

cpp

public.cpp

cpp

protected.cpp

cpp

private.cpp

Access Control and Inheritance Conclusion

In object-oriented inheritance, base class private members are inaccessible to derived classes, safeguarding them from modification or retrieval and protected members can only be accessed within the subclass, while public members can be accessed externally. You can experiment with it using code snippet below.

cpp

main.cpp

Remember

Protected members, accessible within derived and subsequent derived classes, they serve as bridge between private and public elements.

Constructors and destructors are automatically invoked for derived class objects, ensuring proper resource initialization and cleanup. To access these base class elements directly, constructors and destructors must be declared public.


question-icon

What determines how members of a base class can be accessed by a derived class?

Select a few correct answers

Everything was clear?

Section 4. Chapter 2

Access Modifiers in InheritanceAccess Modifiers in Inheritance

Access modifiers in inheritance are crucial in C++ object-oriented programming. They dictate how members (attributes and methods) of a base class can be accessed by a derived class. Understanding these modifiers is essential for effective class design and for maintaining encapsulation and integrity of the data.

Access Types of Inheritance

A class can be derived from another class. The derived class inherits members from the base class, but the accessibility of these inherited members depends on both the access modifier used in the base class and the type of inheritance.

Base Class publicBase Class protectedBase Class private
public Inheritance
Public in Derived Class
Protected in Derived Class
Not Accessible
protected Inheritance
Protected in Derived Class
Protected in Derived Class
Not Accessible
private Inheritance
Private in Derived Class
Private in Derived Class
Not Accessible

This table provides a clear overview of how the access level of members from a base class changes in the derived class, depending on the type of inheritance used.

cpp

public.cpp

cpp

protected.cpp

cpp

private.cpp

Access Control and Inheritance Conclusion

In object-oriented inheritance, base class private members are inaccessible to derived classes, safeguarding them from modification or retrieval and protected members can only be accessed within the subclass, while public members can be accessed externally. You can experiment with it using code snippet below.

cpp

main.cpp

Remember

Protected members, accessible within derived and subsequent derived classes, they serve as bridge between private and public elements.

Constructors and destructors are automatically invoked for derived class objects, ensuring proper resource initialization and cleanup. To access these base class elements directly, constructors and destructors must be declared public.


question-icon

What determines how members of a base class can be accessed by a derived class?

Select a few correct answers

Everything was clear?

Section 4. Chapter 2
some-alt