The class constructor and destructor in C++




c ++ class constructor, c ++ class destructor, constructor and destructor class c ++
You may have noticed, that defining class, we cannot initialize its fields (members) in the definition. You can give it a value, writing the appropriate method of the class and calling its, after the creation of the object outside the class. This method is not very useful, since announcing, let's say, 33 class object we have 33 times to call the method, which assigns values ​​to fields of the class. Therefore,, usually, to initialize class fields, as well as dynamic allocation of memory for, used designer.

Constructor (from the construct-create, build) – this is a special method on a class, which runs automatically when you create an object of class. I.e, if we write it, what values you should initialize the fields during the announcement of a class object, it works without “special invitation”. It is not necessary to specifically call, as a normal class method.

Example:

In strings 11 – 17 define constructor: the name must be identical to the class name; Designer does NOT have a return type (including void). One object is declared once during the class definition – string 25. When the program starts, the constructor for this object will trigger even before entering the main function. This can be seen in the following screenshot:

c ++ class constructor, c ++ class destructor, constructor and destructor class c ++

the program has not yet reached the run string 29 setlocale(LC_ALL, "rus"); , and the constructor already “reported”, that worked (cyrillic was displayed incorrectly). In string 30 – look, what are class fields. The second time the designer will work in string 32, during object creation obj2.

The destructor (от destruct – destroy) – as a special class method, which operates in the destruction of the object class. In most cases it is the role of, to free dynamic memory, which are allocated for the object constructor. The name of his, as with the constructor, must match the class name. Only need to add a character before the name ~

Adding to the previous destructor code. And create a class two constructors: one will take parameters, second – no.

The destructor is defined in strings 34 – 37. For simplicity it just displays the string in place of the program, where work. String 43 – declare an object of the class and pass the data to write into the fields. Here work a constructor with the parameters. In string 46 – trigger default constructor.

c ++ class constructor, c ++ class destructor, constructor and destructor class c ++

See, that the destructor will automatically load twice (because this program was the two class objects). He then fires, when the work program is completed and all data is destroyed.

Important:

  • The constructor and destructor should be public;
  • Constructor and destructor does not have a return type;
  • Class names, constructor and destructor must match;
  • The constructor accepts parameters. A destructor does not accept parameters;
  • When defining a destructor before the name of the symbol should be added ~ ;
  • There can be multiple constructors, but their signature must be different (the number of parameters taken, for example);
  • The destructor in the class must be defined only one.

To support our site – click on the piggy bank and choose any convenient way.

Newsletter of programming:

PureCodeCpp – The basics of programming in C ++ for beginners

The class constructor and destructor in C++
4.1 (82.22%) 9 votes

9 thoughts on “The class constructor and destructor in C++

  1. I understand I am the author live in different time zones, and then I have another 09.11.14
    fun :)

    1. Yes – we here in the land of the rising sun the 10th came to 2 days earlier )) And tomorrow, so generally the 31 th December will!

  2. >> One object is declared once during the class definition - string 25. When you run the program the designer of this object will work even before the entrance to the main function.

    It's not important right “determining during” or later. Called before he, because it is a global object.

  3. >> In most cases it is the role of, to free dynamic memory, which are allocated for the object constructor.

    If the object is allocated within a memory – then yes, I need to write a destructor hands and release it.
    But in your case, the heap is not allocated, but still need a destructor (he must properly destroy the object). But your example it could be and does not write – If no destructor in the class – it will create a default destructor – he will remove the correct object, but the memory, distributed dynamically naturally frees.

    However, there is in C ++ smart pointers, and when to use them – it is almost always possible to rely on the default destructor )).

    Not affected by a lot of questions on the subject of the article, for example:
    – constructor initialization list;
    – virtual destructor;
    – copy constructor;
    – default constructors;
    – exceptions in constructors and destructors;
    – t .p.

    It all will be explained? )

    1. This cute smiley face at the end of a comment…

      Of course, article will still. Copy constructor – be sure to consider a separate article. About the default constructor – I add to this article.

  4. setlocale(LC_ALL, “rus”);
    something in the example of the Russian constructor encoding works, and in the example destructor – no. =:(

    1. Constructor (from the construct-create, build) - is a special class method, which runs automatically when you create an object of class.

Leave a Reply

Your email address will not be published. Required fields are marked *