A problem with C + + inheritance

Source: Internet
Author: User

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


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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.