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

Course Content

C++ OOP

C++ OOP

1. Fundamentals of OOP in C++
2. Constructors and Destructors
3. Encapsulation Overview
4. Inheritance Overview
5. Polymorphism Overview

book
Access Modifiers in Inheritance

Access modifiers play a crucial role in object-oriented programming, especially in inheritance. They determine how the members (attributes and methods) of a base class can be accessed by derived classes. Understanding these modifiers is essential for designing effective classes and maintaining data encapsulation and integrity.

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

Public in Derived Class

Protected in Derived Class

Not Accessible

protected

Protected in Derived Class

Protected in Derived Class

Not Accessible

private

Private in Derived Class

Private in Derived Class

Not Accessible

public.cpp

public.cpp

protected.cpp

protected.cpp

private.cpp

private.cpp

copy
12345
class Derived : public Base { // publicMember is public // protectedMember is protected // privateMember is not accessible };

Access Control and Inheritance Conclusion

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

main.cpp

main.cpp

copy
1234567891011121314151617181920
class Base { public: int publicAttribute; protected: int protectedAttribute; private: int privateAttribute; }; class PublicDerived : public Base {}; class ProtectedDerived : protected Base {}; class PrivateDerived : private Base {}; int main() { PublicDerived obj1; ProtectedDerived obj2; PrivateDerived obj3; }
Note
Note

Protected members, accessible within derived and further derived classes, serve as a 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 mark

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

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 4. ChapterΒ 2

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

course content

Course Content

C++ OOP

C++ OOP

1. Fundamentals of OOP in C++
2. Constructors and Destructors
3. Encapsulation Overview
4. Inheritance Overview
5. Polymorphism Overview

book
Access Modifiers in Inheritance

Access modifiers play a crucial role in object-oriented programming, especially in inheritance. They determine how the members (attributes and methods) of a base class can be accessed by derived classes. Understanding these modifiers is essential for designing effective classes and maintaining data encapsulation and integrity.

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

Public in Derived Class

Protected in Derived Class

Not Accessible

protected

Protected in Derived Class

Protected in Derived Class

Not Accessible

private

Private in Derived Class

Private in Derived Class

Not Accessible

public.cpp

public.cpp

protected.cpp

protected.cpp

private.cpp

private.cpp

copy
12345
class Derived : public Base { // publicMember is public // protectedMember is protected // privateMember is not accessible };

Access Control and Inheritance Conclusion

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

main.cpp

main.cpp

copy
1234567891011121314151617181920
class Base { public: int publicAttribute; protected: int protectedAttribute; private: int privateAttribute; }; class PublicDerived : public Base {}; class ProtectedDerived : protected Base {}; class PrivateDerived : private Base {}; int main() { PublicDerived obj1; ProtectedDerived obj2; PrivateDerived obj3; }
Note
Note

Protected members, accessible within derived and further derived classes, serve as a 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 mark

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

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 4. ChapterΒ 2
some-alt