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
- Animal (int height, int weight)
- {
- cout<<"Animal construct" <<endl;
- }
- ...
- };
- Class Fish: Publicanimal
- {
- 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.
Exceptions like MFC common CDialog inheritance can be seen in this example:
In the header file
- #pragma once
- Class Cdrugdlg: Public CDialogEx
- {
- Declare_dynamic (CDRUGDLG)
- Public
- Cdrugdlg (cwnd* pparent = NULL); //Standard constructors
- virtual ~cdrugdlg ();
- dialog box data
- enum {IDD = Idd_drug_dialog};
- Protected
- virtual void DoDataExchange (cdataexchange* pDX); //DDX/DDV support
- Declare_message_map ()
- Public
- afx_msg void OnSize (UINT nType, int cx, int cy);
- Virtual BOOL OnInitDialog ();
- afx_msg void OnPaint ();
- afx_msg void OnDestroy ();
- };
Implementation file
DrugDlg.cpp: Implementing Files
- //
- #include "stdafx.h"
- #include "Medical.h"
- #include "DrugDlg.h"
- #include "afxdialogex.h"
- Cdrugdlg dialog box
- Implement_dynamic (Cdrugdlg, CDialog)
- Cdrugdlg::cdrugdlg (cwnd* pparent /*=null*/)
- : CDialogEx (Cdrugdlg::idd, pparent)
- {
- }
- Cdrugdlg::~cdrugdlg ()
- {
- }
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.
Constructor methods that inherit and invoke the parent class in the C + + neutron class