Some forgetting details about C ++ inheritance

Source: Internet
Author: User

When I read a book on C ++ over the past two days, I feel a little unfamiliar with an inherited knowledge point. Maybe I have never been in touch before, maybe I have never been in touch for too long, but I forget it now. It should be noted down to make a memo. By the way, let's look at the concept of inheritance.

That is the access statement in C ++.

As we all know, C ++ has three inheritance Methods: Public inheritance, protection inheritance, and private inheritance. Public inheritance: the public and protected members of the base class do not change in the nature of the derived class. Protection inheritance: the public and protected members of the base class become protected members in the derived class. The protection member is designed for the inheritance mechanism so that it is visible in the derived class and invisible outside the class. Private inheritance: all the members of the base class are converted into private members in the derived class.

The problem is that when I use some inheritance method, I want to change a member of the base class into another access method. How can I implement it? Therefore, the access adjustment mechanism is used.

Class

{

Publice:

Int get_data (int x, int y );

Int data;

}

Class B: Private

{

Publice:

A: get_data;

Int get_s () {return s ;};

Int set_s () {S = x * Y ;};

PRIVATE:

Int S;

};

Obviously, if a: get_data is not added to publice of B, get_data will become a private member in B. The access declaration can convert it to public.

Note that:

The object declared in the access declaration cannot be of any type or contain parameters or return types.

Private Members of the base class cannot be used for access declaration.

The accessibility of base class members cannot be reduced or improved. For example, the Public Members of the base class cannot be declared as protection, and the protection cannot be declared as public.

The access Declaration for the overload function name will adjust the access domains of all functions with the same name in the base class. However, functions with different access domains in the base class cannot be used for access declaration, a function of the same name as a base class does not support access declaration.

It seems that there have been a lot of errors!

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.