I. Purpose of introducing inheritance
1. code reuse
The Inheritance and derivation mechanisms of classes enable programmers to get new classes by adding a small amount of code or modifying a small amount of code on the basis of existing classes without having to modify existing classes, this effectively solves the issue of code reuse.
2. Code Expansion
Only when a new member is added to a derived class and a new function is added can the derivation of the class be meaningful.
Ii. Declaration format of a derived class (single inheritance)
Class derived class name: inheritance method base class name {// data member and member function added to the derived class };
1) The base class name is the name of a defined class, which can also be called the parent class;
2) The name of the derived class is the name of the new class generated by inheriting the features of the original class;
3) The Inheritance Method specifies how to access the members inherited from the base class, and specifies the access permissions of the derived class members and objects for the Members inherited from the base class, it includes the keywords private, protected, and public (indicating private, protected, and public inheritance respectively ).
4) if the inheritance mode keyword is not displayed, the system defaults to private ).
3. Access attributes of base class members in the derived class
1. view the base class member attributes
1) when the access attribute of the base class member in the base class is private,
Access attributes in the derived classes of the three inheritance methods cannot be directly accessed;
2) When the access attribute of the base class member in the base class is public,
The Inheritance Method is public, and the access attribute in the derived class is public,
The Inheritance Method is private, and the access attribute in the derived class is private,
The Inheritance Method is protected, and the access attribute in the derived class is protected;
3) when the access attribute of the base class member in the base class is protected,
The Inheritance Method is public, and the access attribute in the derived class is protected,
The Inheritance Method is private, and the access attribute in the derived class is private,
The Inheritance Method is protected, and the access attribute in the derived class is protected.
Base class members access attributes in the base class |
Base class members access attributes in the derived class |
Public |
Private |
Protected |
Public |
Public |
Private |
Protected |
Private |
Not directly accessible |
Not directly accessible |
Not directly accessible |
Protected |
Protected |
Private |
Protected |
2. From the Inheritance Method perspective
1) when the inheritance method is private,
If the base class member attributes are public and protected, the access attribute in the derived class is private,
If the base class member attribute is private, the access attribute in the derived class is not directly accessible;
2) When the inheritance method is public,
If the base class member attributes are public and protected, the access attributes in the derived class remain unchanged,
If the base class member attribute is private, the access attribute in the derived class is not directly accessible;
3) when the inheritance method is protected,
If the base class member attributes are public and protected, the access attribute in the derived class is protected,
If the base class member attribute is private, the access attribute in the derived class is not directly accessible.
Inheritance of a derived class |
Base class members access attributes in the base class |
Public |
Private |
Protected |
Public |
Public |
Not directly accessible |
Protected |
Private |
Private |
Not directly accessible |
Private |
Protected |
Protected |
Not directly accessible |
Protected |
Iv. constructor and destructor of a derived class (single inheritance)
1. Description:
1) constructors and destructor of the base class cannot be inherited;
2) In a derived class, if you initialize new members in the derived class, you need to add the constructor of the derived class;
3) initialization of all members inherited from the base class is completed by the base class constructor;
4) when the base class contains constructors with parameters, the derived class must define constructors to set the parameters required by the base class constructors;
5) when the base class constructor has no parameters or does not explicitly define the constructor (that is, the default constructor is used), the derived class can not pass parameters to the base class, you may not even define constructors;
6) if the base class of the derived class is also a derived class, each derived class only needs to be responsible for its direct construction of the base class, which is traced back once;
7) The derived class is independent from the destructor of the base class (because the Destructor does not contain parameters, the destructor of the base class will not be executed because the derived class does not have the destructor ).
2. execution sequence of constructor and destructor:
1) when creating a derived class object, first execute the constructor of the base class, and then execute the constructor of the derived class;
2) When revoking a derived class object, first execute the destructor of the derived class, and then execute the destructor of the base class.
Example:
# Include <iostream. h> class kbase {public: kbase () // constructor of the base class {cout <"constructing base class/N ";}~ Kbase () // destructor of the base class {cout <"destructing base class/N" ;}}; class kderive1: Public kbase {public: kderive1 () // constructor {cout <"constructing derive1 class/N ";}~ Kderive1 () // destructor of the derived class 1 {cout <"destructing derive1 class/N" ;}}; class kderive2: Public kderive1 {public: kderive2 () // constructor {cout <"constructing derive2 class/N ";}~ Kderive2 () // destructor of the derived class 2 {cout <"destructing derive2 class/N" ;}}; class kderive3: Public kderive2 {public: kderive3 () // constructor 3 of the derived class {cout <"constructing derive3 class/N ";}~ Kderive3 () // destructor of the derived class 3 {cout <"destructing derive3 class/N" ;}}; int main () {kderive3 OBJ; return 0 ;}
Program result:
Constructing base class
Constructing derive1 class
Constructing derive2 class
Constructing derive3 class
Destructing derive3 class
Destructing derive2 class
Destructing derive1 class
Destructing base class
3. When a derived class contains embedded object members, the execution sequence of the constructor is as follows:
1) First, call the constructors of the base class,
2) second, call the constructor of embedded object members (when multiple object members exist, the calling sequence is determined by the order they declare in the class ),
3) Finally, execute the content in the constructor body of the derived class.
4) when the object is revoked, the calling sequence of the Destructor is the opposite of that of the constructor.
4. Construct rules
4.1. general format of the derived class constructor:
Derived class name (parameter table): Base Class Name (parameter table) {// initialization statement for adding a member to the derived class}
Note: The parameters of the base class constructor are generally derived from the parameter summary table of the derived class constructor and can also be a constant value.
4.2. When a derived class contains embedded object members, the general form of the constructor is as follows:
Derived class name (parameter table): Base Class Name (parameter table 1), embedded object name 1 (embedded object parameter table 1 ),......, Embedded object name N (embedded object parameter table N) {// initialization statement for adding a member to a derived class}
4.3. Example:
# Include <iostream. h> class kbase {PRIVATE: int X; public: kbase (int I) {x = I; cout <"constructing base class/N ";}~ Kbase () {cout <"destructing base class/N";} void show () {cout <"x =" <x <Endl ;}}; class kderived: Public kbase {PRIVATE: kbase D; // The Base Class Object D serves as the object Member of the derived class int y; public: // The constructor format kderived (int I, Int J, int K) when the derived class is embedded with object members: kbase (I), D (j) {Y = K; cout <"constructing derived class/N ";}~ Kderived () {cout <"destructing derived class/N";} void show () {kbase: Show (); D. show (); cout <"Y =" <Y <Endl ;}}; int main () {kderived OBJ (, 8); obj. show (); Return 0 ;}
Program result:
Constructing base class
Constructing base class
Constructing derived class
X = 4
X = 6
Y = 8
Destructing derived class
Destructing base class
Destructing base class
5. The member of the derived class overwrites the member of the same name of the base class.
1. Meaning:
A member with the same name as the base class is defined in the derived class. A member of the derived class overwrites the member with the same name as the base class.
2. Use the same name Member of the base class in the derived class:
2.1 base class name: member name;
2.2 Object Name of the derived class. Base Class Name: member name;
3. Example:
# Include <iostream. h> # include <string. h> // class studentclass kstudent {PRIVATE: char * Name; char * stu_no; float score; public: kstudent (char * name1, char * stu_no1, float score1 );~ Kstudent (); void show () ;}; kstudent: kstudent (char * name1, char * stu_no1, float score1) {name = new char [strlen (name1) + 1]; strcpy (name, name1); stu_no = new char [strlen (stu_no1) + 1]; strcpy (stu_no, stu_no1); score = score1;} kstudent ::~ Kstudent () {Delete [] Name; Delete [] stu_no;} void kstudent: Show () {cout <"/n name:" <name; cout <"/n stu_no:" <stu_no; cout <"/N score:" <score ;}// class kustudentclass kustudent: Public kstudent {PRIVATE: char * Major; public: kustudent (char * name1, char * stu_no1, float score1, char * major1 );~ Kustudent (); void show (); // In the derived class, the member function show () is redefined}; kustudent: kustudent (char * name1, char * stu_no1, float score1, char * major1): kstudent (name1, stu_no1, score1) {major = new char [strlen (major1) + 1]; strcpy (Major, major1);} kustudent: :~ Kustudent () {Delete [] Major;} void kustudent: Show () {kstudent: Show (); // when defining a derived class, cout <"/n Major:" <major <Endl ;}int main () {kustudent stu1 ("liming ", "990201", 90, "computer"); stu1.show (); stu1.kstudent: Show (); // format of the member with the same name of the base class accessed by the derived class Object return 0 ;}
Program result:
Name: Liming
Stu_no: 990201
Score: 90
Major: Computer
Name: Liming
Stu_no: 990201
Score: 90
Vi. Access statement
1. Format: (in the same name segment of the private derived class)
Base Class Name: member function name (or data member name) of the base class );
2. Description:
1) data members and function members can use access declarations;
2) The access declaration method protects members or public members of private Derived classes and base classes (Private Members of the base class cannot be accessed );
3) The access statement must be written in the same name section of the derived class definition;
4) The access statement cannot change the nature of the original base class members. You can only change the protected members of the base class to the protected members of the derived class, the Public Member of the original base class is adjusted to the Public Member of the derived class;
5) The access declaration mechanism can individually adjust the member nature inherited from the base class of the private derived class, so that the outside world can directly access some members of the base class through the interface of the derived class, it does not affect the closeness of other base class members;
6) Note that the format of the function access declaration does not contain the return type and parameters (neither return type nor parentheses can be written );
7) Be careful when using the access Declaration for the overload function (because the access declaration takes effect for all functions with the same name in the base class ).
3. Example:
# Include <iostream. h> class Ka {PRIVATE: int X; public: Ka (INT X1) {x = x1;} void print () {cout <"x =" <X ;}}; class KB: Private ka {PRIVATE: int y; public: KB (INT X1, int Y1 ): KA (X1) {Y = Y1;} ka: Print; // access Declaration. In this case, print becomes a public member of the KB class}; int main () {kb B (10, 20); B. print (); Return 0 ;}
Program result: x = 10
VII. Multi-Inheritance
1. Declaration format:
Class derived class name: Inheritance Method 1 base class name 1 ,......, Inheritance Method N base class name n {// data member and member function added by the derived class };
2. Description:
1) The default Inheritance Method is private;
2) in Multi-inheritance, public inheritance and private inheritance are the same for the Accessability of the base class members in the derived class as the single inheritance rule.
3. constructor and destructor
3.1. constructor Definition Format:
Derived class name (parameter table): base class name 1 (parameter table 1), base class name 2 (parameter table 2 ),......, Base Class Name N (parameter table N) {// initialization statement of the new member in the derived class };
3.2. execution sequence of the constructor: (the execution sequence of the constructor is the same as that of the single inheritance constructor)
1) First, execute the constructors of the base class (the execution sequence of the constructors of each base class at the same level depends on the sequence of the base classes specified when the derived class is declared, it has nothing to do with the order of the member initialization list defined in the derived class constructor );
2) then execute the constructor of the object member;
3) Finally, execute the derived class constructor.
4) The execution sequence of the Destructor is the opposite of that of the constructor.
3.3. Note:
1) The constructor of a Multi-inheritance derived class is similar to that of a single-inheritance derived class. It must call all the base class constructor of the derived class at the same time, the number of parameters of a derived class must contain the number of parameters required to complete the initialization of all base classes;
3.4. Example:
# Include <iostream. h> class kx {PRIVATE: int A; public: kx (int sa) {A = sa; cout <"constructing class kx/N ";}~ Kx () {cout <"destructing class kx/N";} int getx () {return a ;}}; class Ky {PRIVATE: int B; public: KY (int sb) {B = Sb; cout <"constructing class ky/N ";}~ KY () {cout <"destructing class ky/N";} int Gety () {return B ;}}; class KZ: Public kx, private Ky {PRIVATE: int C; public: KZ (int sa, int SB): kx (SA), KY (SB) {c = Sb; cout <"constructing class kz/N ";} ~ KZ () {cout <"destructing class kz/N";} int Getz () {return C ;}/ * int Gety () // use the overload method to make the Gety () function become a public member of the KZ class {return Ky: Gety ();} */KY: Gety; // use the access declaration method to make the function Gety () A public member of the KZ class}; int main () {kz obj (2, 4); int MA = obj. getx (); cout <"A =" <MA <Endl; int MB = obj. gety (); cout <"B =" <MB <Endl; int MC = obj. getz (); cout <"c =" <Mc <Endl; return 0 ;}
Program result:
Constructing class kx
Constructing class Ky
Constructing class kZ
A = 2
B = 4
C = 4
Destructing class kZ
Destructing class Ky
Destructing class kx