1. Concept
Inheritance: When defining a new class B, if the class is similar to a known class A (meaning that B has all the features of a), then you can use a as a base class and B as a derived class (also called a subclass).
- Derived classes are obtained by modifying and augmenting the base class. In a derived class, you can extend new member variables and member functions.
- Once a derived class is defined, it can be used independently and not dependent on the base class.
- Derived classes have all member functions and member variables of the base class, whether private,protected or public
- In the individual member functions of a derived class, you cannot access the private members in the base class.
2. Examples of inheritance mechanisms required-student management system
All students have common attributes:
- Name
- School Number
- Gender
- Results
Common methods for all students (member functions)
- Is it time to repeat
- Whether the reward
and different students have their own different properties and methods.
- Postgraduate: Mentor and Department
- University students: a department
- Middle School students: competitive extra points
3, the derivation of the wording of the class
class Public base class Name {};
4. Memory space for derived class objects
The volume of the derived class object is equal to the volume of the base class object, plus the volume of the derived class object's own member variable.
In the derived class object, contains the base class object, and the base class object is stored in a new member of the derived class object
Variable before. The following code shows:
#include <iostream>#include<string.h>using namespacestd;classCBase {intv1, v2;};classCDerived: PublicCBase {intv3;};intMain () {printf ("sizeof (cderived) =%d\n",sizeof(cderived)); return 0;}
The result of the output is: 12
5. Inheriting instance program: Student Status Management
#include <iostream>#include<string.h>using namespacestd;classcstudent {Private: stringname; stringId//Student Number CharGender//' F ' for female, ' M ' for male intAge ; Public: voidPrintinfo () {cout<<"Name:"<< name <<Endl; cout<<"ID:"<< ID <<Endl; cout<<"Gender:"<< Gender <<Endl; cout<<"Age :"<< Age <<Endl; } voidSetInfo (Const string& Name_,Const string&Id_,intAge_,Chargender_) {Name=name_; ID=id_; Age=Age_; Gender=Gender_; } stringGetName () {returnname;};classCundergraduatestudnet: Publiccstudent{Private: stringDepartment//students belong to the Department Public: voidQualifiedforbaoyan () {cout<<"qualified for Baoyan"<<Endl; } voidPrintinfo () {cstudent::P rintinfo ();//call the printinfo of the base classcout <<"Department:"<< Department <<Endl; } voidSetInfo (Const string&name_,Const string&Id_,intAge_,CharGender_,Const string&Department_) {Cstudent::setinfo (name_, Id_, Age_, gender_);//call the SetInfo of the base classDepartment =Department_; }};intMain () {cundergraduatestudnet s2; S2. SetInfo ("Harry Potter","115200", -,'M',"CS"); cout<< S2. GetName () <<Endl; S2. Qualifiedforbaoyan (); S2. Printinfo (); return 0;}
The results of the implementation are as follows:
C + + inheritance and derivation and student management examples