logo

منشئ C ++

في لغة C++، المُنشئ هو أسلوب خاص يتم استدعاؤه تلقائيًا في وقت إنشاء الكائن. يتم استخدامه لتهيئة أعضاء البيانات للكائن الجديد بشكل عام. المُنشئ في C++ له نفس اسم الفئة أو البنية.

باختصار، يتم استدعاء إجراء معين يسمى المُنشئ تلقائيًا عند إنشاء كائن في لغة C++. بشكل عام، يتم استخدامه لإنشاء بيانات أعضاء لأشياء جديدة. في C++، يعمل اسم الفئة أو البنية أيضًا كاسم المُنشئ. عند اكتمال الكائن، يتم استدعاء المنشئ. لأنه يخلق القيم أو يعطي بيانات للشيء، فإنه يعرف باسم المنشئ.

يبدو النموذج الأولي للمنشئين كما يلي:

 (list-of-parameters); 

يتم استخدام بناء الجملة التالي لتحديد منشئ الفئة:

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

يتم استخدام بناء الجملة التالي لتعريف مُنشئ خارج الفئة:

كائن المصفوفة في Java
 : : (list-of-parameters){ // constructor definition} 

يفتقر المنشئون إلى نوع الإرجاع نظرًا لعدم وجود قيمة إرجاع لديهم.

يمكن أن يكون هناك نوعان من المنشئات في لغة C++.

  • المنشئ الافتراضي
  • منشئ المعلمة

C++ المنشئ الافتراضي

يُعرف المنشئ الذي لا يحتوي على وسيطة بالمنشئ الافتراضي. يتم استدعاؤه في وقت إنشاء الكائن.

دعونا نرى المثال البسيط لمنشئ C++ الافتراضي.

 #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++ منشئ المعلمات

يسمى المُنشئ الذي يحتوي على معلمات مُنشئًا ذو معلمات. يتم استخدامه لتوفير قيم مختلفة لكائنات مميزة.

دعونا نرى المثال البسيط لمنشئ معلمات C++.

 #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<<\'>

ما الذي يميز المنشئين عن وظيفة العضو النموذجية؟

  1. اسم المنشئ هو نفس اسم الفصل
  2. الافتراضي لا توجد وسيطة إدخال للمنشئين. ومع ذلك، تتوفر وسيطات الإدخال للنسخ والمنشئات ذات المعلمات.
  3. لا يوجد نوع إرجاع للمنشئين.
  4. يتم استدعاء مُنشئ الكائن تلقائيًا عند إنشائه.
  5. يجب أن يتم عرضه في المنطقة المفتوحة بالفصل الدراسي.
  6. يقوم مترجم C++ بإنشاء مُنشئ افتراضي للكائن إذا لم يتم تحديد مُنشئ (يتوقع أي معلمات ويحتوي على نص فارغ).

باستخدام مثال عملي، دعونا نتعرف على أنواع المنشئات المختلفة في لغة C++. تخيل أنك زرت متجرًا لشراء قلم تحديد. ما هي البدائل المتاحة لك إذا كنت ترغب في شراء علامة؟ بالنسبة للخطوة الأولى، تطلب من أحد المتاجر أن يعطيك علامة، نظرًا لأنك لم تحدد اسم العلامة التجارية أو لون العلامة التي تريدها، فما عليك سوى طلب مبلغ واحد مقابل الطلب. لذا، عندما قلنا للتو، 'أحتاج فقط إلى قلم تحديد'، كان يسلمنا أيًا كان قلم التحديد الأكثر شيوعًا في السوق أو متجره. المُنشئ الافتراضي هو بالضبط ما يبدو عليه! الطريقة الثانية هي الذهاب إلى المتجر وتحديد أنك تريد علامة حمراء للعلامة التجارية XYZ. سوف يعطيك تلك العلامة منذ أن طرحت الموضوع. تم تعيين المعلمات في هذه الحالة وبالتالي. والمنشئ ذو المعلمات هو بالضبط ما يبدو عليه! أما الخيار الثالث فيتطلب منك زيارة أحد المتاجر والإعلان عن رغبتك في الحصول على علامة تبدو بهذا الشكل (علامة فعلية على يدك). وبالتالي سوف يلاحظ صاحب المتجر تلك العلامة. سوف يزودك بعلامة جديدة عندما تقول حسنًا. لذلك، قم بعمل نسخة من تلك العلامة. وهذا ما يفعله منشئ النسخ!

حلقة جافا المحسنة

ما هي خصائص المنشئ؟

  1. المنشئ له نفس اسم الفئة التي ينتمي إليها.
  2. على الرغم من أن ذلك ممكن، إلا أنه يتم عادةً الإعلان عن المنشئات في القسم العام للفصل. ومع ذلك، هذا ليس أمرا لا بد منه.
  3. نظرًا لأن المنشئات لا تُرجع القيم، فإنها تفتقر إلى نوع الإرجاع.
  4. عندما نقوم بإنشاء كائن فئة، يتم استدعاء المنشئ على الفور.
  5. من الممكن أن يكون هناك منشئون مثقلون.
  6. لا يُسمح بالإعلان عن مُنشئ افتراضي.
  7. لا يمكن للمرء أن يرث المنشئ.
  8. لا يمكن الرجوع إلى عناوين المنشئ.
  9. عند تخصيص الذاكرة، يقوم المنشئ بإجراء استدعاءات ضمنية للعوامل الجديدة والحذف.

ما هو منشئ النسخ؟

تقوم وظيفة العضو المعروفة باسم مُنشئ النسخ بتهيئة عنصر باستخدام كائن آخر من نفس الفئة - وهي مناقشة متعمقة حول مُنشئي النسخ.

في كل مرة نحدد فيها مُنشئًا غير افتراضي أو أكثر (مع معلمات) لفئة ما، نحتاج أيضًا إلى تضمين مُنشئ افتراضي (بدون معلمات)، لأن المترجم لن يوفر واحدًا في هذه الحالة. أفضل الممارسات هي الإعلان دائمًا عن المُنشئ الافتراضي، على الرغم من أنه ليس مطلوبًا.

مطلوب إشارة إلى كائن ينتمي إلى نفس الفئة من قبل منشئ النسخة.

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

ما هو المدمر في C++؟

وظيفة العضو الخاص المكافئة للمنشئ هي المدمرة. يقوم المنشئ بإنشاء كائنات فئة يتم تدميرها بواسطة المدمر. الكلمة 'destructor'، متبوعة برمز التلدة ()، هي نفس اسم الفئة. يمكنك تحديد أداة تدمير واحدة فقط في كل مرة. إحدى طرق تدمير كائن تم إنشاؤه بواسطة المنشئ هي استخدام أداة التدمير. ونتيجة لذلك، لا يمكن تحميل المدمرات بشكل زائد. المدمرون لا يأخذون أي حجج ولا يعيدون أي شيء. بمجرد أن يغادر العنصر النطاق، يتم استدعاؤه على الفور. تقوم أدوات التدمير بتحرير الذاكرة المستخدمة بواسطة الكائنات التي أنشأها المُنشئ. يعكس Destructor عملية إنشاء الأشياء عن طريق تدميرها.

اللغة المستخدمة لتعريف أداة تدمير الفئة

 ~ () { } 

اللغة المستخدمة لتعريف مدمر الفصل خارجه

كيفية تحويل السلسلة إلى عدد صحيح جافا
 : : ~ (){}