inheritance is the most important feature of object-oriented programming, so to speak, if you do not know the inheritance, it is equal to No
Master the essence of classes and objects, so a good grasp of inheritance is the basis for us to learn C + +, the following we introduce it.
Inheritance is an important means of object-oriented reuse . By inheriting the definition of a class, the relationships between their types are modeled , sharing common things, and implementing something different in their nature.
There are three ways of inheriting: public inheritance, protected inheritance, private inheritance.
Summary :
A private member of a base class cannot be accessed in a derived class, and if some base class members do not want to be accessed directly by the base class object but need to be accessible in the derived class, they are defined as protected members. You can see that the Protection member qualifier occurs because of inheritance .
Public inheritance is an interface inheritance that maintains the is-a principle, and the members that are available to each parent class are also available to the subclass, because each child class object is also a parent class object.
Protetced/private inheritance is an implementation of inheritance, some members of the base class are not completely part of the subclass interface, is the has-a relationship principle, so the two inheritance relationships are not used under special circumstances, and in most scenarios, public inheritance is used.
Either way, the public and protected members of the base class can be accessed inside the derived class, but the private members of the base class exist but are not visible in the subclass (inaccessible).
The default inheritance method when using the keyword class is private, and the default inheritance method when using a struct is public, but it is best to show the inheritance in the way it is written.
In the actual use of the general is the public inheritance, very few scenarios will use Protetced/private inheritance.
class Person
{
Public :
void Display ()
{
cout << "_name" << Endl;
}
protected :
string _name; //Name
Private :
int _age; //Age
};
class Student : Public Person //You can modify the Inheritance property here
{
protected :
int _num; //Study No.
};
void Test ()
{
person < Span style= "FONT-SIZE:14PT;" >p;
Student s; //created an object of a derived class (subclass) type
//1. Subclass objects can be assigned to parent class objects (cut/slice)
p = s;
//2. The parent class object cannot be assigned to a child class object
s = p;
//3. A pointer/reference to a parent class can point to a child class object
person* p1 = &s;
person& r1 = s;
//4. A pointer/reference to a subclass cannot point to the parent class object (can be done by forcing the type conversion)
student * p2 = ( student *) & P;
Student& r2 = (Student &) p;
//What will happen here?
P2->_num = 10;
R2._num = 20;
}
After debugging, the following conclusions are obtained:
Subclass objects can be assigned to parent class objects (cut/slice)
Parent class object cannot be assigned to a subclass object
A pointer/reference to a parent class can point to a child class object
A pointer/reference to a subclass cannot point to the parent class object (can be done by forcing the type conversion)
In the inheritance system, both the base class and the derived class have separate scopes.
Subclasses and parent classes have members of the same name, and child class members block direct access to members of the parent class. (in subclass member functions, you can use the base class:: base class member access)--Hide
Note that in practice, it is best not to define a member with the same name in the inheritance system .
Single inheritance--a subclass that has only one direct parent class that is called the inheritance relationship as a single inheritance
Multiple inheritance--when a subclass has two or more direct parent classes, this inheritance relationship is called multiple inheritance
Virtual inheritance solves the problem of the data redundancy & waste space of the face class object containing multiple parent objects in the diamond-like inheritance system.
Virtual inheritance systems look complicated, and in practice we don't usually define such a complex inheritance system. It is generally not the last resort to define the virtual inheritance architecture of a diamond structure, because using virtual inheritance to solve data redundancy problems also leads to performance loss.
This article is from the "10911544" blog, please be sure to keep this source http://10921544.blog.51cto.com/10911544/1762307
Features of C + +--Inheritance