C ++ learning: simple inheritance first knowledge, learning inheritance first Knowledge
Simple inheritance
Is there any relationship between classes? Of course there are some, combinations and inheritance. A combination is a member variable of a class that contains other class objects. What we are talking about today is inheritance.
What is inheritance? Literally, it means getting all. Object-oriented Inheritance refers to the parent-child relationship between classes. The parent class is also called the base class, And the subclass is also called the derived class. Subclass has all attributes and actions of the parent class, so the subclass object can be used as a parent class object, therefore, when creating a subclass, in addition to calling its own constructor, it also calls the constructor of the parent class. The sequence is the first parent class and the last child class. Therefore, it is a special parent class, you can also add attributes and actions that are not in the parent class to the subclass. attributes are member variables and actions are member functions. So how should we use it?
class Parent{public: Parent() { cout << "Parent()" << endl; }};class Child : public Parent{public: Child() { cout << "Child()" << endl; }};int main(){ Child c; Parent p; p = c; return 0;}
The output result is:
Parent ()
Child ()
Parent ()
Through the program, we can know that the inheritance relationship is formed through the ':' operator, and the inheritance level is public inheritance. As we said, the parent class constructor is called before the subclass constructor is called, the subclass object can initialize the parent class object, and the subclass object can be assigned to the parent class object.
Why is there inheritance? Inheritance is an important means of code reuse in C ++. by inheritance, all functions of the parent class can be obtained, and existing functions can be rewritten in the subclass or new functions can be added.
Access Level
As mentioned above, subclasses can be used as parent classes. Can subclasses access private member variables in parent classes?
According to the object-oriented theory, all attributes and behaviors of the parent class can be obtained for the subclass. Therefore, the subclass can directly access the Private Members of the parent class.
According to the C ++ syntax, it can be concluded that the private members of the parent class cannot be directly accessed outside the class, so the subclass cannot directly access the Private Members of the parent class.
So what is the real answer?
Class Parent {private: int mv; public: Parent () {mv = 100 ;}}; class Child: public Parent {public: int addValue (int v) {mv = mv + v; // how do I access non-public members of the parent class ?? }};When a subclass tries to modify the private member variable of the parent class, an error is returned when the result is compiled, indicating that the mv member variable in the parent class is private and cannot be accessed by the outside world. So how do we access non-public members in the parent class?
Previously, we only learned two access levels: public and private. In fact, there is a third access level in C ++: protected. What does it mean, that is, it cannot be accessed by the outside world, but it can be accessed by the quilt class, between public and private.
When the current parent class member is set to protected, the compilation will pass normally, indicating that the Child class can access the protected members in the parent class, but the outside is not accessible.
Why do we need a protected access level?
First, the protected member cannot be accessed by the outside world, so that the subclass member can access the members of the parent class. Therefore, it is specially designed for the inheritance relationship, without protected, you cannot reuse the code in the real sense.