Home  >  Article  >  Backend Development  >  Detailed explanation of C++ function inheritance: What are inherited access rights?

Detailed explanation of C++ function inheritance: What are inherited access rights?

PHPz
PHPzOriginal
2024-05-03 08:42:021093browse

In C, the access rights of derived classes to base class functions depend on the inheritance level: public: Derived classes can access and override base class public functions without restrictions. Protected: Derived classes can only access and override protected functions of the base class, and cannot call them directly from objects of the base class. private: Derived classes cannot access private functions of the base class.

C++ 函数继承详解:什么是继承访问权限?

#C Detailed explanation of function inheritance: What is inherited access permissions?

In C, when a derived class inherits a base class, it can inherit members of the base class, including functions. Depending on the inheritance permissions, derived classes have different ways of accessing base class functions.

Inherited Access Levels

There are three inherited access levels in C:

  • public: Derived classes have unlimited access to and Override the public function of the base class.
  • protected: Derived classes can only access and override protected functions of the base class, and cannot call them directly from objects of the base class.
  • private: Derived classes cannot access private functions of the base class.

Practical case

Consider the following base class and derived class:

class Base {
public:
  void public_function();
protected:
  void protected_function();
private:
  void private_function();
};

class Derived : public Base {
public:
  // 派生类可以无限制地访问 public 函数
  void call_public_function() {
    public_function();
  }

protected:
  // 派生类只能访问 protected 函数
  void call_protected_function() {
    protected_function();
  }
};

As can be seen from this example:

  • Derived class Derived You can access the public_function of the base class through the call_public_function method.
  • Derived classes can also access the protected_function of the base class through the call_protected_function method.
  • Derived classes cannot directly access or override the base class's private_function because it is private.

Note

It is worth noting that although the protected functions of the base class cannot be called directly from the objects of the base class, they can be called from the protected or public functions of the derived class. The premise is that the derived class has access to these protected functions.

The above is the detailed content of Detailed explanation of C++ function inheritance: What are inherited access rights?. 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