Summary of C + + review Essentials nine--Inherit two

Source: Internet
Author: User
Tags access properties
A method of handling a member variable with the same name in an inheritance

1. When the child class member variable has the same name as the parent class member variable

2, subclasses still inherit the same name from the parent class member

3. In a subclass by scope resolution:: Make a member of the same name (using a member of the same name in a derived class, explicitly using the class name qualifier)

4. A member of the same name is stored in a different location in memory

Two. Static keywords in derived classes

What happens when inheritance and static keywords come together?

Theoretical knowledge

A static member of the base class definition that will be shared by all derived classes

Ø different access properties in class hierarchy based on the access characteristics of static members and the inheritance of derived classes (subject to access control of derived classes)

A static member is accessed in a derived class and is explicitly described in the following form:

Class Name:: Member

or access the object name through the object. Members

Summarize:

1> static function also adheres to 3 access principles

2> static error (not only to initialize, more important to show the compiler to allocate memory)

The 3> constructor defaults to private

Multiple Inheritance Concepts

Ø A class has multiple inheritance relationships for direct base classes called multiple inheritance

Ø Multiple Inheritance Declaration syntax

Class Derived classes Name: Access Control base class name 1, access control base class Name 2, ..., access control base class name n

{

data member and member function declarations

};

Ø class C can inherit members of class A and Class B at the same time based on access control, and add

Own members

Multi-inheritance derived class constructs and accesses

Ø a derived class constructor for multiple base classes can initialize a data member with an initial call to the base class constructor

Ø the execution order is similar to the single-inheritance constructor. The order in which multiple direct base class constructors are executed depends on the order of each inherited base class that is specified when the derived class is defined.

Ø a derived class object has members of multiple direct or indirect base classes. Access to different names does not appear to be two semantic. If a different base class has a member of the same name, the derived class object should be recognized when accessing it.

Two virtual inheritance

If a derived class derives from more than one base class, and the base class has a common base class, the ambiguity may be generated when the name declared in the base class is accessed.

Analysis:

Summarize:

Ø if a derived class derives from more than one base class, and these base classes have a common

base class, the name that is declared in the base class may have a

Ambiguity

Ø If there is a common base class on multiple inheritance paths, somewhere in the inheritance path

The common base class will produce multiple base class sub-objects in the object of the derived class.

Ø in order for this public base class to produce only one child object in a derived class, the base class must be

Declared as virtual inheritance, which makes this base class A virtual base class.

Ø virtual Inheritance declaration using the keyword virtual

Experiment: Note the number of constructor calls after adding the virtual keyword.

Summary of three succession

Ø inheritance is an important method for object-oriented programming to realize software reuse. A programmer can define a new derived class on the basis of an existing base class.

Ø a derived class of single inheritance has only one base class. Multiple-inheritance derived classes have more than one base class.

The access of a derived class to a base class member is determined by the inheritance mode and the nature of the member.

Ø when creating a derived class object, first call the base class constructor to initialize the base class member in the derived class. The order of the destructor is called and the constructor is called in the reverse order.

Øc++ provides a virtual inheritance mechanism to prevent the two semantics of member access in class inheritance relationships.

Multi-inheritance provides the powerful function of software reuse, and also increases the complexity of the program.

The above is the C + + review key summary of nine--inherit two content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • Related Article

    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.