Inheritance
★ Inheritance Concept
the Inheritance (inheritance) mechanism is Object-oriented programming is the most important means of reusing code, which allows programmers to extend and add functionality based on preserving the original class features. . This produces a new class, called a derived class. Inheritance presents the hierarchical structure of object-oriented programming, which embodies the cognitive process from simple to complex.
Inheritance definition Format
★ Inheritance Relations & Access Qualifiers
Class base{public : Base () { cout<< "B ()" <<endl; } ~base () { cout<< "~b ()" <<endl; } void Showbase () { cout<< "_pri =" <<_pri< < Endl; cout<< "_pro =" <<_pro<< Endl; cout<< "_pub =" <<_pub<< Endl;} Private: int _pri; Protected: int _pro; Public: int _pub;}; Class Derived:public Base {public : Derived () { cout<< "D ()" <<endl; } ~derived () { cout<< "~d ()" <<endl; } void showderived () { cout<< "_d_pri = "<<_d_pri<< Endl; cout<< "_d_pro =" <<_d_pro<< Endl; cout<< "_d_pub =" <<_d_pub<< Endl; } Private: int _d_pri; Protected: int _d_pro; Public: int _d_pub;};
Summarize:
of the base class Private a member cannot be accessed in a derived class, and if the base class member does not want to be accessed directly outside of the class, but needs to be accessible in the derived class, it is defined as protected . You can see that the protection member qualifier occurs because of inheritance .
Public inheritance is an interface inheritance that keeps is-a principle, the members that are available to each parent class are also available to the child class, because each subclass object is also a parent class object.
Protetced/private Inheritance is an implementation inheritance, Some members of a base class are not completely part of a subclass interface. is a has-a relationship principle, the two inheritance relationships are not used under special circumstances and are used in most scenarios as public inheritance.
Regardless of the inheritance method, the public and protected members of the base class can be accessed inside the derived class, and 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.
default member functions for derived classes
In an inheritance relationship, if the six member functions are not displayed in a derived class, the compilation system defaults synthesize these six default member functions .
"Sequence of constructor calls in an inheritance relationship"
Description
1. The base class does not have a default constructor, and the derived class must explicitly give the base class name and argument list in the initialization list.
2. If the base class does not have a constructor defined, the derived class can also use the default constructor without defining it.
3. The base class defines the constructor with the formal parameter list, and the derived class must define the constructor function.
"Destructor call procedure in inheritance relationship"
Class Test1{public:test1 (int data) {cout << "Test1 ()" <<ENDL;} ~test1 () {cout<< "~test1 ()" <<ENDL;}; Class Test2{public:test2 (int data) {cout << "Test2 ()" <<ENDL;} ~test2 () {cout<< "~test2 ()" <<ENDL;}; Class BASE1{PUBLIC:BASE1 (int data): _data (data) {cout << "Base1 ()" <<ENDL;} ~base1 () {cout<< "~base1 ()" <<ENDL;} Protected:int _data;}; Class BASE2{PUBLIC:BASE2 (int data): _DATA2 (data) {cout << "Base2 ()" <<ENDL;} ~base2 () {cout<< "~base2 ()" <<ENDL;} Protected:int _data2;}; Class Derive:public Base1, public base2{public://derive (): Base1 (0), Base2 (1), T1 (3), T2 (4)//derive (): Base2 (0), B ASE1 (1), T2 (3), T1 (4)//derive (): T1 (3), T2 (4), Base1 (0), Base2 (1) Derive (): T2 (3), T1 (4), Base2 (0), BASE1 (1) {cout << "Derive ()" <<ENDL;} ~derive () {cout<< "~derive ()" <<ENDL;} Protected:test1 T1; Test2 T2;};
. Scopes in the inheritance system
In the inheritance system, base classes and derived classes are two different 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--redefine
Note that in practice, it is best not to define members with the same name in the inheritance system .
Class Person{public: Person (const char * name = "", int id = 0) : _name (name), _num (id) {}protected: s Tring _name; name int _num; ID number};</span>
<span style= "font-family:fangsong_gb2312;" >class student:public person{public: Student (const char * name, int id, int stunum) : Person (name, id), _num (Stunum) {} void Displaynum () { cout<< "ID Number:" <<person:: _num<< Endl; c14/>cout<< "study number" << _num << Endl; } Protected: int _num; Study number};
★ Inheritance and Conversion-- assignment compatibility rule--public inheritance
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)
★ Friends and Inheritance
A friend relationship cannot inherit, which means that the base class friend cannot access the subclass private and protected members.
Class person{ friend void Display (person &p, Student&s);p rotected: string _name; Name};</span>
<span style= "font-family:fangsong_gb2312;" >class student:public person{protected: int _stunum; Study number};void Display (person &p, Student &s) { cout<<p._name<<endl; cout<<s._name<<endl; Cout<<s._stunum<<endl;} void TestPerson1 () {person p; Student s; Display (P, s);}
★ Inherited and Static members
The base class defines a static member, and there is only one such member in the entire inheritance system. No matter how many subclasses are derived, there is only one static member instance.
Class Person{public: Person () {+ + _count;} Protected: string _name; Name public: static int _count; Count the number of people. };int person::_count = 0;class student:public person{protected: int _stunum; School Number};</span>
<span style= "font-family:fangsong_gb2312;" >class graduate:p ublic student{protected: string _seminarcourse; Research Subjects};void TestPerson1 () { Student s1; Student S2; Student S3; Graduate S4; cout<< "Number of people:" <<Person::_count<<endl; Student:: _count = 0; cout<< "Number of people:" <<PERSON::_COUNT<<ENDL;}
★ Single Inheritance & Multiple Inheritance & Diamond inheritance
"Single Inheritance"
when a subclass has only one direct parent, this inheritance is referred to as a single inheritance .
"Multiple Inheritance"
When a subclass has two or more direct parent classes, this inheritance relationship is called multiple inheritance
"Diamond Inheritance"
Class Person{public: string _name; Name};class student:public person{protected: int _num; Study number};class teacher:public person{protected: int _id; Employee number};class Assistant:public Student, public teacher{protected: string _majorcourse; Major course};void Test () { //shows the member Assistant A that specifies which parent class to access ; A.student:: _name = "xxx"; A.teacher:: _name = "yyy";}
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 "10909090" blog, please be sure to keep this source http://10919090.blog.51cto.com/10909090/1763357
(Inheritance and its access qualifier) && (derived class and its default member function) && (Assignment compatibility rule)