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.
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.
There are three inherited access levels in C:
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
You can access thepublic_function
of the base class through thecall_public_function
method.protected_function
of the base class through thecall_protected_function
method.private_function
because it is private.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!