"C + +" Inheritance (virtual base class)

Source: Internet
Author: User

Inheritance and derivation of classes

Object-oriented technology emphasizes the reusability of software, which is realized through inheritance mechanism. In the process of inheriting classes, the original class being reused is called the base class, and the newly created class is called the derived class. The derived class definition syntax is formatted as follows:

Class < Derived classes name >: < Inheritance > < base class name 1>, < inherited way > < base class name 2> ...
{
< definition of derived class members >;
}

Inheritance controls how derived class members and derived class objects access members inherited from a base class; ":" After only one base class is single-inheritance, there are multiple base classes for multiple inheritance.

Constructors and destructors for derived classes

Because constructors and destructors for a base class cannot be inherited, in a derived class, you must add a new constructor for the derived class if you initialize the new member of the derived class.

constructor function

Derived classes inherit non-private member functions and data members from the base class, but when you create a derived class object, the system executes the constructor of the derived class without automatically executing the constructor of the base class. If the constructor of the base class contains initialization of the base class data, you should also provide parameters for the initialized base class data member when designing the constructor for the derived class, all of which are provided as an initialization list in the constructor of the derived class, with the following syntax:

< derived class name > (< formal parameter list >): < base class name 1> (< parameter list 1>), < base class name 2> (< formal parameter table 2>) ...
{
< initialization of new data members for derived classes >;
}

When you create a derived class object, the system uses the arguments provided by the constructor parameter table of the derived class, first calling the constructor of the base class.

Destructors

When you execute a destructor for a derived class, the destructor for the base class is also automatically called, in the order that the constructor of the derived class is called in the reverse order, first the destructor of the derived class, and the destructor for the base class.

Virtual base class

Multiple inheritance can cause ambiguity, and both the scope and virtual base techniques in C + + solve this problem, but it is best to try not to use multiple inheritance.

The scope resolution is the class field tag "::" and the syntax format is as follows:

< derived class object name >.< base class name >::< member function name > (< parameter table >);

Virtual base class is another simple and effective way to eliminate ambiguity. For a base class that may produce multiple copies of a member in a different derived path, it can be set to a virtual base class, so that in memory, its data members have only one copy, and the member function has only one mapping, which resolves the problem of unique identification of member functions with the same name.

Virtual base class has the keyword virtual identity, the syntax format is as follows:

Class < derived class name >:virtual < Inheritance method > < base class name >

When using a scope-resolved character, a member of the base class has multiple copies in memory and uniquely identifies the use replica by making a base class name, so that different data can be stored for different operations. Using virtual base class technology, a member of the base class has only one copy in memory. In contrast, the former can hold more data, the latter in a more concise form and save more memory space. You can choose the right way according to the actual situation.

"C + +" Inheritance (virtual base class)

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.