I. Meaning of inheritance
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.
Two. Inheritance relationships and Access qualifiers
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/7E/D9/wKiom1cKTsvxJcs9AAB4Ghkzajc847.png "title=" capture. PNG "alt=" Wkiom1cktsvxjcs9aab4ghkzajc847.png "/>
Here is a summary:
1. 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.
For example:
Class date{public: date () { cout << "Date ()" << endl; } ~date () { cout << "~date ()" << endl; } void Display () { cout << _ year << "-" << _month << "-" << _day < < endl; }public: int _year;protected: int _month;private: int _day;}; Class time : public date{public: time () { cout << "Date ()" << endl; } ~time () { cout << "~date ()" << endl; } void display () { cout << _year << "-"; cout << _month << "-"; //cout<< _day << "-"; // The private member of the base class cannot be accessed in a derived class. cout << _hour << "-" < < _minute << "-" << _second << endl; }public: int _hour;protected: int _minute; Private int _second;};
2.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 child class, because each subclass object is also a parent class object.
void Test1 () {time t; Date T1; t = t1; The parent class object cannot assign a value to a subclass object T1 = t; A subclass object cannot assign a value to a parent class object time* p1 = NULL; date* P2 = NULL; P2 = &t; A pointer/reference to a parent class can point to a subclass object//P1 = &t1; A pointer/reference to a subclass cannot point to the parent class object (which can be done by forcing the type conversion) T._hour = 0;}
3.protetced/private inheritance is an implementation inheritance, some members of the base class are not completely part of the subclass interface, and are the has-a relationship principle.
4. Regardless of the inheritance method, 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).
Class date{public: date () { cout << "Date ()" << endl; } ~date () { cout << "~date ()" << endl; } void Display () { cout << _ year << "-" << _month << "-" << _day < < endl; }public: int _year;protected: int _month;private: int _day;}; Class time : protected date{public: time () { cout << "Date ()" << endl; } ~time () { cout << "~date ()" < < endl; } void display () { cout << _year << "-"; cout << _month << "-"; //cout<< _day << "-"; cout << _ hour << "-" << _minute << "-" << _second << endl; }public: int _hour;protected: int _minute;private: int _second;}; Void test2 () { time t; t._year = 0; t._month = 0; //cannot access the public and protected members of the parent class outside the world. t._hour = 0;}
5. 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.
6. Scopes in the inheritance system
(1). Both the base class and the derived class in the inheritance system have separate scopes.
(2). Subclasses and parent classes have members of the same name, and child class members will mask the parent class's direct access to members. (in subclass member functions, you can use the base class:: base class member access)--Hide
Class date{public: date () { cout << "Date ()" << endl; } ~date () { cout << "~date ()" << endl; } void Display () { cout << _ year << "-" << _month << "-" << _day < < endl; }public: int _year;protected: int _month;private: int _day;}; Class time : protected date{public: time () { cout << "Date ()" << endl; } ~time () { cout << "~date ()" < < endl; } void display () { date::D isplay (); //can access it in such a way cout << _hour << "-" < < _minute << "-" << _second << endl; }public: int _hour;protected: int _minute; private: int _second;}; Void test3 () { time t; t.display (); //is called externally, Called a function of the parent class}
Three. member functions for derived classes
In an inheritance relationship, if the six member functions are not displayed in a derived class, the compilation system will default to the six default member functions.
Class date{public: date () { cout << "Date ()" << endl; } ~date () { cout << "~date ()" << endl; } void Display () { cout << _ year << "-" << _month << "-" << _day < < endl; }public: int _year;protected: int _month;private: int _day;}; Class time : protected date{public: void display () { D ate::D isplay (); cout << _hour << "-" << _minute << "-" << _second << endl ; }public: int _hour;protected: int _minute;private: int _second;}; Void test3 () { time t;00182a9d lea ecx,[t] 00182AA0 call Time::Time (01813FCh) //called the constructor by assembling the code 00182aa5 mov dword ptr [ebp-4],0 t.display ();}
Four. Diamond inheritance and virtual inheritance
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/7E/D6/wKioL1cKXbnQrxfnAAAytiDh7Wg416.png "title=" Capture 1. PNG "alt=" Wkiol1ckxbnqrxfnaaaytidh7wg416.png "/>
From the classic diamond inheritance above, it can be seen that the class C contains two values of Class A, which creates a redundant waste of space and, when accessed, displays the members that specify which parent class to access.
Class a{public: a (int data = 0) :_a (data) {}//private: int _a;}; CLASS&NBSP;B1&NBSP;:&NBSP;PUBLIC&NBSP;A{PUBLIC:&NBSP;&NBSP;&NBSP;&NBSP;B1 (int data=0) &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;:_B1 (data) {}private: int _b1 ;}; CLASS&NBSP;B2&NBSP;:&NBSP;PUBLIC&NBSP;A{PUBLIC:&NBSP;&NBSP;&NBSP;&NBSP;B2 (int data = 0) &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;:_B2 (data) {}private: int _b2;}; class c :p ublic b1,public b2{public: c (int data = 0) :_c (data) {} Void fun () { //_a = 0; //instructions are ambiguous. B1::_a = 1; b2::_a = 2; }private: int _c;};
Virtual inheritance adds the keyword virtual to the inheritance method, which solves the problem of data redundancy & wasted space in the face-class object containing multiple parent objects in the diamond-like inheritance system.
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/7E/D9/wKiom1cKYe7Sbu2xAAA0j-fX3g0034.png "title=" Capture 2. PNG "alt=" Wkiom1ckye7sbu2xaaa0j-fx3g0034.png "/>
Basic understanding of the inheritance of C + + class