Initialize List
Class Name:: Class name (parameter 1, parameter 2,... Parameter n): data member 1 (parameter 1), data member 2 (parameter 2),..., data member n (parameter N)
{
...
}
Rule 1, initializing the list for data member initialization
Rule 2, initializing the list for the initialization of the parent class
1#include <iostream>2 using namespacestd;3 4 classA5 {6 Private:7 inti;8 Public:9AintII): I (ii)//Initialize listTen { OneStd::cout <<"a::a ()"<<Std::endl; A } -~A () - { theStd::cout <<"~a::a ()"<<Std::endl; - } - voidprint () - { +Std::cout <<"A::p rint ()"<< I <<Std::endl; - } + A voidPrintinti) at { -Std::cout << i <<Std::endl; - print (); - } - - void Set(intII) in { -i =II; to } + }; - the classD | PublicA * { $ Public:Panax NotoginsengB (): A ( the) - { theStd::cout <<"b::b ()"<<Std::endl; + } A~B () the { +Std::cout <<"b::~b ()"<<Std::endl; - } $ voidprint () $ { -Std::cout <<"B::p rint ()"<<Std::endl; - } the voidf () - {Wuyi Set( -); the print (); - } Wu }; - About voidMain () $ { - b b; -B.Set(Ten); - b.print (); A B.f (); +B.A::p rint ( $); the -System"Pause"); $}
When a constructor is defined for a derived class and a base class, the compiler calls the constructor of the base class first, if there is an object member that executes the constructor of the object member, and finally the constructor of the derived class. The destructor is in the exact opposite order of the constructor function.
Object-Oriented Programming-c++_ class 16 subclass parent Relationship