The relationship between the C + + subclass parent Constructor

Source: Internet
Author: User

constructor methods that inherit and invoke the parent class in the C + + neutron class

The constructor method is used to initialize the object of the class, unlike the other members of the parent class, which cannot inherit from the child class (the subclass can inherit all the member variables and member methods of the parent class, but not the parent class's constructor method). Therefore, when creating a subclass object, the system needs to invoke the constructor of its parent class in order to initialize the data members inherited from the parent class.

Without an explicit constructor, the compiler gives a default constructor, and the default constructor is created only if the constructor is not explicitly declared.

The construction principles are as follows:

1. If the subclass does not have a constructor method defined, call the constructor of the parent class without arguments.

2. If a subclass defines a construction method, whether it is a parameterless or a parameter, when the object of the subclass is created, it first executes the parent class parameterless constructor, and then executes its own construction method.

3. When creating a subclass object, the default parameterless constructor of the parent class is called if the constructor of the subclass does not display the constructor that invokes the parent class.

4. When a subclass object is created, the parent class's own parameterless constructor is called if the constructor for the subclass does not display the constructor that invokes the parent class and the parent class provides itself with a parameterless constructor.

5. When creating a subclass object, if the constructor of the subclass does not show the constructor of the calling parent class and the parent class only defines its own parameter constructor, an error occurs (if the parent class has only a constructor that has parameters, the subclass must display the call to this parameter constructor).

6. If a subclass invokes a constructor with a parent class with arguments, it needs to initialize the parent class member object in such a way that:

#include <iostream.h>

class animal
{
public:
< Span style= "font-size:18px" > animal (int height, int weight)
{
cout<<" Animal Construc T "<<ENDL;
}
...
};

class Fish:public animal
{
public:
Fish (): Animal (400,300)
{
cout<<" Fish construct "<<ENDL;
}
...
};
void Main ()
{
fish fh;
}

After the fish class's constructor, add a colon (:), and then add the parent class's constructor with parameters. Thus, when the constructor of a subclass is called, the system invokes the constructor of the parent class with parameters to construct the object. This initialization is also often used to initialize constant (const) members in a class, as shown in the following code:

Class Point
{
Public
Point (): X (0), y (0)
Private
const int x;
const int y;
};

Of course, ordinary member variables in a class can also be initialized in this way, however, this is not necessary.

The relationship between the C + + subclass parent Constructor

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.