C + + Object-oriented inheritance

Source: Internet
Author: User

1. Object-oriented programming has 4 main features: Abstraction, inheritance, encapsulation, polymorphism.

2. Relationships between classes and classes: Include relationships, use relationships, and inheritance relationships.

3. Important Notes on inheritance:

Subclasses have all member variables and member functions of the parent class;

Subclasses can have member methods and properties that the parent class does not have;

Subclass is a special kind of parent class;

The subclass object can be used as the parent class object;

4. The type compatibility principle in inheritance:

The compatibility principle refers to the use of derived class objects wherever the base class is needed, and all members outside the constructor and destructor in the base class are obtained through the public inheritance derived class;

In this way, the derived class has all the functions of the base class, and the derived class can solve the problem that the base class can solve.

4. In the case of inheritance and composition mashup, the order of the constructors and destructors is called:

Call the ancestor's constructor first, call the constructor of the parent class, call the constructor of the member class object, and finally call your own constructor.

Call your own destructor first, call the destructor of the Member class object, call the parent class, and then call the ancestor's destructor.

5. member variables and member functions with the same name in inheritance

In the test function to modify/Invoke the same name member variable, member function, by default, the child class is modified;

6. The Static keyword in inheritance

A static member defined by a base class, shared by all derived classes;

The access control of static members still follows the principle of access control inherited by ordinary members;

A static member in a class is declared in a class without allocating memory, and must be allocated at the same time as the external initialization of the class;

The constructor definition should be defined as public access attribute except in singleton mode;

7. Multiple Inheritance: member variables/attributes that inherit several classes at the same time

Multi-Inheritance ambiguity: Virtual inheritance is implemented by virtual keyword to solve the two semantics of inheritance.

C + + Object-oriented 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.