Chapter fifth inheritance and derivation 5.1 inheritance and derivation of the concept of inheritance and derivation: When a new class B is defined, if the class is similar to an existing Class A (It means B has all the features of a.), then you can use a as abase class, and B as a base classDerived Classes(also known assub-class)。
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 a base class ofall member functions and member variables, whether private, protected, public. In the individual member functions of a derived class, you cannot access the private members in the base class.
Examples of the need for inheritance mechanisms all students have common attributes: Name, school number, gender, achievement all students have common method (member function): whether to repeat, whether the reward
and different students have their own different properties and methods. Postgraduate: Tutor, Department, college students: Department, middle School students: competition extra points
If you write a class from scratch for each class of students, there will obviously be a lot of duplicate code wasted. A better practice is to write a "student" class, summarizing the common characteristics of various students, and then derive from the "Student" class, "middle school students" category, "Graduate class".
The wording of a derived class
Class Derived classes Name: Public base class name {};
Example
Class cstudent{ Private: string sName; int nAge; Public: bool Isthreegood () {}; void SetName (const string & name) {sName = name;} //......}; Class Cundergraduatestudent:public cstudent{ private: int ndepartment; Public: bool Isthreegood () {...};//overwrite bool Canbaoyan () {...};};//derived class is: Class Name: Public base class name
Overwrite: A member function in a derived class is exactly the same as in a base class, but the behavior may need to be different, so the behavior of the function is rewritten in the derived class. Here the feeling should be hidden, rather than covering, as for the difference between overloading, covering, hiding, etc., there is time behind to write a blog summary.
The volume of a derived class object's memory space-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 a derived class object, contains the base class object, and where the base class object is stored in the new member variable of the derived class objectbefore。
Class cbase{ int v1,v2;}; Class cderived:public cbase{ int v3;};
Inheriting instance programs: Student Status Management
#include <iostream> #include <string>using namespace Std;class cstudent{private:string name; String ID; Study number char gender; Gender, ' F ' stands for female, ' M ' stands for male int age; Public:void Printinfo (); void SetInfo (const string & name_,const String & Id_,int Age_, char gender_); String GetName () {return name;}}; Class Cundergraduatestudent:public cstudent{//undergraduates, inheriting the Cstudent class Private:string department;//The name of the department to which the student belongs public: void Qualifiedforbaoyan () {//grant cout << "qualified for Baoyan" << Endl; } void Printinfo () {cstudent::P rintinfo ();//Call Printinfo cout of base class << "Department:" << departme NT <<endl; } void SetInfo (const string & name_,const string & id_,int Age_,char gender_, const string & Department_) {Cstudent::setinfo (Name_,id_,age_,gender_);//Call the base class SetInfo department = Department_; }};void cstudent::P rintinfo () {cout << "Name:" << naMe << Endl; cout << "ID:" << ID << endl; cout << ' Age: ' << age << Endl; cout << "Gender:" << Gender << Endl;} void Cstudent::setinfo (const string & name_,const string & id_,int Age_,char gender_) {name = Name_; id = id_; age = Age_; gender = Gender_;} int main () {cundergraduatestudent s2; S2. SetInfo ("Harry Potter", "118829212", N, ' M ', "Computer Science"); cout << S2. GetName () << ""; S2. Qualifiedforbaoyan (); S2. Printinfo (); return 0;} Output: Harry Potter qualified for Baoyanname:harry Potterid:118829212age:19gender:mdepartment:computer
Like in the Printinfo function of a derived class, a member function of the same name inside the base class is called first to accomplish something related to the base class. Then write some code to accomplish the derivation. This practice is actually particularly common.
PKU C + + program Design Internship Study NOTE 2