c ++-constructor and destructor

The C ++ constructor and destructor are mainly responsible for constructing objects and destroying objects. After the object scope ends, destroy the objects.

In general, the constructor is used to complete the initial initialization and preparation work (application for allocation of memory), the destructor is used to complete the necessary cleanup work (clear memory)

The constructor characteristic of the initial constructor: the
constructor name is the same as the class name

Definition:
Car(void);
Call:

Car::Car(void)
{
	color="white";
	engine="v8";
	gas_tank=100;
	wheel=4;
}

Add a destructor to the front ~

1. Function name: add ~ before the class name
2. No return value
3. No parameters, can not be overloaded
4. Must be public
5. No destructor, the default destructor
6. Only one analysis in a class Constructor

~Car();

transfer:

Car::~Car()
{
	
}
Published 29 original articles · praised 0 · visits 483

Guess you like

Origin blog.csdn.net/qq_43771959/article/details/104506036