Inheritance
1) Inheritance is an important method for object-oriented programming to realize software reuse. A program ape can define a new derived class based on an existing base class.
2) A derived class of single inheritance has only one base class. Multiple-inheritance derived classes have more than one base class.
3) access to base class members by derived classes is determined by the way of inheritance and the nature of the members.
4) When creating a derived class object, first call the base class constructor to initialize the base class member in the derived class. The order of the destructor is called and the constructor is called in the reverse order.
5) C + + provides virtual inheritance mechanism to prevent the two semantics of members ' access in class inheritance relationships.
6) Multiple inheritance provides a powerful feature of software reuse. The complexity of the program is also added.
class member access level for derived classes
1. Members who need to be visited by outsiders are set directly to public
2. Only members that can be interviewed in the current class are set to private
3. Only members that can be interviewed in the current class and subclass are set to protected. Access permissions for protected members are between public and private.
//class's Inheritance way the sub-class external access properties influence#include <cstdlib>#include <iostream>using namespace STD;classa{Private:intAprotected:intb Public:intC A () {a =0; b =0; c =0; }void Set(intAintBintc) { This->a = A; This->b = b; This->c = C; }};classD | Publica{ Public:voidPrint () {//cout<< "a =" <<a; Err cout<<"b ="<<b;cout<<"C ="<<endl; }};classE |protecteda{ Public:voidPrint () {//cout<< "a =" <<a; Err cout<<"b ="<<b;cout<<"C ="<<endl; }};classB |Privatea{ Public:voidPrint () {//cout<< "a =" <<a; Err cout<<"b ="<<b<<endl;cout<<"C ="<<c<<endl; }};intMAIN_01 (intargcChar*argv[]) {A AA; B BB; C cc; D DD; AA.C = -;//okBB.C = -;//ok //CC.C = n; what does the external meaning of the//err class mean? //DD.C =//errAa.Set(1,2,3); Bb.Set(Ten, -, -);//cc.set (//ee); //dd.set (//ee);Bb.print (); Cc.print (); Dd.print (); System"Pause");return 0;}
type compatibility principles
A subclass object can be used as a parent class object
Subclass objects can be assigned directly to the parent class object
Subclass object can initialize parent class object directly
The parent pointer can point directly to the child class object
The parent class reference can directly refer to the Child class object
#include <cstdlib>#include <iostream>usingnamespace Std;//Subclass is a special kind of parentClass parent03{protected:Const Char* NAME; Public:Parent03() {name ="Parent03"; }voidPrint () {cout<<"Name:"<<name<<endl; }};class CHILD03: Publicparent03{protected:intI Public:Child03(inti) { This->name ="Child2"; This->i = i; }};intMain () {Child03 child03 ( +);///define Parent class object parent class pointer parent class reference childParent03 parent = child03; parent03* pp = &child03; parent03& RP = child03; Parent.print (); Pp->print (); Rp.print (); System"Pause");return 0;}
construction and destruction in inheritance
When a subclass object is constructed. You need to call the parent class constructor to initialize the member to which it inherits
When a subclass object is destructor. You need to call the parent class destructor to clean up the members it inherits from
#include <cstdlib>#include <iostream>using namespace STD;classparent04{ Public: Parent04 (Const Char* s) {cout<<"Parent04 ()"<<" "<<s<<endl; } ~parent04 () {cout<<"~parent04 ()"<<endl; }};classCHILD04: Publicparent04{ Public: Child04 (): Parent04 ("Parameter from child!") {cout<<"Child04 ()"<<endl; } ~child04 () {cout<<"~child04 ()"<<endl; }};voidRun04 () {Child04 child;}intMain_04 (intargcChar*argv[]) {run04 (); System"Pause");return 0;}
1. When the subclass object is created, the constructor of the parent class is called first
2. After the parent class constructor runs, the constructor of the child class is run
3, when the constructor of the parent class has a parameter, it is necessary to display the call in the initialization list of the child class
4. The sequence of destructor calls is the opposite of the constructor function
member variables with the same name in inheritance
1. When the child class member variable has the same name as the parent class member variable
2, subclasses still inherit the same name from the parent class member
3. In a subclass by scope resolution:: Make a member of the same name (using a member of the same name in a derived class, explicitly using the class name qualifier)
4. A member of the same name is stored in a different location in memory
C + + Inheritance Summary