SỰ RA ĐỜI CỦA PHƯƠNG THỨC KHỞI TẠO (CONSTRUCTOR), PHƯƠNG THỨC HỦY (DESTRUCTOR) TRONG MỘT CLASS

     

A contructor class is a special member function of a class that is executed whenever we create new objects of that class.

Bạn đang xem: Sự ra đời của phương thức khởi tạo (constructor), phương thức hủy (destructor) trong một class


Class Contructor in C ++

A contructor class is a special member function of a class that is executed whenever we create new objects of that class.

A constructor will have the same name as the class và it does not have any return type, including void type.Constructors can be very useful for setting initialization values ​​for specific member variables.

Xem thêm: Chuyển Đọng Cơ Học Lớp 8 Sbt, Giải Sbt Vật Lí 8 Bài 1: Chuyển Động Cơ Học

The following example explains the concept of constructor in C ++:

#include using namespace std ; class Line public : void setChieuDai ( double dẻo ); double layChieuDai ( void ); Line (); // Day la constructor private : double chieudai ; ; // phan dinh nghia cac đê mê thanh vien, bao gom ca constructor Line :: Line ( void ) { cout Compiling and running the above C ++ program will produce the following results:

*

The constructor is parameterized in C ++

A mặc định constructor in C ++ does not have any parameters, but if you need it, a constructor can have parameters. This helps you assign the initial value lớn an object at the time of creating it, as in the following example:

#include using namespace std ; class Line public : void setChieuDai ( double dai ); double layChieuDai ( void ); Line ( double dai ); // Day la phan khai bao constructor private : double chieudai ; ; // phan dinh nghia cac tê mê thanh vien, bao gom constructor Line :: Line ( double dai ) { cout Compiling & running the above C ++ program will produce the following results:

*


Use the initialization menu for initialization fields

In case the constructor is parameterized, you can use the following syntax lớn initialize fields.

Line :: Line ( double dẻo ): chieudai ( dẻo ) { cout The above syntax is equivalent khổng lồ the following syntax:

Line :: Line ( double dẻo ) { cout If with a class in C, you have multiple X, Y, Z, . Fields khổng lồ be initialized, then you can use the same syntax và distinguish the fields by commas, as follows:

C :: C ( double a , double b , double c ): X ( a ), Y ( b ), Z ( c ) .

Destructor class in C ++

A destructor is a special thành viên function of a class that is executed whenever an object of that class is out of scope or whenever the delete expression is applied to lớn a pointer to lớn the object of the class. There.

Xem thêm: Soạn Lịch Sử 8 Bài 5 : Công Xã Pa, Lý Thuyết Lịch Sử Lớp 8 Bài 5: Công Xã Pa

A destructor will have the same name as the class & be preceded by the ~ symbol and it can: not return a value and not receive any parameters. Destructor can be very useful to free resources before exiting the program, for example: closing files, freeing memory .

The following example explains the concept of destructor in C ++:

#include using namespace std ; class Line public : void setChieuDai ( double dẻo ); double layChieuDai ( void ); Line (); // Day la phan khai bao constructor ~ Line (); // Day la phan khai bao destructor private : double chieudai ; ; // phan dinh nghia mê mẩn thanh vien, bao gom constructor va destructor Line :: Line ( void ) { cout Compiling và running the above C ++ program will produce the following results: