Contructor and Destructor in C ++

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 and it does not have any return type, including void type.Constructors can be very useful for setting initialization values ​​for specific member variables.

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

 #include using namespace std ; class Line { public : void setChieuDai ( double dai ); double layChieuDai ( void ); Line (); // Day la constructor private : double chieudai ; }; // phan dinh nghia cac ham thanh vien, bao gom ca constructor Line :: Line ( void ) { cout << "Doi tuong da duoc tao!" << endl ; } void Line :: setChieuDai ( double dai ) { chieudai = dai ; } double Line :: layChieuDai ( void ) { return chieudai ; } // Ham main cua chuong trinh int main ( ) { Line line ; // thiet lap chieu dai cua line line . setChieuDai ( 6.0 ); cout << "Chieu dai cua duong la: " << line . layChieuDai () << endl ; return 0 ; } 

Compiling and running the above C ++ program will produce the following results:

Contructor and Destructor in C ++ Picture 1

The constructor is parameterized in C ++

A default 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 to 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 ham thanh vien, bao gom constructor Line :: Line ( double dai ) { cout << "Doi tuong dang duoc tao, chieudai = " << dai << endl ; chieudai = dai ; } void Line :: setChieuDai ( double dai ) { chieudai = dai ; } double Line :: layChieuDai ( void ) { return chieudai ; } // ham main cua chuong trinh int main ( ) { Line line ( 10.0 ); // lay chieu dai da duoc khoi tao ban dau. cout << "Chieu dai cua line la: " << line . layChieuDai () << endl ; // thiet lap chieu dai mot lan nua line . setChieuDai ( 6.0 ); cout << "Chieu dai cua line la: " << line . layChieuDai () << endl ; return 0 ; } 

Compiling and running the above C ++ program will produce the following results:

Contructor and Destructor in C ++ Picture 2

Use the initialization list for initialization fields

In case the constructor is parameterized, you can use the following syntax to initialize fields.

 Line :: Line ( double dai ): chieudai ( dai ) { cout << "Doi tuong dang duoc tao, chieudai = " << dai << endl ; } 

The above syntax is equivalent to the following syntax:

 Line :: Line ( double dai ) { cout << "Doi tuong dang duoc tao, chieudai = " << dai << endl ; chieudai = dai ; } 

If with a class in C, you have multiple X, Y, Z, . fields to be initialized, then you can use the same syntax and 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 member 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 a pointer to the object of the class. there.

A destructor will have the same name as the class and 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 dai ); double layChieuDai ( void ); Line (); // Day la phan khai bao constructor ~ Line (); // Day la phan khai bao destructor private : double chieudai ; }; // phan dinh nghia ham thanh vien, bao gom constructor va destructor Line :: Line ( void ) { cout << "Doi tuong dang duoc tao" << endl ; } Line ::~ Line ( void ) { cout << "Doi tuong dang bi xoa!" << endl ; } void Line :: setChieuDai ( double dai ) { chieudai = dai ; } double Line :: layChieuDai ( void ) { return chieudai ; } // ham main cua chuong trinh int main ( ) { Line line ; // Thiet lap chieu dai cua line line . setChieuDai ( 6.0 ); cout << "Chieu dai cua line la: " << line . layChieuDai () << endl ; return 0 ; } 

Compiling and running the above C ++ program will produce the following results:

Contructor and Destructor in C ++ Picture 3

According to Tutorialspoint

Previous article: Access Modifier for class in C ++

Next: Copy constructor in C ++

4 ★ | 1 Vote

May be interested

  • How to play Pokemon GO in Landscape Mode on the iPhoneHow to play Pokemon GO in Landscape Mode on the iPhone
    although players can play pokemon go in portrait mode. however, if you want to watch and play games on a large and eye-catching screen, players can switch to playing games in landscape mode.
  • The secret to controlling Pokemon Go employees at workThe secret to controlling Pokemon Go employees at work
    these days, hr managers are faced with an extremely painful problem that is the status of priority employees playing pokemon go more than work. this has caused a small impact on productivity and efficiency.
  • Check out the 'buffalo' Pokémon in Pokémon GoCheck out the 'buffalo' Pokémon in Pokémon Go
    each type of pokemon has hp, cp, ability to attack and endure differently. based on these indicators, players can determine as well as choosing the most powerful pokemon for their offensive tactics.
  • Sitting home can also locate Pokemon around, do you believe it?Sitting home can also locate Pokemon around, do you believe it?
    the tightening of the niantic developers' rules to prevent players from abusing the support tools also brings annoyance, such as those who have no conditions to move much, go away, it is hard to know. get the location of the pokemon around the area they live in
  • 5 undeniable benefits when playing Pokemon Go5 undeniable benefits when playing Pokemon Go
    get to know many new people, breathe fresh air, relieve stress, increase concentration thanks to going out for a walk .... are compelling reasons to force you to try pokemon go now .
  • Want to earn the fastest Pokécoins in Pokémon Go? So don't miss this article!Want to earn the fastest Pokécoins in Pokémon Go?  So don't miss this article!
    pokécoins in pokémon go play the role of buying items in the store. the more coins you earn, the more likely you are to buy more items. to earn pokécoins, players will have to complete certain tasks or buy real money.
  • Pokémon systems when fighting in Pokémon GoPokémon systems when fighting in Pokémon Go
    each pokémon system in pokémon go has different strengths, along with a specific weakness. this type of pokémon will have the power to attack the other pokémon, but can defeat the other pokémon. if you know the characteristics of each type, it will be easier to choose which pokémon to battle.
  • The terms you need to know when playing Pokémon GoThe terms you need to know when playing Pokémon Go
    pokémon go is the most prominent name in recent days. this game of capturing and training virtual animals has created a relatively new way of playing, as players have to constantly move to catch pokémon. during the process of joining pokémon go, you will encounter and use a lot of important terms. so what do they mean?
  • How to play Pokemon GO on Windows computersHow to play Pokemon GO on Windows computers
    recently, pokemon go has become a popular game, attracting thousands of gamers around the world. in previous posts network administrator has guided you on how to play pokemon go on android devices and ios devices. in the article below, network administrator will guide you how to play pokemon go on windows computers using bluestacks emulator software.
  • How to become a good Pokémon Go hunter?How to become a good Pokémon Go hunter?
    with a new way of playing, pokémon go has created a new phenomenon in the game village around the world. players will have to go around and find pokémon for themselves. so how to quickly catch pokémon and in large numbers?