Home >Backend Development >C++ >How Do Access Specifiers Affect Member Accessibility in C Inheritance?

How Do Access Specifiers Affect Member Accessibility in C Inheritance?

Barbara Streisand
Barbara StreisandOriginal
2024-12-26 03:27:08443browse

How Do Access Specifiers Affect Member Accessibility in C   Inheritance?

Understanding Access Specifiers in Inheritance

In object-oriented programming, access specifiers are keywords that control the accessibility of class members. When inheriting from a base class, the accessibility of its members in the derived class depends on the access specifiers used during inheritance.

Types of Inheritance

C supports three types of inheritance:

  • Public inheritance: Public members of the base class become public members of the derived class. Protected members become protected in the derived class.
  • Protected inheritance: Public members of the base class become protected members of the derived class. Protected members become protected in the derived class.
  • Private inheritance: Public and protected members of the base class become private members of the derived class.

Member Access Rules

Here's how the access level of members changes during inheritance based on the access specifier used:

Public and Protected Inheritance:

  • Public members of the base class remain public or protected in the derived class.
  • Protected members of the base class remain protected in the derived class.

Private Inheritance:

  • Public and protected members of the base class lose their access specifiers and become private in the derived class.

Example:

class Base {
public:
    int public_member;
protected:
    int protected_member;
    int private_member; // private members are not inherited
};

class Derived1 : public Base {
public:
    void access_member() {
        // Access rules:
        public_member = 10; // allowed
        protected_member = 20; // allowed
        // private_member = 30; // not accessible
    }
};

class Derived2 : protected Base {
public:
    void access_member() {
        // Access rules:
        public_member = 10; // allowed
        protected_member = 20; // allowed
        // private_member = 30; // not accessible
    }
};

class Derived3 : private Base {
public:
    void access_member() {
        // Access rules:
        public_member = 10; // not accessible
        protected_member = 20; // not accessible
        // private_member = 30; // not accessible
    }
};

Best Practices for Inheritance

When choosing an access specifier for inheritance, consider the following guidelines:

  • Default to private unless otherwise needed: Private protects the internal implementation of the class.
  • Use protected to expose functionality to derived classes.
  • Use public sparingly as it allows access to members from outside the class hierarchy.
  • Avoid public inheritance of private or protected members: This breaks encapsulation and can lead to unexpected behavior.

The above is the detailed content of How Do Access Specifiers Affect Member Accessibility in C Inheritance?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn