Access attribute problems that are hard to remember in C ++

Source: Internet
Author: User
When learning the inheritance of C ++, we are often confused by access attributes, and it is not easy to remember. It is more difficult for beginners to understand. So how can we remember it and it is not easy to forget it? That is to remember it on the basis of understanding. Most books often describe this piece of text in three paragraphs, and you will be confused if you don't get dizzy, it's okay to extract it into your own language.

Let's take a look at the inheritance form of the class:

Class derived class name: [access attribute] Base class Name
{

}

There are three access attributes: public, protected, and private. This makes it easier to remember. During inheritance:

1. No matter which form (public, protected, or private) is used, private Members in the base class cannot be inherited. If you have to use private members of the base class in the derived class, there are two ways to do this: use attributes, and use friend functions.

2. If the public form is used, all the other private members of the base class are inherited to the derived class as they are; that is, the base class is public, it is public in the derived class, protected in the base class, and protected in the derived class.

3. If the form of protected is used, all other statements of the base class except the private member are inherited to the derived class in the form of protected.

This seems a little organized and easy to remember. Therefore, understanding is very important in the process of learning. It is much easier to remember to interpret the language in the book into your own language.

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.