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

Source: Internet
Author: User

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:

  1. #include <iostream.h>
  2.  Class Animal
  3. {
  4.  Public
  5. Animal (int height, int weight)
  6. {
  7. cout<<"Animal construct" <<endl;
  8. }
  9. ...
  10. };
  11.  Class Fish: Publicanimal
  12. {
  13.  Public
  14. Fish (): Animal (400,300)
  15. {
  16. cout<<"Fish construct" <<endl;
  17. }
  18. ...
  19. };
  20.  void Main ()
  21. {
  22. Fish FH;
  23. }

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

  1. #pragma once
  2. Class Cdrugdlg: Public CDialogEx
  3. {
  4. Declare_dynamic (CDRUGDLG)
  5. Public
  6. Cdrugdlg (cwnd* pparent = NULL); //Standard constructors
  7. virtual ~cdrugdlg ();
  8. dialog box data
  9. enum {IDD = Idd_drug_dialog};
  10. Protected
  11. virtual void DoDataExchange (cdataexchange* pDX); //DDX/DDV support
  12. Declare_message_map ()
  13. Public
  14. afx_msg void OnSize (UINT nType, int cx, int cy);
  15. Virtual BOOL OnInitDialog ();
  16. afx_msg void OnPaint ();
  17. afx_msg void OnDestroy ();
  18. };
Implementation file

DrugDlg.cpp: Implementing Files

  1. //
  2. #include "stdafx.h"
  3. #include "Medical.h"
  4. #include "DrugDlg.h"
  5. #include "afxdialogex.h"
  6. Cdrugdlg dialog box
  7. Implement_dynamic (Cdrugdlg, CDialog)
  8. Cdrugdlg::cdrugdlg (cwnd* pparent /*=null*/)
  9. : CDialogEx (Cdrugdlg::idd, pparent)
  10. {
  11. }
  12. Cdrugdlg::~cdrugdlg ()
  13. {
  14. }

This initialization is also often used to initialize constant (const) members in a class, as shown in the following code:

    1. Class Point
    2. {
    3.   Public
    4. Point (): X (0), y (0)
    5.   Private
    6. const int x;
    7. const int y;
    8. };

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

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.