I've always had a problem with C + + inheritance. I don't quite understand, I tried it today and finally understood
If the base class has a private data member, the derived class will also have a corresponding data member, except that its own member function cannot be accessed, but it can be accessed through the member function of the call base class.
Examples are as follows:
This is a base class.
1 classperson{2 Public:3Person (): Name ("Noname"), Age (0){}4Person (std::stringXName, size_t xage);5PersonConstperson&obj);6person&operator=(Constperson&obj);7STD::stringget_name ();8 voidSet_name (std::string);9 Ten Private: OneSTD::stringname; A size_t age; - -};
Specific implementation:
1Person::p Erson (Constperson&obj) {2Name =Obj.name;3Age =Obj.age;4 }5 6person&7Person::operator=(Constperson&obj) {8Name =Obj.name;9Age =Obj.age;Ten return* This; One } A -STD::string - Person::get_name () { the returnname; - } - - void +Person::set_name (std::stringXName) { -Name =XName; +}
This is a derived class
1 class Public person{2 Public : 3 father () {}4 };
To test the main function:
1 int2Mainvoid){3 father F;4 5Std::cout << f.get_name () <<Std::endl;6F.set_name ("Potter");7Std::cout << f.get_name () <<Std::endl;8 9 return 1;Ten}
Output is
No Name
Potter
In addition, according to one of the others ' blogs that I found, the derived class had to go through three processes:
1. Absorbing base class members: all members except the construction and destructor are absorbed into the derived class
2. Change inherited Members: 1) Change access control permissions (depending on the type of inheritance and the types of access that members have in the base class) 2) overwrite with the same name (the member with the same name in the derived class overrides the base class)
3. Add a member that is unique to the derived class.
Also note that a derived class constructor calls a constructor of the same type as the base class
A problem with C + + inheritance