Reference: http://tieba.baidu.com/p/1167962877
one, the parent class does not write the constructor,
The constructor of a
subclass can be written in "any" form without regard to the constructor of the parent class.
For example: The parent class is a CA, the subclass is CB, no constructor is written in the parent class, the constructor is not written in a subclass, or the constructor can be written in the following ways:
CB () {};
CB (): CA () {};
CB (int a, int b) {};
CB (int a, int b): CA () {};
You can do this in any of several ways. That is, the constructor of the parent class is completely ignored.
Two, the parent class writes out only one parameterless constructor.
Subclasses can not write constructors, or they may be written in the following categories.
CB () {};
CB (): CA () {};
CB (int a, int b) {};
CB (int a, int b): CA () {}; The
is the same when the parent class writes out only one parameterless constructor or no write constructor effect.
Three, the parent class only writes out a parameter constructor
At this point, the subclass should write the constructor "standard", such as
CA (int a, int b) {};
CB (int A, int b): CA (a,b) {};
or:
CA (int a, int b) {};
CB (int A, int b, int c): CA (a,b) {};
Four, the parent class has more than one parameter constructor,
At this point, the subclass implements the constructor of a parent class at least.
Five, the parent class has more than one parameter function and one parameterless constructor.
At this point, a subclass can only implement a constructor of a parent class, regardless of whether the subclass implements a parameter constructor or an parameterless constructor.
It should be noted at this point that the parameterless constructor of the parent class can be ignored even if the subclass is written as CB () {} and is correct. Even written as CB (int a, int b) {};
Summary of the above can be summed up in C + + subclass inherits the parent class when the rule of the constructor is:
1, when the parent class has an explicitly written constructor, the child class minimum implementation of the parent class.
2, when the parent class does not explicitly write the constructor, the subclass can write the constructor without writing the constructor or the "free" function. A more general rule of
is summarized as follows: The constructor of a subclass is based on a constructor that satisfies the parent class at least.