Inheritance in the pass and call order of the parameters
First, the constructor of a simple derived class is passed
The inheritance attribute of the C + + language , which refers to subclasses inheriting the properties and behaviors of the parent class, and the ability to define or add new properties and behaviors again.
Properties and Behaviors in the parent class that are private are inherited, but are still not visited in subclasses. Under the inheritance mechanism. Constructors cannot be inherited, so the parameters of a base class constructor are passed to a subclass constructor.
The declaration (. h) of a subclass constructor of a single inheritance is:
The name of the derived class constructor (the total table of the parameters) ();
The definition of a single-inheritance subclass constructor (in. cpp) is:
Derived class Name:: Derived class constructor name (reference summary table): base class constructor name (table of parameters)
{
The initialization statement of the new member of the derived class;
};
When you define a constructor for a derived class, the parameters that are required for the base class constructor are included in the general table of the constructor and the parameters that are required for the initialization of the new data members of the derived class. The base class constructor name (the reference table) after the colon represents the constructor to call the base class.
#include "stdafx.h" #include <iostream> #include <string> using namespace std; Class parent{public:parent (int a,int b) //base class constructor, need to pass in the initialization of two parameters {//base class This->a = A;this->b = b;} Public:int A, b;}; Class Child:public Parent{public:child (int a,int b,int c);//subclass constructor with reference total table (all parameters) void Showtest () {cout<< "a =" < <a<<endl;cout<< "b =" <<b<<endl;cout<< "c =" <<C<<ENDL;} Public:int c;};/ /subclass Constructor, the initialization of the child::child (int a,int b,int c):p arent (A, B) {///subclass Parameters this->c = C;} By initializing the table enclosing the parameter initialization void Test () {child T1 (T1); Showtest ();} int _tmain (int argc, _tchar* argv[]) {test (); System ("pause"); return 0;}
Test results:
Ii. constructors and destructors for complex derived classes
A newly added member in a derived class can be a simple data member. can also be a class object. The data that a derived class needs to initialize has three parts: an inherited member, a member of a new class object, and a new normal member.
The constructor definition for such a complex derived class is as follows:
Derived class Name:: Derived class constructor name (total tables)
: base class constructor Name 1 (1),
Base class constructor Name 2 (reference 2), ...
Sub-object Name 1 (n),
Sub-object Name 2 (numerical n+1) ...
{
The derived class adds the initialization of a normal data member.
}
Test.cpp: Defines the entry point of the console application. #include "stdafx.h" #include <iostream> #include <string> using namespace std; Class A{public:a () {}a (int i) {A = i;} ~a () {}void Print () {cout<< "A =" <<a<<endl;} int Geta () {return A;} Private:int A;}; Class B{public:b () {}b (int i) {B = i;} ~b () {}void Print () {cout<< "B =" <<b<<endl;} int Getb () {return B;} Private:int b;}; Class C:public a{public:c (void) {}c (int i,int j,int k): A (i), B1 (j) {C = k;} ~c (void) {}void Print () {cout<< "a =" <<geta () <<endl; cout<< "b =" <<b1. GETB () <<endl;cout<< "c =" <<c<<endl;} Private:int C; B B1;}; void Test () {C C1 (C1); Print ();} int _tmain (int argc, _tchar* argv[]) {test (); System ("pause"); return 0;}
Test results:
watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvegf1df96ami=/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center ">
Note: In the VS2010 compilation environment, if you change the declaration of C (void) {} to C (void), a compilation error will occur:
1). obj: error LNK2019: unresolved external symbol "Public: __thiscall c::c (void)" (?
[email protected]@[email protected]). The symbol is in the function "void __cdecl test (void)" (?
referenced in [email protected] @YAXXZ)
2) Fatal error LNK1120: 1 unresolved external commands
The reason is that the other method simply declares the constructor by default and is not implemented. The first is the implementation of the function definition, although ";" Works like {}, but it is different here!
!!
Summary of Knowledge points:
The invocation order of the derived class constructor is as follows:
STEP1: base class constructor.
Call them sequentially, in the order in which they are defined in the derived class.
STEP2: The constructor for the inline object.
Call them sequentially, in the order in which they are defined in the derived class.
Step3: constructor of the derived class.
The destructor of a complex derived class requires only the completion of the new normal member, and the aftermath of the class object and base class is completed by the destructor of the class object and the base class. Destructors are called in the opposite order of constructors.
Inheritance in the pass and call order of the parameters