ClassDerived: The process of generating a new class from an existing class. ClassInheritanceA new class obtains the existing features from an existing class.
An existing class is called a parent class or a base class, and a new class is called a subclass or a derived class.
Definition Format:
// Single inheritance
New class Name: Inheritance Method Base class
{
......
}
// Multi-Inheritance
New class Name: Inheritance Method Base class 1, Inheritance Method Base class 2,..., inheritance method base class n
{
......
}
Class inheritance gives a hierarchical relationship between the class and the class. The parent class is not unique and the subclass is not unique. In short, there is no limit.
Inheritance Method:
- Public inheritance public: A common member (public) in the parent class to protect the access of the member (protected)Attribute in subclassUnchangedPrivate Members cannot be directly accessed.
- Private inheritance private: shared and protected members in the parent classPrivate memberPrivate Members cannot directly access the website.
- Protection inheritance protected: the common and protected members in the parent classProtect membersPrivate members are not allowed to directly access.
To sum up, a private member cannot be inherited. A public member changes the member identity in the inheritance mode to protect the Member from being a private member only in the private inheritance mode.
According to this feature, you need to know one thing:After private inheritanceAssume that ClassSon inherits ClassFather, then all the members of ClassFather become private members in ClassSon. If ClassSon2 of a class needs to inherit ClassSon, so we can only inherit from the public and protected members in ClassSon, And the things in ClassFather have nothing to do with ClassSon2.
This shows thatDifferences between protecting protected member protected and private member private: Both of them can only be accessed by internal functions of the class and cannot be accessed by object objects of the class. However, Members in protected can be inherited, but Members in private cannot be directly accessed by the quilt class.
Compatibility rules: In fact, like many of our software, they are compatible with the above. Child classes can be used to do things in place of the parent class, but the parent class cannot do things in place of the Child class. However, note that child classes can only be inherited from the parent class.
Constructor of the derived class:
(After reading this part, I thought it was a tragedy)
Like a common constructor, you must initialize the inherited members and all data members of the base class. Syntax format:
Derived class name: derived class name (parameter list): base class name 1 (parameter table 1 ),..., accumulation name n (parameter table n), embedded object name 1 (embedded parameter table 1 ),..., embedded object m (embedded parameter table m ){ The initialization statement of the new member in the derived class; } |
I think most people are dizzy. No matter whether you are dizzy or not, I am dizzy. so, let's take a short example and summarize the following points based on the example, you can understand this:
Class Son: public A, public B, public C {
Public:
Son (int a, int B, int c, int d): A (a), sonB (d), sonA (B), B (c ){}; // location (1)
Private:
A sonA; // location (2)
B sonB ;//
C sonC; // assume that the constructor in Class C has no parameters to pass.
}
// Reference:
Son s (1, 2, 3, 4 );
- Note that in position 1, I specially disrupted the position of a B sonA sonB, because this order is not important. Of course, it is recommended that the order of parameters be the same as that of parameters in the total parameter table.
- SonC is defined in private, but if the constructor of C does not have parameters to be passed, C and sonC can be omitted in the Son constructor.
The Son constructor initializes ABC in front of the function body, but if there are new members in the private, such as int, they should still be initialized in the function body.
Hide rules:
Let's say that if the parent class Father has a data member int Time and a common function member int getTime (), the Child class Son inherits the class Father, in addition, a data member int Time is defined in the class, and a function member int getTime () is also defined. In this case, the inherited Time and getTime () are hidden. When referencing, if only Time is written, it is actually the Time defined by the letter. Only Father: Time indicates the inherited Time.
Virtual base class:
It's a virtual parent class. I prefer this popular name.
Declared Syntax: class derived class name: virtual inheritance method base class name 1, inheritance method base class name 2
Here, virtual only applies to the base class name 1. The base class name 2 is not a virtual parent class.
The difference between the virtual parent class and the parent class is that if the common base class is a virtual base class, the data member with the same name inherited from different paths has only one copy in the memory, the same function name only has one ing. Draw a chart:
This should be clear.