C Member function inheritance rules: Public inheritance: If a derived class publicly inherits the member functions of the base class, then the member functions of the derived class are also public. Protected inheritance: If a derived class protects and inherits the member functions of the base class, then the member functions of the derived class are protected. Private inheritance: The derived class privately inherits the member functions of the base class. The member functions of the derived class are private and cannot be directly accessed by the derived class itself.
Inheritance rules for C member functions
In C object-oriented programming, a class can inherit from a base class through inheritance Data members and member functions. For the inheritance of member functions, follow the following rules:
Practical Example:
Consider the following example:
class Shape { public: virtual double getArea(); // 抽象函数 }; class Rectangle : public Shape { public: Rectangle(double length, double width); double getArea() override; // 重写父类的 getArea 函数 private: double length; double width; }; class Square : protected Shape { public: Square(double side); double getArea() override; private: double side; }; class Circle : private Shape { public: Circle(double radius); double getArea() override; private: double radius; };
In this example:
class publicly inherits the
Shape class, so the
getArea function is also public in the
Rectangle class. The
class protection inherits the
Shape class, so the
getArea function is also protected in the
Square class. The
class privately inherits from the
Shape class, so the
getArea function is private in the
Circle class.
Note:
The above is the detailed content of Inheritance rules for C++ member functions. For more information, please follow other related articles on the PHP Chinese website!