"C + +" C + + self-learning Advanced (5): Inheritance (1)--Getting Started

Source: Internet
Author: User

Inheritance can be defined by the new class contains all the properties of existing classes, so that many new functions can be implemented, practical application is also more convenient.

A class that already exists is called a base class or parent class , and the newly created class is called a subclass or derived class . Some concepts: single-inheritance, multiple-inheritance (multiple inheritance). Direct inheritance, indirect inheritance. Inheritance is transitive and asymmetric .

I. Defining methods and the composition of derived classes

Single inheritance: class class Name: Inheritance mode base class Name {    ...}; Multiple inheritance: class name: Inherit Way base class name 1, Inherit way base class Name 2 ... {    ......};

The way of inheritance is public, private and protected.

The three-step walk is composed:

① accepts base class members. In addition to constructors and destructors, the whole is accepted. So design carefully to avoid wasting space.

② to transform the base class members. Create a function that is identical to the name, type, parameter table of the member function in the base class, and so on.

③ add a new member. Add your own members.

Second, access control in the inheritance

1. Public inheritance

The derived class that this inheritance establishes is called a common derived class, and the base class is called the common base class.

As for access, I think it's good to use this chart and form in the book.

In derived classes, private (even derived classes) are not accessible, public or public, protected, or protected.

Where protect members are allowed only base classes and derived classes, that is, they can be used for their families, but private can only be used by themselves, and derived classes cannot be used.

2. Private inheritance

corresponding, private derived class, private base class.

3. Protection of inherited protected

Corresponding, protect the derived class, and protect the base class.

The main embodiment is on the indirect derived class. An indirect derived class can access the protected member at its upper level, so protected inherited by the base class member can also be used.

"C + +" C + + self-learning Advanced (5): Inheritance (1)--Getting Started

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.