Examples of simple inheritance:
1#include <iostream>2#include <string>3 using namespacestd;4 5 classcstudent6 {7 Private:8 stringname;9 stringId//School NumberTen CharGender//gender, ' F ' stands for female, ' M ' stands for male One intAge ; A Public: - voidprintinfo (); - voidSetInfo (Const string& Name_,Const string&Id_, the intAge_,Chargender_); - stringGetName () {returnname;} - }; - + voidcstudent::P rintinfo () - { +cout <<"Name:"<< name <<Endl; Acout <<"ID:"<< ID <<Endl; atcout <<"Age :"<< Age <<Endl; -cout <<"Gender:"<< Gender <<Endl; - } - - voidCstudent::setinfo (Const string& Name_,Const string&Id_, - intAge_,Chargender_) in { -Name =name_; toID =id_; +Age =Age_; -Gender =Gender_; the } * $ classCundergraduatestudent: PubliccstudentPanax Notoginseng { - //Undergraduate class, inherited the Cstudent class the Private: + stringDepartment//name of the department to which the student belongs A Public: the voidQualifiedforbaoyan () + { - //Qualification for the grant of insurance $cout << "Qualified forBaoyan "<<Endl; $ } - - voidPrintinfo () the { -Cstudent::P rintinfo ();//call the printinfo of the base classWuyicout << "Department:" << Department <<Endl; the } - Wu voidSetInfo (Const string& Name_,Const string&Id_, - intAge_,CharGender_,Const string&Department_) About { $Cstudent::setinfo (Name_,id_,age_,gender_);//call the SetInfo of the base class -Department =Department_; - } - }; A + intMain () the { - cundergraduatestudent S2; $S2. SetInfo ("Harry Potter", "118829212”, +, ' M ', "Computer Science"); thecout << S2. GetName () <<""; the S2. Qualifiedforbaoyan (); the S2. Printinfo (); the return 0; -}
Two kinds of relationships between classes
(1) Inheritance: "Yes" relationship.
Base Class A, B is a derived class of base class A. Logically requires: "A B object is also a object of a".
(2) Compound: "Have" relationship.
Class C has a member variable K, K is an object of Class D, and C and D are composite relationships, which is generally logically required: "D objects are intrinsic properties or components of C objects."
In the geometry program, it is necessary to write "point" class, also need to write "Circle" class
1 class CPoint 2 3 double X, Y, 4 }; 5 Class ccircle: public CPoint Span style= "color: #008080;" >7 { 8 double R; 9 };
It is unreasonable to use a compound relationship; each "Circle" object contains a "point" object, which is the center of the point
1 classCPoint2 {3 Doublex, y;4 //easy to operate the center of the Ccirle class5Friendclassccircle;6 };7 8 classccircle9 {Ten DoubleR; One CPoint Center; A};
Covered
A derived class can define a member with the same name as a base class member, which is called overwrite. When accessing such a member in a derived class, the default is to access the members defined in the derived class. To access a member of the same name defined by the base class in a derived class, use the scope symbol "::"
Access control characters
The following functions can be accessed by the following function
1. Private members of the base class:
(1) member functions of the base class
(2) The friend function of the base class
2. Public members of the base class:
(1) member functions of the base class
(2) The friend function of the base class
(3) member functions for derived classes
(4) The friend function of the derived class
(5) Other functions
3. Protected members of the base class:
(1) member functions of the base class
(2) The friend function of the base class
(3) A member function of a derived class can access the protected member of the base class of the current object
1 classFather2 {3 Private: 4 intNprivate;//Private Members5 Public: 6 intNpublic;//Public Members7 protected: 8 intnprotected;//Protect Members9 };Ten One classSon: PublicFather A { - voidAccessfather () - { theNpublic =1;//OK; -Nprivate =1;//wrong -nprotected =1;//OK, access to the protected member inherited from the base class - Son F; +f.nprotected =1;//wrong, F is not the current object - } + }; A at intMain () - { - Father F; - Son S; -F.npublic =1;//Ok -S.npublic =1;//Ok inf.nprotected =1;//Error -F.nprivate =1;//Error tos.nprotected =1;//Error +S.nprivate =1;//Error - return 0; the}
constructors for derived classes
When you create an object of a derived class, you call the constructor of the base class: Initializes a member of the derived class object that inherits from the base class. The constructor for the base class is always executed before executing a constructor for a derived class.
Two ways to call a base class constructor
(1) Explicit mode: In the constructor of the derived class, provide parameters for the constructor of the base class. Derived::d erived (arg_derived-list): Base (arg_base-list)
(2) Implicit: In the constructor of a derived class, when the base class constructor is omitted, the constructor of the derived class automatically calls the default constructor of the base class.
When a destructor for a derived class is executed, the destructor for the base class is automatically called after the destructor of the derived class is executed.
When you create an object of a derived class:
(1) The constructor of the base class is executed first to initialize the members inherited from the base class in the derived class object;
(2) Executes the constructor of the member object class to initialize the member object in the derived class object.
(3) Finally execute the derived class's own constructor
When a derived class object dies:
(1) Perform the derived class's own destructor first
(2) Execute the destructor of each member object class in turn
(3) Last execution of the destructor of the base class
Destructors are called in the opposite order of the constructor's call order.
Assignment rules
(1) An object of a derived class can be assigned to a base class object
b = D;
(2) A derived class object can initialize a base class reference
Base & br = D;
(3) The address of a derived class object can be assigned to a base class pointer
Base * PB = & D;
If the derivation is private or protected, then the above three lines are not available.
In the case of a public derivation, pointers to derived class objects can be assigned directly to the base class pointer
Base * Ptrbase = &objDerived;
(1) Ptrbase points to an object of the derived class;
*ptrbase can be thought of as a base class object that accesses its public members directly through Ptrbase, but cannot access a member of the derived class in the Objderived object that is not part of the base class through Ptrbase
(2) Even if the base-class pointer is pointing to an object of a derived class, there is no access to the base class pointer through the base class, and there are members in the derived class.
(3) By forcing a pointer type conversion, you can convert the ptrbase to a pointer to the derived class
Base * Ptrbase = &objDerived;
Derived *ptrderived = (Derived *) ptrbase;
Make sure that ptrbase points to an object of the derived class, otherwise it is easy to make an error.
New standard C + + programming Reading notes _ inheritance and polymorphism