Constructors in C++: Default, Parameterized and Copy Constructor Notes in Hindi & English : कंस्ट्रक्टर (Constructor): डिफ़ॉल्ट, पैरामीटरयुक्त और कॉपी कंस्ट्रक्टर और 20 महत्वपूर्ण MCQ प्रश्न (Rajasthan IT Exam)

Post a Comment

PGDCA, BCA, B-Tech, MCA, M-Tech, B.Sc computer science राजस्थान सूचना सहायक व कंप्यूटर शिक्षक विशेष नोट्स: इस अध्याय में हम Constructors in C++: Default, Parameterized and Copy Constructor (कंस्ट्रक्टर (Constructor): डिफ़ॉल्ट, पैरामीटरयुक्त और कॉपी कंस्ट्रक्टर) की संपूर्ण थ्योरी, मुख्य तकनीकी बिंदु, तुलनात्मक तालिका और विगत परीक्षाओं के पैटर्न पर आधारित 20 अति-महत्वपूर्ण बहुविकल्पीय प्रश्न (MCQs) विस्तृत हिंदी व अंग्रेजी व्याख्या सहित पढ़ेंगे।

Overview: A constructor is a special C++ member function sharing the exact class name with no return type that executes automatically whenever an object is created to establish valid initial state. C++ supports default, parameterized, and copy constructors (which take a const reference parameter) alongside member initializer lists for efficient initialization.

अध्याय सार: कंस्ट्रक्टर C++ का एक विशेष मेंबर फंक्शन है जिसका नाम क्लास के समान होता है, कोई रिटर्न टाइप नहीं होता और ऑब्जेक्ट बनते समय स्वतः चलकर उसे प्रारंभिक मान देता है। C++ में डिफ़ॉल्ट, पैरामीटरयुक्त और कॉपी कंस्ट्रक्टर (जो const रेफरेंस पैरामीटर लेता है) के साथ तेज़ इनिशियलाइज़ेशन के लिए मेंबर इनिशियलाइज़र लिस्ट का उपयोग होता है।

Constructors in C++: Default, Parameterized and Copy Constructor: Detailed Notes & Architecture (कंस्ट्रक्टर (Constructor): डिफ़ॉल्ट, पैरामीटरयुक्त और कॉपी कंस्ट्रक्टर)

1. Constructor Rules and Default Constructor Semantics (कंस्ट्रक्टर के नियम और डिफ़ॉल्ट कंस्ट्रक्टर की कार्यप्रणाली)

[EN] A constructor must bear the exact name of its class, have NO return type (not even void), and cannot be declared static, virtual, or const. Constructors can be overloaded by differing parameter lists and are automatically invoked upon stack, static, or heap (new) object creation. A default constructor is one callable with zero arguments—either Box() {} or a constructor where every parameter has a default value such as Box(int x = 1). Crucial exam rule: the compiler generates an implicit default constructor ONLY if you define NO constructors at all; defining any parameterized constructor suppresses the compiler-synthesized default constructor.

[HI] कंस्ट्रक्टर का नाम बिल्कुल क्लास के समान होना चाहिए, इसका कोई रिटर्न टाइप (void भी नहीं) नहीं होता और यह static, virtual या const नहीं हो सकता। इन्हें अलग-अलग पैरामीटर लिस्ट के साथ ओवरलोड किया जा सकता है और स्टैक, स्टैटिक या हीप (new) पर ऑब्जेक्ट बनते ही ये स्वतः चलते हैं। डिफ़ॉल्ट कंस्ट्रक्टर वह होता है जिसे बिना किसी आर्गुमेंट के कॉल किया जा सके—या तो Box() या ऐसा कंस्ट्रक्टर जिसके सभी पैरामीटर में डिफ़ॉल्ट मान हों। महत्वपूर्ण परीक्षा नियम: कंपाइलर स्वतः डिफ़ॉल्ट कंस्ट्रक्टर तभी बनाता है जब क्लास में कोई अन्य कंस्ट्रक्टर न लिखा गया हो; पैरामीटरयुक्त कंस्ट्रक्टर लिखने पर डिफ़ॉल्ट कंस्ट्रक्टर स्वतः नहीं बनता।

2. Parameterized Constructors and Member Initializer Lists (पैरामीटरयुक्त कंस्ट्रक्टर और मेंबर इनिशियलाइज़र लिस्ट)

[EN] A parameterized constructor accepts arguments to initialize objects with caller-supplied data, written either as Point p(3, 4); (direct), Point p{3, 4}; (uniform brace initialization), or Point p = Point(3, 4);. Instead of assigning inside the constructor body (x = a;), modern C++ prefers a member initializer list right after the signature: Point(int a, int b) : x(a), y(b) {}. Initializer lists directly construct members in one step rather than default-constructing then reassigning, and they are strictly MANDATORY for initializing const data members, reference members (&), and base/member objects lacking default constructors. Members are always initialized in the exact order they are declared in the class body, NOT the order written in the initializer list.

[HI] पैरामीटरयुक्त कंस्ट्रक्टर ऑब्जेक्ट को उपयोगकर्ता द्वारा दिए गए मानों से शुरू करता है, जैसे Point p(3, 4); या Point p{3, 4};। कंस्ट्रक्टर बॉडी के अंदर असाइनमेंट करने के बजाय सिग्नेचर के बाद मेंबर इनिशियलाइज़र लिस्ट (Point(int a, int b) : x(a), y(b) {}) का प्रयोग अधिक कुशल होता है। const डेटा मेंबर्स, रेफरेंस मेंबर्स (&) और बिना डिफ़ॉल्ट कंस्ट्रक्टर वाले सब-ऑब्जेक्ट्स को इनिशियलाइज़ करने के लिए इनिशियलाइज़र लिस्ट लिखना अनिवार्य होता है। ध्यान रहे कि मेंबर्स हमेशा इनिशियलाइज़र लिस्ट के क्रम में नहीं, बल्कि क्लास के अंदर घोषित किए गए क्रम में इनिशियलाइज़ होते हैं।

3. Copy Constructor: Syntax, Invocation, and Pass-by-Reference Rule (कॉपी कंस्ट्रक्टर: सिंटैक्स, कॉलिंग स्थितियां और रेफरेंस नियम)

[EN] The canonical copy constructor signature is ClassName(const ClassName& other);, creating a new object as a duplicate of an existing one. The parameter MUST be passed by reference (&): if passed by value (ClassName(ClassName other)), copying the argument into other would call the copy constructor recursively forever, triggering a compile error. A copy constructor is invoked in three classic scenarios: (1) direct/copy initialization of a new object from an existing one (Box b2 = b1; or Box b2(b1);), (2) passing an object by value to a function, and (3) returning an object by value (subject to compiler copy elision/RVO). By contrast, b2 = b1; when b2 already exists invokes the copy assignment operator (operator=), NOT the copy constructor!

[HI] कॉपी कंस्ट्रक्टर का मानक सिंटैक्स ClassName(const ClassName& other); होता है, जो किसी मौजूदा ऑब्जेक्ट की नकल से नया ऑब्जेक्ट बनाता है। इसका पैरामीटर रेफरेंस (&) द्वारा पास होना अनिवार्य है: यदि इसे वैल्यू से पास किया जाए (ClassName(ClassName other)), तो आर्गुमेंट कॉपी करने के लिए फिर से कॉपी कंस्ट्रक्टर कॉल होकर अनंत लूप बन जाएगा (कंपाइल एरर)। यह तीन प्रमुख स्थितियों में चलता है: (1) मौजूदा ऑब्जेक्ट से नया ऑब्जेक्ट बनाना (Box b2 = b1; या Box b2(b1);), (2) फंक्शन में ऑब्जेक्ट को बाय-वैल्यू पास करना, और (3) फंक्शन से ऑब्जेक्ट बाय-वैल्यू लौटाना। इसके विपरीत, यदि b2 पहले से मौजूद है तो b2 = b1; लिखने पर कॉपी कंस्ट्रक्टर नहीं बल्कि कॉपी असाइनमेंट ऑपरेटर (operator=) चलता है!

4. Shallow Copy vs Deep Copy and explicit Conversions (शैलो कॉपी बनाम डीप कॉपी और explicit कीवर्ड)

[EN] If no user-defined copy constructor exists, the compiler generates a default copy constructor that performs a memberwise (shallow) copy of every field. For primitive members this works fine, but if the class owns a raw pointer (int *arr = new int[n];), shallow copying copies only the pointer address so two objects point to the exact same heap buffer—causing memory corruption and double-free crashes in destructors. Writing a custom deep-copy constructor allocates a fresh heap block (arr = new int[other.n]) and copies the underlying elements safely (Rule of Three: custom destructor, copy constructor, and copy assignment). Finally, single-argument constructors enable implicit type conversions unless marked explicit (explicit Box(int x)), which prevents unintended automatic conversions.

[HI] यदि प्रोग्रामर कॉपी कंस्ट्रक्टर न लिखे, तो कंपाइलर डिफ़ॉल्ट कॉपी कंस्ट्रक्टर बनाता है जो प्रत्येक मेंबर की शैलो (मेम्बरवाइज) कॉपी करता है। यदि क्लास में हीप मेमोरी की ओर संकेत करने वाला पॉइंटर (int *arr) हो, तो शैलो कॉपी केवल पता नकल करती है जिससे दोनों ऑब्जेक्ट्स एक ही मेमोरी साझा करते हैं और डिस्ट्रक्टर में डबल-फ्री क्रैश होता है। डीप कॉपी कंस्ट्रक्टर नया हीप ब्लॉक आवंटित करके डेटा कॉपी करता है (Rule of Three: डिस्ट्रक्टर, कॉपी कंस्ट्रक्टर और असाइनमेंट ऑपरेटर साथ लिखना)। सिंगल-पैरामीटर कंस्ट्रक्टर के आगे explicit कीवर्ड लगाने से अनचाहे स्वचालित टाइप रूपांतरण (इम्प्लिसिट कन्वर्जन) रुक जाते हैं।

Syntax, Formula & Technical Example (Constructors in C++: Default, Parameterized and Copy Constructor)

#include <iostream>
class Box {
    int *len;
public:
    Box(int v = 1) : len(new int(v)) {}              // Default / Param
    Box(const Box& o) : len(new int(*o.len)) {}      // Deep Copy
    ~Box() { delete len; }
    int get() const { return *len; }
};
int main() {
    Box b1(15), b2 = b1;
    std::cout << b1.get() << ' ' << b2.get() << '\n';
}

Constructors in C++: Default, Parameterized and Copy Constructor Quick Reference & Comparison Table

Constructor TypeSignature ExampleKey Exam Rule
Default ConstructorBox() or Box(int x = 0)Suppressed if any other constructor is user-defined
Parameterized ConstructorBox(int x, int y)Supports overloading and initializer lists
Copy ConstructorBox(const Box& other)Reference (&) required; prevents infinite recursion
Initializer ListBox(int a) : c_val(a) {}Mandatory for const, reference, and base members

Constructors in C++: Default, Parameterized and Copy Constructor Top 20 Bilingual MCQs with Answers

Q1. Which of the following is true regarding the return type of a C++ constructor?

(C++ कंस्ट्रक्टर के रिटर्न टाइप के बारे में निम्नलिखित में से कौन-सा कथन सत्य है?)

(A) A constructor returns int status 0 on success

(B) A constructor must return void

(C) A constructor has no return type at all, not even void

(D) A constructor returns a pointer to the object

Correct Answer: (C)

[EN] Specifying any return type—even void—on a constructor causes a compilation error in C++.

[HI] C++ कंस्ट्रक्टर का कोई रिटर्न टाइप नहीं होता; यहाँ तक कि void लिखने पर भी कंपाइल एरर आता है।

Q2. Why MUST a C++ copy constructor accept its parameter by reference (const T&) rather than by value (T)?

(C++ कॉपी कंस्ट्रक्टर को अपना पैरामीटर वैल्यू (T) के बजाय रेफरेंस (const T&) द्वारा स्वीकार करना क्यों अनिवार्य है?)

(A) C++ forbids passing objects to functions

(B) Passing by value modifies the original object

(C) References use zero CPU registers

(D) Passing by value itself invokes the copy constructor, causing infinite recursion

Correct Answer: (D)

[EN] To pass an object by value, C++ must invoke the copy constructor to construct the parameter, leading to endless self-recursion.

[HI] बाय-वैल्यू पैरामीटर कॉपी करने के लिए स्वयं कॉपी कंस्ट्रक्टर को कॉल करना पड़ता है, जिससे अनंत रिकर्शन हो जाता है।

Q3. Suppose class Book defines only Book(int pages); and no other constructor. What happens when main() writes Book b1;?

(मान लीजिए class Book में केवल Book(int pages); परिभाषित है। main() में Book b1; लिखने पर क्या होगा?)

(A) The compiler silently synthesizes Book() and runs cleanly

(B) pages is automatically initialized to 0

(C) b1 becomes a NULL pointer

(D) Compile-time error: no matching default constructor exists

Correct Answer: (D)

[EN] Defining any user constructor disables automatic synthesis of the compiler's implicit default constructor.

[HI] कोई भी यूजर-डिफाइन्ड कंस्ट्रक्टर लिखने पर कंपाइलर स्वतः डिफ़ॉल्ट कंस्ट्रक्टर बनाना बंद कर देता है, अतः Book b1; पर एरर आएगा।

Q4. Given Test t1; Test t2; already exist, which special function is called by the statement t2 = t1;?

(यदि Test t1; Test t2; पहले से बने हुए हैं, तो स्टेटमेंट t2 = t1; किस विशेष फंक्शन को कॉल करता है?)

(A) Copy constructor

(B) Default constructor

(C) Copy assignment operator (operator=)

(D) Destructor

Correct Answer: (C)

[EN] t2 = t1; assigns into an already constructed object via operator=, whereas Test t2 = t1; constructs a new object via the copy constructor.

[HI] पहले से बने ऑब्जेक्ट में t2 = t1; लिखने पर कॉपी असाइनमेंट ऑपरेटर (operator=) चलता है, जबकि Test t2 = t1; पर कॉपी कंस्ट्रक्टर चलता है।

Q5. Which class members MUST be initialized using a constructor's member initializer list rather than assigned inside the constructor body?

(क्लास के किन मेंबर्स को कंस्ट्रक्टर बॉडी में असाइन करने के बजाय मेंबर इनिशियलाइज़र लिस्ट से इनिशियलाइज़ करना अनिवार्य होता है?)

(A) public float members

(B) static int members

(C) Global variables

(D) const data members and reference (&) members

Correct Answer: (D)

[EN] const and reference members cannot be assigned after creation; they must be initialized in the member initializer list.

[HI] const और रेफरेंस (&) मेंबर्स बनने के बाद असाइन नहीं हो सकते, इसलिए इन्हें इनिशियलाइज़र लिस्ट में इनिशियलाइज़ करना अनिवार्य है।

Q6. In what order are class data members initialized when a constructor uses an initializer list like Demo(int a, int b) : y(b), x(a) {} if class Demo declares int x; before int y;?

(यदि class Demo में int x; पहले और int y; बाद में घोषित है, और कंस्ट्रक्टर : y(b), x(a) लिखा है, तो पहले कौन इनिशियलाइज़ होगा?)

(A) x is initialized first, then y (declaration order in the class)

(B) y is initialized first, then x (initializer list order)

(C) Both are initialized simultaneously in reverse

(D) Alphabetical order of parameter names

Correct Answer: (A)

[EN] C++ always initializes non-static data members in the order they appear in the class declaration, regardless of initializer list order.

[HI] C++ में मेंबर्स हमेशा इनिशियलाइज़र लिस्ट के क्रम में नहीं, बल्कि क्लास में उनकी घोषणा के क्रम (पहले x, फिर y) में इनिशियलाइज़ होते हैं।

Q7. Which problem occurs when two objects share the same dynamically allocated heap pointer due to a compiler-generated shallow copy?

(कंपाइलर द्वारा बनाई गई शैलो कॉपी के कारण जब दो ऑब्जेक्ट्स एक ही हीप पॉइंटर को साझा करते हैं, तो कौन-सी गंभीर समस्या होती है?)

(A) sizeof(object) becomes 0

(B) The class fails to compile

(C) Dangling pointer and double-free crash when both destructors call delete on the same address

(D) Heap memory expands indefinitely without delete

Correct Answer: (C)

[EN] When the first object is destroyed, delete frees the shared buffer, leaving the second object with a dangling pointer that crashes on the second delete.

[HI] पहला ऑब्जेक्ट नष्ट होने पर मेमोरी delete कर देता है, जिससे दूसरे ऑब्जेक्ट का पॉइंटर डैंगलिंग हो जाता है और दूसरे delete पर डबल-फ्री क्रैश होता है।

Q8. How many constructors can a single C++ class define?

(एक अकेली C++ क्लास अधिकतम कितने कंस्ट्रक्टर परिभाषित कर सकती है?)

(A) Exactly one constructor

(B) Multiple overloaded constructors with distinct parameter lists

(C) Maximum two (default and copy only)

(D) Zero if the class has private members

Correct Answer: (B)

[EN] Constructors can be overloaded freely as long as each has a unique parameter signature.

[HI] कंस्ट्रक्टर्स को अलग-अलग पैरामीटर सिग्नेचर के साथ कितनी भी बार ओवरलोड किया जा सकता है।

Q9. What happens if you declare a constructor as virtual (e.g. virtual Box();) in C++?

(यदि आप C++ में किसी कंस्ट्रक्टर को virtual (जैसे virtual Box();) घोषित करें, तो क्या होगा?)

(A) Turns the class into an abstract interface

(B) Calls derived constructor before base constructor

(C) Enables runtime polymorphic object creation automatically

(D) Compilation error: constructors cannot be virtual

Correct Answer: (D)

[EN] Because the virtual table (vptr) is set up during construction, C++ forbids virtual constructors.

[HI] ऑब्जेक्ट बनते समय ही वर्चुअल टेबल सेट होती है, इसलिए C++ में कंस्ट्रक्टर को virtual बनाना वर्जित है और कंपाइल एरर आता है।

Q10. What is the effect of prefixing a single-argument constructor with the explicit keyword (explicit Meter(double m))?

(एक पैरामीटर वाले कंस्ट्रक्टर के आगे explicit कीवर्ड (explicit Meter(double m)) लगाने का क्या प्रभाव होता है?)

(A) Disables default arguments

(B) Forces heap allocation with new

(C) Prevents unintended implicit conversion from double to Meter (such as Meter d = 5.0;)

(D) Makes the constructor private

Correct Answer: (C)

[EN] explicit requires direct initialization like Meter d(5.0); and rejects silent implicit conversions like Meter d = 5.0; or passing 5.0 where Meter is expected.

[HI] explicit लगाने से Meter d = 5.0; जैसे अनचाहे स्वचालित रूपांतरण रुक जाते हैं और केवल Meter d(5.0); जैसी सीधी इनिशियलाइज़ेशन मान्य रहती है।

Q11. Which constructor signature acts as BOTH a default constructor and a parameterized constructor?

(कौन-सा कंस्ट्रक्टर सिग्नेचर डिफ़ॉल्ट और पैरामीटरयुक्त दोनों कंस्ट्रक्टर की तरह काम कर सकता है?)

(A) Point(int x = 0, int y = 0)

(B) Point(const Point& p)

(C) Point(int x, int y)

(D) Point(int& x)

Correct Answer: (A)

[EN] Because all parameters have default values, Point() can be called with 0, 1, or 2 arguments.

[HI] सभी पैरामीटर में डिफ़ॉल्ट मान होने से इसे 0, 1 या 2 आर्गुमेंट्स के साथ कॉल किया जा सकता है।

Q12. When creating an array of 5 objects via Student arr[5];, which constructor is invoked automatically for each element?

(Student arr[5]; द्वारा 5 ऑब्जेक्ट्स का ऐरे बनाने पर प्रत्येक तत्व के लिए स्वतः कौन-सा कंस्ट्रक्टर चलता है?)

(A) The copy constructor, 5 times

(B) The default constructor, 5 times

(C) No constructor runs for arrays

(D) The destructor runs first

Correct Answer: (B)

[EN] Every element of an object array requires default construction (unless explicitly initialized with an aggregate brace list).

[HI] ऑब्जेक्ट ऐरे के प्रत्येक तत्व के लिए डिफ़ॉल्ट कंस्ट्रक्टर 5 बार स्वतः कॉल होता है।

Q13. What does C++11's delegating constructor feature allow?

(C++11 में डेलिगेटिंग कंस्ट्रक्टर (Delegating Constructor) सुविधा क्या अनुमति देती है?)

(A) A constructor calling the destructor manually

(B) A derived constructor calling main()

(C) One constructor calling another constructor of the same class in its member initializer list

(D) Skipping base class construction

Correct Answer: (C)

[EN] Since C++11, ClassName() : ClassName(0, 0) {} delegates initialization to another constructor of the same class cleanly.

[HI] C++11 से एक कंस्ट्रक्टर अपनी इनिशियलाइज़र लिस्ट में ही उसी क्लास के दूसरे कंस्ट्रक्टर को कॉल करके कोड दोहराव बचा सकता है।

Q14. In an inheritance hierarchy where Derived inherits from Base, in what order do constructors execute when a Derived object is instantiated?

(इनहेरिटेंस में जब Derived ऑब्जेक्ट बनाया जाता है, तो कंस्ट्रक्टर्स किस क्रम में चलते हैं?)

(A) Base constructor runs first, then Derived constructor

(B) Derived constructor runs first, then Base constructor

(C) Only Derived constructor runs

(D) They execute in random thread order

Correct Answer: (A)

[EN] A derived object relies on its base subobject being ready first, so Base construction always precedes Derived construction.

[HI] डिराइव्ड ऑब्जेक्ट पहले अपने बेस भाग पर निर्भर करता है, इसलिए हमेशा पहले Base कंस्ट्रक्टर और फिर Derived कंस्ट्रक्टर चलता है।

Q15. What does the Rule of Three state in classical C++ resource management?

(क्लासिकल C++ रिसोर्स मैनेजमेंट में 'रूल ऑफ थ्री' (Rule of Three) क्या कहता है?)

(A) Objects may only be copied three times

(B) If a class defines a custom destructor, copy constructor, or copy assignment operator, it likely needs all three

(C) A class can have at most three constructors

(D) Every class needs three access specifiers

Correct Answer: (B)

[EN] Classes managing raw pointers/resources require all three—destructor, copy constructor, and copy assignment operator—to prevent leaks and double-frees.

[HI] रिसोर्स संभालने वाली क्लास में यदि डिस्ट्रक्टर, कॉपी कंस्ट्रक्टर या कॉपी असाइनमेंट में से किसी एक की जरूरत हो, तो सामान्यतः तीनों को परिभाषित करना चाहिए।

Q16. Which of the following statements about C++ constructors is NOT true?

(C++ कंस्ट्रक्टर्स के बारे में निम्नलिखित में से कौन-सा कथन सत्य नहीं है?)

(A) Constructors can be overloaded with different parameter counts or types

(B) Constructors can accept default parameter values

(C) A copy constructor can be overloaded by changing its return type

(D) If no constructor is declared, the compiler generates a default constructor

Correct Answer: (C)

[EN] Constructors have NO return type at all and functions in C++ can never be overloaded solely by return type.

[HI] कंस्ट्रक्टर का कोई रिटर्न टाइप होता ही नहीं है और C++ में केवल रिटर्न टाइप बदलकर फंक्शन ओवरलोड नहीं किया जा सकता।

Q17. Within Constructors in C++: Default, Parameterized and Copy Constructor, which constructor type directly maps to: "Box() or Box(int x = 0)"?

(Constructors in C++: Default, Parameterized and Copy Constructor में कौन सा Constructor Type सीधे "Box() or Box(int x = 0)" से संबंधित है?)

(A) Initializer List

(B) Parameterized Constructor

(C) Copy Constructor

(D) Default Constructor

Correct Answer: (D)

[EN] In Constructors in C++: Default, Parameterized and Copy Constructor, 'Default Constructor' handles 'Box() or Box(int x = 0)' (Suppressed if any other constructor is user-defined).

[HI] Constructors in C++: Default, Parameterized and Copy Constructor में 'Default Constructor' का कार्य/विवरण 'Box() or Box(int x = 0)' (Suppressed if any other constructor is user-defined) है।

Q18. Which option accurately describes 'Parameterized Constructor' in Constructors in C++: Default, Parameterized and Copy Constructor?

(Constructors in C++: Default, Parameterized and Copy Constructor के संदर्भ में 'Parameterized Constructor' की सही विशेषता कौन सी है?)

(A) Box(const Box& other)

(B) Box(int a) : c_val(a) {}

(C) Box() or Box(int x = 0)

(D) Box(int x, int y)

Correct Answer: (D)

[EN] 'Parameterized Constructor' provides 'Box(int x, int y)' — key point: Supports overloading and initializer lists.

[HI] 'Parameterized Constructor' का मुख्य विवरण 'Box(int x, int y)' है (Supports overloading and initializer lists)।

Q19. In Constructors in C++: Default, Parameterized and Copy Constructor, identify the correct parameter for the specification: "Box(const Box& other)".

(Constructors in C++: Default, Parameterized and Copy Constructor में "Box(const Box& other)" विवरण किस विकल्प के लिए सही है?)

(A) Copy Constructor

(B) Default Constructor

(C) Parameterized Constructor

(D) Initializer List

Correct Answer: (A)

[EN] 'Copy Constructor' corresponds to 'Box(const Box& other)' (Reference (&) required; prevents infinite recursion).

[HI] 'Copy Constructor' का संबंध 'Box(const Box& other)' (Reference (&) required; prevents infinite recursion) से है।

Q20. Considering Constructors in C++: Default, Parameterized and Copy Constructor, what key behavior defines 'Initializer List'?

(Constructors in C++: Default, Parameterized and Copy Constructor में 'Initializer List' की मुख्य भूमिका या मान क्या है?)

(A) Box(int a) : c_val(a) {}

(B) Box(int x, int y)

(C) Box(const Box& other)

(D) Box() or Box(int x = 0)

Correct Answer: (A)

[EN] 'Initializer List' represents 'Box(int a) : c_val(a) {}' (Mandatory for const, reference, and base members).

[HI] 'Initializer List' का अर्थ/मान 'Box(int a) : c_val(a) {}' है (Mandatory for const, reference, and base members)।

Constructors in C++: Default, Parameterized and Copy Constructor — Exam FAQs

Q1: What happens if you declare Test t(); inside main() expecting a default-constructed object?

यदि आप main() में डिफ़ॉल्ट ऑब्जेक्ट बनाने के लिए Test t(); लिख दें, तो क्या गड़बड़ी होती है?

[EN] C++ parses Test t(); as a function declaration (taking no parameters and returning Test)—known as the Most Vexing Parse! Write Test t; or Test t{}; instead.

[HI] C++ इसे ऑब्जेक्ट निर्माण के बजाय एक फंक्शन घोषणा (Most Vexing Parse) मान लेता है! सही तरीका Test t; या Test t{}; लिखना है।

Q2: Can a class have both a default constructor Box() and Box(int x = 0)?

क्या एक ही क्लास में Box() और Box(int x = 0) दोनों एक साथ रखे जा सकते हैं?

[EN] No; writing Box b; would be ambiguous between calling Box() and calling Box(int x = 0) with its default argument, causing a compile error.

[HI] नहीं; इससे Box b; लिखने पर कंपाइलर भ्रमित हो जाएगा कि किस कंस्ट्रक्टर को चुनना है, जिससे एम्बिग्युइटी एरर आएगा।

Q3: Why is initializing members in the initializer list faster than assigning inside the constructor body?

कंस्ट्रक्टर बॉडी में असाइनमेंट करने की तुलना में इनिशियलाइज़र लिस्ट तेज़ क्यों होती है?

[EN] Assigning inside the body first default-constructs non-primitive members (like std::string) and then overwrites them with operator=, whereas initializer lists direct-construct them once.

[HI] बॉडी के अंदर असाइन करने पर मेंबर्स पहले डिफ़ॉल्ट-कंस्ट्रक्ट होते हैं और फिर ओवरराइट होते हैं, जबकि इनिशियलाइज़र लिस्ट सीधे एक ही चरण में निर्माण करती है।

Related Posts

  • Loading related posts...

Post a Comment

Search Topic Notes & 20 MCQs (Hindi / English)