In C + +, the initialization of a single class is simple enough to skip the word.
When a class inherits another class, or there is a class in a class
Class a{
、、、
};
Class B
{
、、、
};
Class c:classa{
Private
B BB;
int x
Public
C (int II): B (ii), BB (ii), X (ii) {};
};
At this point we initialize the settings using C (int II): B (ii), BB (ii), X (ii) There are a few things that must be said clearly:
1. The parameter is passed by C (INT ii) to the class or variable after the colon.
2. The class or variable following the colon is separated by commas
The parameters in 3.C (int II) must be the same as after the colon. Here is the II
4. The inherited Class B (ii) and in B (ii) must have a definition in class B
5. The containing class is defined by the class object, where the class defines the object is BB so it is BB (ii) but must also have a B (int) definition in b
Specific examples are as follows:
#include <iostream>using namespace Std;class x{private:int i;public: X (int II): I (ii) { }; void print () { cout<<i<<endl; }}; class y{ private: int A; Public: Y (int II): A (ii) { }; void print () { cout<<a<<endl; } }; Class Z:public y{ private: int i; X xx; Public: Z (int II): Y (ii), XX (ii), I (ii) { }; void print () { cout<<i<<endl; Y::p rint (); Xx.print (); }}; int main () {Z zzz; Zzz.print (); return 0;}