logo

C++ konstruktors

Programmā C++ konstruktors ir īpaša metode, kas objekta izveides laikā tiek izsaukta automātiski. To izmanto, lai inicializētu jauna objekta datu elementus kopumā. C++ konstruktoram ir tāds pats nosaukums kā klasei vai struktūrai.

Īsumā, konkrēta procedūra, ko sauc par konstruktoru, tiek izsaukta automātiski, kad objekts tiek izveidots programmā C++. Kopumā to izmanto, lai izveidotu jaunu lietu datu dalībniekus. Programmā C++ klases vai struktūras nosaukums kalpo arī kā konstruktora nosaukums. Kad objekts ir pabeigts, tiek izsaukts konstruktors. Tā kā tas rada lietas vērtības vai sniedz datus, to sauc par konstruktoru.

Konstruktoru prototips izskatās šādi:

Apple emocijzīmes operētājsistēmā Android
 (list-of-parameters); 

Klases konstruktora definēšanai tiek izmantota šāda sintakse:

 (list-of-parameters) { // constructor definition } 

Lai definētu konstruktoru ārpus klases, tiek izmantota šāda sintakse:

 : : (list-of-parameters){ // constructor definition} 

Konstruktoriem trūkst atgriešanas veida, jo tiem nav atgriešanas vērtības.

C++ valodā var būt divu veidu konstruktori.

  • Noklusējuma konstruktors
  • Parametrizēts konstruktors

C++ noklusējuma konstruktors

Konstruktoru, kuram nav argumentu, sauc par noklusējuma konstruktoru. Tas tiek izsaukts objekta izveides laikā.

Apskatīsim vienkāršu C++ noklusējuma konstruktora piemēru.

 #include using namespace std; class Employee { public: Employee() { cout&lt;<'default constructor invoked'<<endl; } }; int main(void) { employee e1; creating an object of e2; return 0; < pre> <p> <strong>Output:</strong> </p> <pre>Default Constructor Invoked Default Constructor Invoked </pre> <h2>C++ Parameterized Constructor</h2> <p>A constructor which has parameters is called parameterized constructor. It is used to provide different values to distinct objects.</p> <p>Let&apos;s see the simple example of C++ Parameterized Constructor.</p> <pre> #include using namespace std; class Employee { public: int id;//data member (also instance variable) string name;//data member(also instance variable) float salary; Employee(int i, string n, float s) { id = i; name = n; salary = s; } void display() { cout&lt; <id<<' '<<name<<' '<<salary<<endl; } }; int main(void) { employee e1="Employee(101," 'sonoo', 890000); creating an object of e2="Employee(102," 'nakul', 59000); e1.display(); e2.display(); return 0; < pre> <p> <strong>Output:</strong> </p> <pre>101 Sonoo 890000 102 Nakul 59000 </pre> <h2>What distinguishes constructors from a typical member function?</h2> <ol class="points"> <li>Constructor&apos;s name is the same as the class&apos;s</li> <li>Default There isn&apos;t an input argument for constructors. However, input arguments are available for copy and parameterized constructors.</li> <li>There is no return type for constructors.</li> <li>An object&apos;s constructor is invoked automatically upon creation.</li> <li>It must be shown in the classroom&apos;s open area.</li> <li>The C++ compiler creates a default constructor for the object if a constructor is not specified (expects any parameters and has an empty body).</li> </ol> <p>By using a practical example, let&apos;s learn about the various constructor types in C++. Imagine you visited a store to purchase a marker. What are your alternatives if you want to buy a marker? For the first one, you ask a store to give you a marker, given that you didn&apos;t specify the brand name or colour of the marker you wanted, simply asking for one amount to a request. So, when we just said, &apos;I just need a marker,&apos; he would hand us whatever the most popular marker was in the market or his store. The default constructor is exactly what it sounds like! The second approach is to go into a store and specify that you want a red marker of the XYZ brand. He will give you that marker since you have brought up the subject. The parameters have been set in this instance thus. And a parameterized constructor is exactly what it sounds like! The third one requires you to visit a store and declare that you want a marker that looks like this (a physical marker on your hand). The shopkeeper will thus notice that marker. He will provide you with a new marker when you say all right. Therefore, make a copy of that marker. And that is what a copy constructor does!</p> <h2>What are the characteristics of a constructor?</h2> <ol class="points"> <li>The constructor has the same name as the class it belongs to.</li> <li>Although it is possible, constructors are typically declared in the class&apos;s public section. However, this is not a must.</li> <li>Because constructors don&apos;t return values, they lack a return type.</li> <li>When we create a class object, the constructor is immediately invoked.</li> <li>Overloaded constructors are possible.</li> <li>Declaring a constructor virtual is not permitted.</li> <li>One cannot inherit a constructor.</li> <li>Constructor addresses cannot be referenced to.</li> <li>When allocating memory, the constructor makes implicit calls to the new and delete operators.</li> </ol> <h2>What is a copy constructor?</h2> <p>A member function known as a copy constructor initializes an item using another object from the same class-an in-depth discussion on Copy Constructors.</p> <p>Every time we specify one or more non-default constructors (with parameters) for a class, we also need to include a default constructor (without parameters), as the compiler won&apos;t supply one in this circumstance. The best practice is to always declare a default constructor, even though it is not required.</p> <p>A reference to an object belonging to the same class is required by the copy constructor.</p> <pre> Sample(Sample &amp;t) { id=t.id; } </pre> <h2>What is a destructor in C++?</h2> <p>An equivalent special member function to a constructor is a destructor. The constructor creates class objects, which are destroyed by the destructor. The word &apos;destructor,&apos; followed by the tilde () symbol, is the same as the class name. You can only define one destructor at a time. One method of destroying an object made by a constructor is to use a destructor. Destructors cannot be overloaded as a result. Destructors don&apos;t take any arguments and don&apos;t give anything back. As soon as the item leaves the scope, it is immediately called. Destructors free up the memory used by the objects the constructor generated. Destructor reverses the process of creating things by destroying them.</p> <p>The language used to define the class&apos;s destructor</p> <pre> ~ () { } </pre> <p>The language used to define the class&apos;s destructor outside of it</p> <pre> : : ~ (){} </pre> <hr></id<<'></pre></'default>

C++ parametrizētais konstruktors

Konstruktoru, kuram ir parametri, sauc par parametrizēto konstruktoru. To izmanto, lai atsevišķiem objektiem nodrošinātu dažādas vērtības.

Apskatīsim vienkāršo C++ parametrizētā konstruktora piemēru.

 #include using namespace std; class Employee { public: int id;//data member (also instance variable) string name;//data member(also instance variable) float salary; Employee(int i, string n, float s) { id = i; name = n; salary = s; } void display() { cout&lt; <id<<\' \'<<name<<\' \'<<salary<<endl; } }; int main(void) { employee e1="Employee(101," \'sonoo\', 890000); creating an object of e2="Employee(102," \'nakul\', 59000); e1.display(); e2.display(); return 0; < pre> <p> <strong>Output:</strong> </p> <pre>101 Sonoo 890000 102 Nakul 59000 </pre> <h2>What distinguishes constructors from a typical member function?</h2> <ol class="points"> <li>Constructor&apos;s name is the same as the class&apos;s</li> <li>Default There isn&apos;t an input argument for constructors. However, input arguments are available for copy and parameterized constructors.</li> <li>There is no return type for constructors.</li> <li>An object&apos;s constructor is invoked automatically upon creation.</li> <li>It must be shown in the classroom&apos;s open area.</li> <li>The C++ compiler creates a default constructor for the object if a constructor is not specified (expects any parameters and has an empty body).</li> </ol> <p>By using a practical example, let&apos;s learn about the various constructor types in C++. Imagine you visited a store to purchase a marker. What are your alternatives if you want to buy a marker? For the first one, you ask a store to give you a marker, given that you didn&apos;t specify the brand name or colour of the marker you wanted, simply asking for one amount to a request. So, when we just said, &apos;I just need a marker,&apos; he would hand us whatever the most popular marker was in the market or his store. The default constructor is exactly what it sounds like! The second approach is to go into a store and specify that you want a red marker of the XYZ brand. He will give you that marker since you have brought up the subject. The parameters have been set in this instance thus. And a parameterized constructor is exactly what it sounds like! The third one requires you to visit a store and declare that you want a marker that looks like this (a physical marker on your hand). The shopkeeper will thus notice that marker. He will provide you with a new marker when you say all right. Therefore, make a copy of that marker. And that is what a copy constructor does!</p> <h2>What are the characteristics of a constructor?</h2> <ol class="points"> <li>The constructor has the same name as the class it belongs to.</li> <li>Although it is possible, constructors are typically declared in the class&apos;s public section. However, this is not a must.</li> <li>Because constructors don&apos;t return values, they lack a return type.</li> <li>When we create a class object, the constructor is immediately invoked.</li> <li>Overloaded constructors are possible.</li> <li>Declaring a constructor virtual is not permitted.</li> <li>One cannot inherit a constructor.</li> <li>Constructor addresses cannot be referenced to.</li> <li>When allocating memory, the constructor makes implicit calls to the new and delete operators.</li> </ol> <h2>What is a copy constructor?</h2> <p>A member function known as a copy constructor initializes an item using another object from the same class-an in-depth discussion on Copy Constructors.</p> <p>Every time we specify one or more non-default constructors (with parameters) for a class, we also need to include a default constructor (without parameters), as the compiler won&apos;t supply one in this circumstance. The best practice is to always declare a default constructor, even though it is not required.</p> <p>A reference to an object belonging to the same class is required by the copy constructor.</p> <pre> Sample(Sample &amp;t) { id=t.id; } </pre> <h2>What is a destructor in C++?</h2> <p>An equivalent special member function to a constructor is a destructor. The constructor creates class objects, which are destroyed by the destructor. The word &apos;destructor,&apos; followed by the tilde () symbol, is the same as the class name. You can only define one destructor at a time. One method of destroying an object made by a constructor is to use a destructor. Destructors cannot be overloaded as a result. Destructors don&apos;t take any arguments and don&apos;t give anything back. As soon as the item leaves the scope, it is immediately called. Destructors free up the memory used by the objects the constructor generated. Destructor reverses the process of creating things by destroying them.</p> <p>The language used to define the class&apos;s destructor</p> <pre> ~ () { } </pre> <p>The language used to define the class&apos;s destructor outside of it</p> <pre> : : ~ (){} </pre> <hr></id<<\'>

Kas atšķir konstruktorus no tipiskas locekļa funkcijas?

  1. Konstruktora nosaukums ir tāds pats kā klases nosaukums
  2. Noklusējums Konstruktoriem nav ievades argumenta. Tomēr ievades argumenti ir pieejami kopēšanas un parametrizētajiem konstruktoriem.
  3. Konstruktoriem nav atgriešanas veida.
  4. Objekta konstruktors tiek automātiski izsaukts pēc izveides.
  5. Tas ir jāparāda klases atvērtajā zonā.
  6. C++ kompilators objektam izveido noklusējuma konstruktoru, ja konstruktors nav norādīts (sagaida nekādus parametrus un tam ir tukšs pamatteksts).

Izmantojot praktisku piemēru, uzzināsim par dažādiem C++ konstruktoru veidiem. Iedomājieties, ka apmeklējāt veikalu, lai iegādātos marķieri. Kādas ir jūsu alternatīvas, ja vēlaties iegādāties marķieri? Pirmajā gadījumā jūs lūdzat veikalam piešķirt jums marķieri, ņemot vērā to, ka nenorādījāt vēlamā marķiera zīmola nosaukumu vai krāsu, vienkārši prasot vienu summu par pieprasījumu. Tātad, kad mēs tikko teicām: 'Man vajag tikai marķieri', viņš mums iedeva tirgū vai veikalā pieejamo vispopulārāko marķieri. Noklusējuma konstruktors ir tieši tāds, kā tas izklausās! Otra pieeja ir ieiet veikalā un norādīt, ka vēlaties XYZ zīmola sarkano marķieri. Viņš jums dos šo marķieri, jo jūs esat aktualizējis šo tēmu. Šajā gadījumā parametri ir iestatīti šādi. Un parametrizēts konstruktors ir tieši tāds, kā izklausās! Trešajā gadījumā jums jāapmeklē veikals un jāpaziņo, ka vēlaties marķieri, kas izskatās šādi (fizisks marķieris uz rokas). Tādējādi veikalnieks pamanīs šo marķieri. Viņš nodrošinās jūs ar jaunu marķieri, kad jūs pateiksit, ka viss ir kārtībā. Tāpēc izveidojiet šī marķiera kopiju. Un to dara kopiju konstruktors!

rāmis tkinter

Kādas ir konstruktora īpašības?

  1. Konstruktoram ir tāds pats nosaukums kā klasei, kurai tas pieder.
  2. Lai gan tas ir iespējams, konstruktori parasti tiek deklarēti klases publiskajā sadaļā. Tomēr tas nav obligāti.
  3. Tā kā konstruktori neatgriež vērtības, tiem trūkst atgriešanas veida.
  4. Kad mēs izveidojam klases objektu, konstruktors tiek nekavējoties izsaukts.
  5. Iespējami pārslogoti konstruktori.
  6. Konstruktora pasludināšana par virtuālu nav atļauta.
  7. Nevar mantot konstruktoru.
  8. Uz konstruktora adresēm nevar atsaukties.
  9. Piešķirot atmiņu, konstruktors netieši izsauc jaunos un dzēšanas operatorus.

Kas ir kopiju konstruktors?

Dalībnieka funkcija, kas pazīstama kā kopēšanas konstruktors, inicializē vienumu, izmantojot citu objektu no tās pašas klases - padziļināta diskusija par kopēšanas konstruktoriem.

Katru reizi, kad mēs norādām vienu vai vairākus klases konstruktorus, kas nav noklusējuma konstruktori (ar parametriem), mums ir jāiekļauj arī noklusējuma konstruktors (bez parametriem), jo šādā gadījumā kompilators to nenodrošinās. Labākā prakse ir vienmēr deklarēt noklusējuma konstruktoru, pat ja tas nav nepieciešams.

apgrieztā virkne java

Kopiju konstruktoram ir nepieciešama atsauce uz objektu, kas pieder tai pašai klasei.

 Sample(Sample &amp;t) { id=t.id; } 

Kas ir destruktors valodā C++?

Konstruktoram līdzvērtīga īpašā locekļa funkcija ir destruktors. Konstruktors izveido klases objektus, kurus iznīcina destruktors. Vārds 'iznīcinātājs', kam seko tildes () simbols, ir tāds pats kā klases nosaukums. Vienlaikus varat definēt tikai vienu iznīcinātāju. Viena no metodēm, kā iznīcināt konstruktora izgatavotu objektu, ir izmantot destruktoru. Rezultātā iznīcinātājus nevar pārslogot. Destruktori nekādus argumentus nepieņem un neko neatdod. Tiklīdz vienums atstāj tvērumu, tas tiek nekavējoties izsaukts. Destruktori atbrīvo atmiņu, ko izmanto konstruktora ģenerētie objekti. Destruktors apvērš lietu radīšanas procesu, tās iznīcinot.

Valoda, ko izmanto, lai definētu klases iznīcinātāju

 ~ () { } 

Valoda, ko izmanto, lai definētu klases iznīcinātāju ārpus tās

 : : ~ (){}