Chapter 8 Object-Oriented Programming (8)

Source: Internet
Author: User

15.4 constructor and replication Control
Constructor and replication Control Members cannot inherit. Each class defines its own constructor and replication control members.
15.4.1 base class constructor and replication Control
Some classes only need special constructors used by the derived classes. Such constructors are defined as protected.
15.4.2 derived class Constructor
The constructor of a derived class is affected by the inheritance relationship. In addition to initializing its own data members, each constructor of a derived class also initializes the base class.
1. default constructor of the synthesized derived class
The synthesis default constructor of a derived class is only different from that of a Non-derived constructor. In addition to initializing the data members of a derived class, the constructor also initializes the base class part of the object of the derived class. The base class is initialized by the default constructor of the base class.
2. Define the default constructor
3. Pass real parameters to the base class Constructor
The initialization list of the derived class constructor can only initialize the members of the derived class, but cannot directly initialize the inherited members. Instead, the derived class constructor indirectly initializes inherited members by including the base class in the constructor initialization list.

Class Base
{
Private:
Int value1, value2;
Protected:
Base (int val1, int val2)
{
Value1 = val1;
Value2 = val2;
}
};
 
Class Child: Base
{
Private:
Int value3;
Public:
Child (int val1, int val2, int val3): value3 (val3), Base (val1, val2 ){}
};
The initialization list of the constructor provides the initial values for the base class and members of the class. It does not specify the initialization execution order. First, initialize the base class, and then initialize the members of the derived class according to the Declaration Order.
4. Use the default real parameters in the derived class Constructor
5. Only the direct base class can be initialized
A class can only initialize its own direct base class. The direct base class is the class specified in the derived list.
Refactoring involves redefining the class hierarchy and moving operations and/or data from one class to another. To adapt to the needs of the application, you are most likely to need to refactor the class to add new functions or handle other changes.
The constructor can only initialize its direct base class because each class defines its own interface. Once a class defines its own interface, all interactions with this class object should be passed through this interface, even if the object is part of a derived class object, it is no exception.
Similarly, the derived class constructor Cannot initialize the base class members and should not assign values to the base class members. If the members are public or protected, the derived constructor can assign values to the base class members in the constructor body. However, this violates the interface of the base class. The derived class should respect the initialization intent of the base class by using the base class constructor, rather than assigning values to these members in the derived class constructor body.

From xufei96's column

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.