C + + inheritance and derivation concepts, what is inheritance and derivation
Reusability in C + + is achieved through the mechanism of inheritance (inheritance). Therefore, inheritance is an important part of C + +.
The class is described earlier, and a class contains a number of data members and member functions. In different classes, data members and member functions are not the same. But sometimes the contents of two classes are essentially the same or partly the same, such as the class student that declares the student's basic data:
Class Student
{public
:
void display () ///definition of the member function display
{
cout<< "num:" <<num <<endl;
cout<< "Name:" << name <<endl;
cout << "Sex:" <<sex<<endl;
}
Private:
int num;
string name;
char sex;
};
If a department in the school needs to use the number, the name, the gender, but also needs to use the age, the address and so on information. Of course, you can declare another class Student1:
Class Student1
{public
:
void Display ()//This line originally has
{
cout<< ' num: ' <<num<<endl; This row originally had
cout<< "name:" << name <<endl; This trip originally had
cout << "Sex:" <<sex<<endl; This trip was originally
cout << Age: <<age<<endl;
cout << "Address:" <<addr<<endl;
}
private: int num;//This line originally had a string name;//The line originally had
char sex;//
This line originally had an int age;
Char addr[20];
You can see that quite a few are already there, you can use the original declared class student as the basis, plus new content to reduce the duplication of effort. The inheritance mechanism provided by C + + is to solve this problem.
In C + +, the so-called "inheritance" is to build a new class on the basis of an existing class. A class that already exists is called a base class or parent class (Father Class), and the new class is called a derived class (derived class) or subclass (son Class).
A new class obtains its existing attributes from an existing class, a phenomenon known as class inheritance. By inheriting, a new subclass obtains the attributes of the parent class from the existing parent class. From another perspective, a new subclass, called a derivation of a class, is generated from an existing class (the parent Class). The inheritance of a class is the programming technique of building a specialized class with an existing class. A derived class inherits all the data members and member functions of the base class and can make the necessary additions or adjustments to the members. A base class can derive multiple derived classes, and each derived class can then derive a new derived class as a base class, so the base class and the derived class are relative. From generation to generation, the inheritance hierarchy of classes is formed. The equivalent of a large family, with many branches, all descendants inherit the basic characteristics of their ancestors, while there are differences and development. Similarly, each derivation of a class inherits the basic characteristics of its base class, while adjusting and expanding its original characteristics as needed.
The simplest scenario described above is that a derived class derives only from a base class, which is called a single inheritance, and the hierarchy formed by this inheritance relationship is a tree structure, as shown in the figure.
A derived class can derive not only from a base class, but also from multiple base classes. In other words, a derived class can have one or more base classes. A derived class has two or more base classes called multiple inheritance (multiple inheritance). With respect to the relationship between the base class and the derived class, you can express that the derived class is the materialization of the base class, and the base class is the abstract of the derived class.
Access properties for C + + derived class members
Since the derived class contains members of the base class and derived classes themselves, the problem of the relationship between the two parts and the access properties arises. When creating a derived class, it is not simply a private member of the base class that directly acts as a private member of the derived class, and the public members of the base class are directly public members of the derived class.
In fact, the addition of members to base class members and derived classes is handled according to different principles. Specifically, when discussing access properties, consider the following:
The member function of the base class accesses the base class member.
A member function of a derived class accesses its own added member.
A member function of a base class accesses a member of a derived class.
A member function of a derived class accesses a member of the base class.
Accesses a member of a derived class outside of a derived class.
Access the members of the base class outside the derived class.
In the case of paragraphs (1) and (2), it is simpler that the member functions of the base class can access the base class members, and the member functions of the derived class can access the derived class members. Private data members can only be accessed by member functions in the same class, and public members are accessible to the outside world.
The first (3) case is also clear, and the member function of the base class can only access members of the base class, not the members of the derived class.
The first (5) case is also clear, where the public members of the derived class can be accessed outside of the derived class, and the private members of the derived class cannot be accessed.
For the cases of paragraphs (4) and (6), it is slightly more complex and easily confusing. For example, the question was raised that:
A member function in a base class can access any member of a base class, the newly added member in the derived class can equally access private members in the base class;
Outside a derived class, you can access a public member inherited from a base class by using the object name of the derived class.
These involve questions about how to determine the access properties of a member of a base class in a derived class. Consider not only the access attributes that are declared to the base class members, but also the inheritance of the base class declared by the derived class, which together determine the access properties of the base class member in the derived class, based on these two factors.
As mentioned earlier, in a derived class, the inheritance of a base class can have 3 kinds of public (common), private (private), and protected (protected). The different inheritance methods determine the access properties of the base class member in the derived class. In short, this can be summed up as the following points.
1) Common inheritance (public inheritance)
The public and protected members of the base class retain the original Access property in the derived class, and the private members are still private to the base class.
2 Private Inheritance (privately inheritance)
The public members and protection members of the base class become private members in the derived class, and the private members are still private to the base class.
3 protected inheritance (Protected inheritance)
The public and protected members of the base class are protected members in the derived class, and their private members are still private to the base class. Protecting members means that they cannot be referenced by outsiders, but can be referenced by members of derived classes.