Analysis of constructor and constructor in C ++ Language
# Include
Using namespace std; class Dog {// by default, the variables are defined as private int a; public: // both functions can only be called automatically, cannot be manually called // The constructor is public // usage: // initialize the member variables or member methods of this class // allocate space Dog (int ); // destructor // The Destructor does not return values. It cannot pass parameters or be overloaded. Only one or more of them can be called automatically before the space is released. If it is a stack zone, it is automatically called before the stack is destroyed. If it is a global zone or a static zone, it is automatically called before the process ends. // usage: // release space ~ Dog () ;}; int main (void) {cout <"befor... "<endl; Dog dog (10); cout <" after... "<endl; return 0;} Dog: Dog (int a) {this-> a = a; cout <" hello "<endl;} Dog ::~ Dog () {cout <"good bey" <endl ;}
Running result: