C ++ basic syntax and basic syntax
1 Constructor
1. You cannot specify any return value, or even void.
2. Different from Java, c ++ is different from new objects. brackets should be omitted when constructors without parameters declare objects.
2 destructor
1. Add ~ before ~, Parameters are not allowed and return values are not allowed.
2. Only one destructor can be declared and public in each class.
Combination of Three Types
# Include <iostream. h> class A {public: A (int x) {cout <"class A construing \ t" <x <endl ;}~ A () {cout <"class A destroy" <endl ;}}; class B {public: B () {cout <"class B construing \ t" <endl ;}// you can use this method to initialize the private variable B (int x): B (x) {// B = x; cout <"class B construing \ t" <B <endl ;}~ B () {cout <"class B destroying" <endl ;}private: int B ;}; class C {public: C (int x, int y ): a1 (y) {cout <"Class A with y, C with xy construing, but B with nothing" <x <y <endl;} C (int x, int a, int B );~ C () {cout <"class C destroying" <endl;} private: A a1; B b1 ;}; C: C (int x, int, int B): b1 (B), a1 (a) {cout <"class a B c construing" <x <a <B <endl ;} int main () {C c1 (2, 3); C c2 (4, 5, 6); return 0 ;}
When creating a composite class, the constructor of the composite class is called according to the declared sequence of embedded objects in the class, instead of the sequence in the member initialization list. Finally, the constructor of the composite class is called.
The call of the Destructor is the opposite.
The initialization list of a member can be included only when the constructor is defined. If only the constructor is declared, the member initialization list cannot be included.
The member initialization list must also be used to initialize common data members in the class.