Constructors for C + + inheritance

Source: Internet
Author: User
What does a subclass inherit?

When a subclass inherits from the parent class, the public member variable and the member method of the parent class inherit from the quilt class. But the constructor of the parent class is not inherited, in fact it is known that because the subclass and the parent class are different names, and constructors and classes have the same name, constructors have no way to inherit.

Sub-class construction principles

Because the parent class constructor is not inherited, when you create a subclass object, you need to initialize the members of the parent class, that is, the constructor of the parent class needs to be called.
In addition, we know that the C + + compiler will do a lot of things automatically, for example, if you don't have a constructor defined, the compiler will generate a default constructor. If the call to the parent class constructor is not displayed in the subclass constructor, the system defaults to calling the parent class's parameterless constructor ...
In different cases, the construction of subclasses follows the following principles :

  1. If a subclass has a defined constructor, the compiler automatically generates the default constructor and automatically calls the parent class's parameterless constructor when the subclass is constructed.
  2. If the subclass customizes the constructor, but does not show the call to the parent class constructor, when constructing the subclass object, first call the parent class's parameterless constructor and then call the subclass's own constructor.
  3. If the child class defines a constructor and displays the constructor that called the parent class, the constructor specified by the parent class is called first, and then the constructor of the subclass is called.

Note that some situations may be problematic, one of which is that if a constructor is not defined in the class, the compiler generates a default constructor, and if the user defines any of the constructors themselves, the compiler will not generate a default constructor.

Calling the parent class constructor

The way the parent class constructor is called is somewhat analogous to initializing the member list, and when the subclass constructor is defined, the parent class constructor is called after using ":".

Reference links
  • Inheriting and invoking the parent class's constructor in a C + + subclass

Constructors for C + + 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.