Home > Backend Development > C++ > Inheritance rules for C++ member functions

Inheritance rules for C++ member functions

WBOY
Release: 2024-04-18 14:45:01
Original
1222 people have browsed it

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.

C++ 成员函数的继承规则

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:

  • 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: The 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, then the member functions of the derived class are private and cannot be directly accessed by the derived class itself.

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;
};
Copy after login

In this example:

  • # The ##Rectangle class publicly inherits the Shape class, so the getArea function is also public in the Rectangle class. The
  • Square class protection inherits the Shape class, so the getArea function is also protected in the Square class. The
  • Circle class privately inherits from the Shape class, so the getArea function is private in the Circle class.

Note:

    Abstract functions must be overridden in derived classes.
  • Member functions of a derived class can access the protected and private data members of the base class, but can only call the public and protected member functions of the base class.
  • The constructor and destructor of a derived class will not be inherited from the base class.

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template