In C, subclass inherits and calls the constructor method of the parent class.

Source: Internet
Author: User

Transferred from: Library 360

Inherit from and call the constructor method of the parent class in C ++

Constructor is used to initialize the Class Object. Unlike other members of the parent class, it cannot inherit from the quilt class (subclass can inherit all member variables and member methods of the parent class, but does not inherit the constructor of the parent class ). Therefore, when creating a subclass object, the system needs to call the constructor of its parent class to initialize data members inherited from its parent class.

If there is no explicit constructor, the compiler will give a default constructor, and the default constructor will only be created without explicitly declaring the constructor.

The construction principles are as follows:

1. If the subclass does not define a constructor, call the constructor without parameters of the parent class.

2. if a subclass defines a constructor, whether it is a non-parameter or a parameter, when creating a subclass object, first execute the constructor without parameters for the parent class, and then execute its own constructor.

3. When creating a subclass object, if the subclass constructor does not show that the constructor of the parent class is called, the default non-argument constructor of the parent class is called.

4. when creating a subclass object, if the constructor of the subclass does not show that the constructor of the parent class is called and the parent class provides its own non-argument constructor, the non-argument constructor of the parent class is called.

5. when creating a subclass object, if the subclass constructor does not show that the constructor of the parent class is called and the parent class only defines its own constructor with parameters, an error occurs. (if the parent class only has a constructor with parameters, the subclass must display that the constructor with parameters is called ).

6. If the subclass calls the constructor with parameters in the parent class, you need to initialize the parent class member object, for example:

# Include <iostream. h>

Class animal
{
Public:
Animal (INT height, int weight)
{
Cout <"animal construct" <Endl;
}
...
};

Class fish: Public animal
{
Public:
Fish (): Animal (1, 400,300)
{
Cout <"Fish construct" <Endl;
}
...
};
Void main ()
{
Fish FH;
}

Add a colon (:) after the constructor of the fish class, and then add the constructor with parameters of the parent class. In this way, when the constructor of the subclass is called, the system will call the constructor with parameters of the parent class to construct the object. This initialization method is also often used to initialize a constant (const) member in the class, as shown in the following code:

Class Point
{
Public:
Point (): x (0), y (0)
PRIVATE:
Const int X;
Const int y;
};

Of course, common member variables in the class can also be initialized in this way. However, this is unnecessary.

 

 

 

As mentioned above, how can we explicitly call the constructor of the parent class?

1. Write the constructor of the subclass in this way.

Fish (): Animal (1, 400,300)
{
}
...
This method is suitable for subclass having a fixed attribute value at the beginning. That is, run the fish OBJ command. The height and width of the fish are fixed to 300 and 400.

2. Write the constructor of the subclass in this way.

The subclass must explicitly call the constructors with parameters of the parent class. The subclass's own page must overwrite a constructor with parameters. So,

First declare in. H of the subclass, fish (INT height, int width );

Add the following implementation functions to the. cpp subclass:

Fish (INT height, int width): Animal (height, width)
{
         

}

You can.

When the child class is suitable for external use, fish OBJ (100,200);, the constructor of the parent class is successfully called, and the Private member variable of the parent class is initialized.

 

,,,,,,,,,,,,,,,,,,,

The following describes how to call the parent class constructor:

Checklocation: checklocation (void): check_base ("peksha") // case 1 calls the constructor of the base class here for direct initialization, but this solution is too fixed.
{
}
Checklocation: checklocation (void) // default constructor.
{

}

Checklocation: checklocation (string Str) // case 2 explicitly calls the base class constructor here. However, compilation fails.
{
Check_base ("peksha ");
}

Checklocation: checklocation (string Str): check_base (string Str) // case 3 explicitly calls the constructors of the base class. Failed.
{
}
Checklocation: checklocation (string Str): check_base (STR) // case 4 explicitly calls the constructors of the base class here. Successful.
{

}

Note the differences between case3 and case 4.

 

 

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.