C++ Classes and Objects: Blueprint, Instances, Access Specifiers Notes in Hindi & English : C++ क्लास और ऑब्जेक्ट्स: डेटा मेंबर्स, मेथड्स और एक्सेस विजिबिलिटी और 20 महत्वपूर्ण MCQ प्रश्न (Rajasthan IT Exam)

Post a Comment

PGDCA, BCA, B-Tech, MCA, M-Tech, B.Sc computer science राजस्थान सूचना सहायक व कंप्यूटर शिक्षक विशेष नोट्स: इस अध्याय में हम C++ Classes and Objects: Blueprint, Instances, Access Specifiers (C++ क्लास और ऑब्जेक्ट्स: डेटा मेंबर्स, मेथड्स और एक्सेस विजिबिलिटी) की संपूर्ण थ्योरी, मुख्य तकनीकी बिंदु, तुलनात्मक तालिका और विगत परीक्षाओं के पैटर्न पर आधारित 20 अति-महत्वपूर्ण बहुविकल्पीय प्रश्न (MCQs) विस्तृत हिंदी व अंग्रेजी व्याख्या सहित पढ़ेंगे।

Overview: In C++, a class is a user-defined blueprint that defines attributes (data members) and behaviors (member functions) without allocating memory until an object instance is created. Objects store separate copies of non-static data members in memory while sharing class methods, accessing public members via the dot (.) or arrow (->) operator.

अध्याय सार: C++ में क्लास एक यूजर-डिफाइन्ड ब्लूप्रिंट (खाका) है जो डेटा मेंबर्स (गुण) और मेंबर फंक्शन्स (व्यवहार) को परिभाषित करती है तथा ऑब्जेक्ट बनने तक मेमोरी नहीं घेरती। प्रत्येक ऑब्जेक्ट मेमोरी में नॉन-स्टैटिक डेटा मेंबर्स की अलग कॉपी रखता है जबकि सभी ऑब्जेक्ट्स क्लास मेथड्स को साझा करते हैं और पब्लिक मेंबर्स को डॉट (.) या एरो (->) ऑपरेटर से एक्सेस करते हैं।

C++ Classes and Objects: Blueprint, Instances, Access Specifiers: Detailed Notes & Architecture (C++ क्लास और ऑब्जेक्ट्स: डेटा मेंबर्स, मेथड्स और एक्सेस विजिबिलिटी)

1. Class Declaration Syntax and Object Instantiation (क्लास घोषणा सिंटैक्स और ऑब्जेक्ट निर्माण)

[EN] A class is declared using the class keyword followed by an identifier, a body enclosed in curly braces {}, and a mandatory terminating semicolon (;). Because a class definition only describes a type layout to the compiler, declaring the class itself consumes zero bytes of runtime RAM. When an object is instantiated (such as Box b1; on the stack or Box *p = new Box(); on the heap), memory is allocated for all non-static data members of that instance. Notably, C++ guarantees that an empty class with no data members still has sizeof(Empty) >= 1 byte (typically 1 byte) so that two distinct objects in an array always occupy unique memory addresses.

[HI] क्लास को class कीवर्ड, नाम, कर्ली ब्रेसिज़ {} के अंदर बॉडी और अंत में अनिवार्य सेमीकोलन (;) लगाकर घोषित किया जाता है। क्लास केवल कंपाइलर को डेटा टाइप की संरचना बताती है, इसलिए केवल क्लास परिभाषित करने से रनटाइम RAM में कोई मेमोरी खर्च नहीं होती। जब कोई ऑब्जेक्ट बनाया जाता है (जैसे स्टैक पर Box b1; या हीप पर Box *p = new Box();), तब उस इंस्टेंस के सभी नॉन-स्टैटिक डेटा मेंबर्स के लिए मेमोरी आवंटित होती है। महत्वपूर्ण परीक्षा तथ्य यह है कि बिना किसी डेटा मेंबर वाली खाली क्लास का भी sizeof(Empty) कम से कम 1 बाइट होता है ताकि दो अलग ऑब्जेक्ट्स का मेमोरी एड्रेस अलग रहे।

2. Data Members, Member Functions, and Scope Resolution (::) (डेटा मेंबर्स, मेंबर फंक्शन्स और स्कोप रेज़ॉल्यूशन (::))

[EN] Member functions (methods) define operations on the object's state and can be written either inside the class body or defined outside using the scope resolution operator (::). Methods defined directly inside the class body are implicitly treated as inline requests by the compiler, whereas larger methods are typically declared as prototypes inside the class and defined outside as ReturnType ClassName::methodName(). Only one copy of each member function's machine instructions exists in the code segment and is shared across all objects of that class. Static data members (static int count;) are likewise shared across all instances in static storage rather than replicated inside individual objects.

[HI] मेंबर फंक्शन्स (मेथड्स) ऑब्जेक्ट के डेटा पर कार्य करते हैं और इन्हें क्लास बॉडी के भीतर या बाहर स्कोप रेज़ॉल्यूशन ऑपरेटर (::) द्वारा परिभाषित किया जा सकता है। क्लास के भीतर सीधे परिभाषित फंक्शन्स को कंपाइलर स्वतः inline मानता है, जबकि बड़े मेथड्स को क्लास में प्रोटोटाइप देकर बाहर ReturnType ClassName::methodName() के रूप में लिखा जाता है। मेंबर फंक्शन्स का मशीन कोड मेमोरी के कोड सेगमेंट में केवल एक बार स्टोर होता है और सभी ऑब्जेक्ट्स द्वारा साझा किया जाता है। इसी प्रकार static डेटा मेंबर्स प्रत्येक ऑब्जेक्ट में दोहराए जाने के बजाय पूरी क्लास के लिए एक साझा कॉपी के रूप में रहते हैं।

3. The Implicit this Pointer and Member Access (. vs ->) (इम्प्लिसिट this पॉइंटर और मेंबर एक्सेस (. बनाम ->))

[EN] Whenever a non-static member function is invoked on an object such as obj.show(), the compiler secretly passes the address of obj as a hidden constant pointer parameter named this (of type ClassName* const for non-const methods). Using this->x disambiguates class data members from identically named local parameters (such as this->age = age;) and enables method chaining via return *this;. Callers access public members through the direct member selection dot operator (obj.area()) for objects or references, and the arrow operator (ptr->area(), equivalent to (*ptr).area()) for object pointers. Static member functions lack a this pointer because they belong to the class itself rather than any specific object instance.

[HI] जब भी किसी ऑब्जेक्ट पर obj.show() जैसा नॉन-स्टैटिक मेंबर फंक्शन कॉल होता है, कंपाइलर चुपचाप obj का पता एक छिपे पॉइंटर पैरामीटर this (ClassName* const) के रूप में पास करता है। this->age = age; लिखकर पैरामीटर और डेटा मेंबर के समान नाम के टकराव को सुलझाया जा सकता है तथा return *this; से मेथड चेनिंग की जा सकती है। सामान्य ऑब्जेक्ट या रेफरेंस के पब्लिक मेंबर्स डॉट ऑपरेटर (obj.area()) से और ऑब्जेक्ट पॉइंटर के मेंबर्स एरो ऑपरेटर (ptr->area(), जो (*ptr).area() के समतुल्य है) से एक्सेस होते हैं। स्टैटिक मेंबर फंक्शन्स किसी विशेष ऑब्जेक्ट से नहीं जुड़े होते, इसलिए उनमें this पॉइंटर उपलब्ध नहीं होता।

4. Class vs Struct and Default Access Visibility in C++ (C++ में Class बनाम Struct और डिफ़ॉल्ट एक्सेस विजिबिलिटी)

[EN] In C++, both struct and class can hold data members, member functions, constructors, destructors, static members, and participate in inheritance and polymorphism. The sole technical difference between them is default visibility: members and base classes of a class default to private, whereas members and base classes of a struct default to public. Inside a class body, if members are listed before any access specifier label (private:, public:, or protected:), the compiler treats them as private automatically. By convention, C++ programmers use struct for passive data aggregates with public fields and class for encapsulated abstractions enforcing invariants.

[HI] C++ में struct और class दोनों ही डेटा मेंबर्स, मेथड्स, कंस्ट्रक्टर्स, डिस्ट्रक्टर्स, इनहेरिटेंस और पॉलीमॉर्फिज्म को सपोर्ट करते हैं। दोनों के बीच एकमात्र तकनीकी अंतर डिफ़ॉल्ट एक्सेस विजिबिलिटी का है: class में मेंबर्स और बेस क्लास डिफ़ॉल्ट रूप से private होते हैं, जबकि struct में वे डिफ़ॉल्ट रूप से public होते हैं। किसी class बॉडी में पहले एक्सेस स्पेसिफायर लेबल से पहले घोषित सभी मेंबर्स स्वतः private माने जाते हैं। प्रोग्रामिंग परंपरा के अनुसार, सरल डेटा समूहों के लिए struct और सख्त एनकैप्सुलेशन वाले ऑब्जेक्ट्स के लिए class का प्रयोग किया जाता है।

Syntax, Formula & Technical Example (C++ Classes and Objects: Blueprint, Instances, Access Specifiers)

#include <iostream>
class Rectangle {
    int w, h; // private by default
public:
    void set(int w, int h) { this->w = w; this->h = h; }
    int area() const;
};
int Rectangle::area() const { return w * h; }
int main() {
    Rectangle r, *ptr = &r;
    r.set(5, 4);
    std::cout << ptr->area() << '\n';
}

C++ Classes and Objects: Blueprint, Instances, Access Specifiers Quick Reference & Comparison Table

ConceptC++ Rule / SyntaxExam Note
class Empty {};sizeof(Empty) is at least 1 byteEnsures unique addresses for distinct objects
Default visibilityprivate in class; public in structOnly syntactic default difference in C++
Out-of-line methodReturnType ClassName::method()Uses :: scope resolution; not implicitly inline
this pointerHidden ClassName* const in methodsAbsent inside static member functions

C++ Classes and Objects: Blueprint, Instances, Access Specifiers Top 20 Bilingual MCQs with Answers

Q1. Which punctuation mark MUST appear immediately after the closing curly brace '}' of a C++ class definition?

(C++ क्लास परिभाषा के समापन कर्ली ब्रेस '}' के तुरंत बाद कौन-सा विराम चिन्ह होना अनिवार्य है?)

(A) Semicolon (;)

(B) Period (.)

(C) Comma (,)

(D) Colon (:)

Correct Answer: (A)

[EN] A class declaration allows optional variable declarators before the terminating semicolon, so omitting ';' causes a syntax error.

[HI] क्लास परिभाषा के अंत में ';' लगाना अनिवार्य है अन्यथा कंपाइल एरर आता है।

Q2. What is the default access specifier for members declared inside a C++ class before any label?

(किसी भी लेबल से पहले C++ class के अंदर घोषित मेंबर्स का डिफ़ॉल्ट एक्सेस स्पेसिफायर क्या होता है?)

(A) protected

(B) friend

(C) private

(D) public

Correct Answer: (C)

[EN] Members of a class default to private, whereas members of a struct default to public.

[HI] C++ में class के मेंबर्स डिफ़ॉल्ट रूप से private होते हैं, जबकि struct के मेंबर्स public होते हैं।

Q3. What does sizeof(Empty) typically return in C++ for class Empty {}; having zero data members?

(बिना किसी डेटा मेंबर वाली खाली क्लास class Empty {}; के लिए C++ में sizeof(Empty) सामान्यतः क्या मान देता है?)

(A) Compile-time error

(B) 1 byte (non-zero)

(C) 0 bytes

(D) -1 bytes

Correct Answer: (B)

[EN] The C++ standard requires complete objects to have non-zero size (commonly 1 byte) so distinct objects in an array have distinct addresses.

[HI] मानक C++ के अनुसार खाली क्लास के ऑब्जेक्ट का आकार शून्य नहीं हो सकता (सामान्यतः 1 बाइट) ताकि ऐरे में दो ऑब्जेक्ट्स के एड्रेस अलग रहें।

Q4. Which operator is used to define a member function outside its class declaration?

(क्लास परिभाषा के बाहर किसी मेंबर फंक्शन को परिभाषित करने के लिए किस ऑपरेटर का उपयोग किया जाता है?)

(A) :: (Scope resolution operator)

(B) & (Address-of operator)

(C) . (Dot operator)

(D) -> (Arrow operator)

Correct Answer: (A)

[EN] Defining a method outside the class requires qualifying its name with ClassName::methodName().

[HI] क्लास के बाहर मेथड परिभाषित करने के लिए ClassName::methodName() के रूप में :: स्कोप रेज़ॉल्यूशन ऑपरेटर लगता है।

Q5. How do you invoke a public method display() using an object pointer Student *ptr = new Student();?

(ऑब्जेक्ट पॉइंटर Student *ptr = new Student(); के माध्यम से पब्लिक मेथड display() को कैसे कॉल किया जाता है?)

(A) *ptr.display()

(B) ptr->display()

(C) ptr.display()

(D) ptr::display()

Correct Answer: (B)

[EN] Object pointers require the arrow operator ptr->display() (or (*ptr).display() with parentheses, because '.' has higher precedence than '*').

[HI] पॉइंटर के लिए एरो ऑपरेटर ptr->display() या कोष्ठक सहित (*ptr).display() का उपयोग होता है।

Q6. Which member functions of a class do NOT receive an implicit this pointer?

(क्लास के किन मेंबर फंक्शन्स को इम्प्लिसिट this पॉइंटर प्राप्त नहीं होता है?)

(A) static member functions

(B) const member functions

(C) inline member functions

(D) private member functions

Correct Answer: (A)

[EN] Static member functions belong to the class as a whole rather than any object instance, so they have no this pointer.

[HI] स्टैटिक मेंबर फंक्शन्स किसी ऑब्जेक्ट से नहीं बल्कि पूरी क्लास से संबंधित होते हैं, इसलिए उनमें this पॉइंटर नहीं होता।

Q7. When a member function is defined completely inside the class body, how does the compiler treat it?

(जब किसी मेंबर फंक्शन को सीधे क्लास बॉडी के अंदर परिभाषित किया जाता है, तो कंपाइलर उसे किस रूप में मानता है?)

(A) Implicitly friend

(B) Implicitly virtual

(C) Implicitly static

(D) Implicitly inline

Correct Answer: (D)

[EN] Functions defined inside a class definition are automatically marked as inline candidates by the C++ standard.

[HI] क्लास बॉडी के अंदर परिभाषित फंक्शन्स को C++ कंपाइलर स्वतः inline मानता है।

Q8. Which statement correctly describes memory allocation for a class with 2 int data members and 5 member functions across 10 objects?

(2 int डेटा मेंबर्स और 5 मेंबर फंक्शन्स वाली क्लास के 10 ऑब्जेक्ट्स बनाने पर मेमोरी आवंटन का सही वर्णन क्या है?)

(A) Only 1 copy of the 2 ints is shared by all 10 objects

(B) Objects share 2 ints while functions are duplicated 10 times

(C) Both data and functions are duplicated 10 times

(D) Each of the 10 objects gets its own 2 ints; the 5 functions share a single copy in code memory

Correct Answer: (D)

[EN] Non-static data members are allocated per instance, while member function code is shared across all objects.

[HI] प्रत्येक ऑब्जेक्ट को अपने 2 int मेंबर्स की अलग कॉपी मिलती है, जबकि सभी 5 मेथड्स का कोड मेमोरी में एक ही कॉपी के रूप में साझा रहता है।

Q9. What is the ONLY fundamental language difference between struct and class in C++?

(C++ में struct और class के बीच एकमात्र मूलभूत भाषाई अंतर क्या है?)

(A) struct does not support inheritance

(B) class cannot be allocated with new

(C) Default member and base-class access is public in struct and private in class

(D) struct cannot contain member functions

Correct Answer: (C)

[EN] In C++, struct and class have identical capabilities except default access visibility (public vs private).

[HI] C++ में struct और class की क्षमताएं समान हैं; केवल डिफ़ॉल्ट एक्सेस विजिबिलिटी (struct में public, class में private) अलग होती है।

Q10. Inside a non-const member function of class Car, what is the type of the this pointer?

(class Car के नॉन-कॉन्स्ट मेंबर फंक्शन के अंदर this पॉइंटर का डेटा टाइप क्या होता है?)

(A) Car* const

(B) const Car*

(C) Car&

(D) void*

Correct Answer: (A)

[EN] this cannot be reassigned to point to another object, so in a non-const method of Car its type is Car* const.

[HI] this को दूसरे ऑब्जेक्ट पर रीडायरेक्ट नहीं किया जा सकता, इसलिए नॉन-कॉन्स्ट मेथड में इसका टाइप Car* const होता है।

Q11. What happens if you declare static int count; inside class Item?

(यदि आप class Item के अंदर static int count; घोषित करते हैं, तो क्या होता है?)

(A) One shared copy of count exists for the entire class and must be defined/initialized outside (or inline since C++17)

(B) Every Item object stores its own private copy of count

(C) static members are forbidden inside C++ classes

(D) count becomes a constant equal to 0 that cannot change

Correct Answer: (A)

[EN] A static data member belongs to the class itself rather than individual objects, so all objects share that single variable.

[HI] स्टैटिक डेटा मेंबर पूरी क्लास का एक साझा वेरिएबल होता है जिसे सभी ऑब्जेक्ट्स साझा करते हैं।

Q12. Which line inside a member function enables method chaining like obj.setX(10).setY(20);?

(किसी मेंबर फंक्शन के अंदर कौन-सा रिटर्न स्टेटमेंट obj.setX(10).setY(20); जैसी मेथड चेनिंग संभव बनाता है?)

(A) return *this;

(B) return void;

(C) return this;

(D) return &this;

Correct Answer: (A)

[EN] Returning ClassName& via return *this; returns a reference to the invoking object so another method can be chained with '.'.

[HI] ClassName& रिटर्न टाइप के साथ return *this; लौटाने से वर्तमान ऑब्जेक्ट का रेफरेंस मिलता है, जिससे अगला मेथड डॉट से जुड़ सकता है।

Q13. How much memory does defining class Student { int roll; char grade; }; (without creating any objects) occupy at runtime?

(बिना कोई ऑब्जेक्ट बनाए केवल class Student { int roll; char grade; }; परिभाषित करने पर रनटाइम डेटा मेमोरी में कितनी जगह घिरती है?)

(A) 1 byte

(B) 5 bytes

(C) 8 bytes

(D) 0 bytes (a class definition is only a compile-time blueprint)

Correct Answer: (D)

[EN] A class declaration creates no object in memory; RAM is allocated only when an object instance is instantiated.

[HI] क्लास परिभाषा केवल कंपाइल-टाइम खाका है; ऑब्जेक्ट बनाने से पहले यह रनटाइम डेटा मेमोरी में 0 बाइट घेरती है।

Q14. In an array of 5 objects Rect arr[5];, how is the 3rd object's area() method invoked?

(5 ऑब्जेक्ट्स के ऐरे Rect arr[5]; में तीसरे ऑब्जेक्ट का area() मेथड कैसे कॉल किया जाता है?)

(A) arr[2].area()

(B) arr[3]->area()

(C) Rect::arr[2].area()

(D) arr.area[2]()

Correct Answer: (A)

[EN] Zero-based indexing puts the 3rd object at index 2, and arr[2] is a Rect value, accessed with the dot operator '.'.

[HI] 0-आधारित इंडेक्सिंग में तीसरा ऑब्जेक्ट arr[2] पर होता है और ऑब्जेक्ट वैल्यू के लिए डॉट ऑपरेटर arr[2].area() लगता है।

Q15. Why does *ptr.show() fail to compile when ptr is a pointer to an object?

(जब ptr किसी ऑब्जेक्ट का पॉइंटर हो, तो *ptr.show() लिखने पर कंपाइल एरर क्यों आता है?)

(A) Pointers cannot invoke member functions

(B) show() cannot return void with pointers

(C) The dot operator '.' has higher precedence than dereference '*', parsing as *(ptr.show())

(D) Dereference '*' is invalid in C++ classes

Correct Answer: (C)

[EN] Member access '.' binds tighter than '*', so parentheses are needed ((*ptr).show()) or better yet ptr->show().

[HI] डॉट ऑपरेटर '.' की प्राथमिकता '*' से अधिक होती है, जिससे यह *(ptr.show()) की तरह पढ़ा जाता है; सही रूप (*ptr).show() या ptr->show() है।

Q16. Which of the following statements about classes and objects in C++ is NOT true?

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

(A) Static data members are shared among all instances of a class

(B) Objects can be created both on the stack and dynamically on the heap

(C) A member function cannot access private data members of another object of the exact same class

(D) A class acts as a user-defined blueprint for creating objects

Correct Answer: (C)

[EN] Access control in C++ is per-class, NOT per-object: a method of class X CAN access private members of another X parameter passed to it (e.g. in copy constructors).

[HI] C++ में एक्सेस कंट्रोल ऑब्जेक्ट-आधारित नहीं बल्कि क्लास-आधारित होता है; इसलिए क्लास का मेथड उसी क्लास के दूसरे ऑब्जेक्ट के private मेंबर्स भी एक्सेस कर सकता है।

Q17. Within C++ Classes and Objects: Blueprint, Instances, Access Specifiers, which concept directly maps to: "sizeof(Empty) is at least 1 byte"?

(C++ Classes and Objects: Blueprint, Instances, Access Specifiers में कौन सा Concept सीधे "sizeof(Empty) is at least 1 byte" से संबंधित है?)

(A) Out-of-line method

(B) Default visibility

(C) class Empty {};

(D) this pointer

Correct Answer: (C)

[EN] In C++ Classes and Objects: Blueprint, Instances, Access Specifiers, 'class Empty {};' handles 'sizeof(Empty) is at least 1 byte' (Ensures unique addresses for distinct objects).

[HI] C++ Classes and Objects: Blueprint, Instances, Access Specifiers में 'class Empty {};' का कार्य/विवरण 'sizeof(Empty) is at least 1 byte' (Ensures unique addresses for distinct objects) है।

Q18. Which option accurately describes 'Default visibility' in C++ Classes and Objects: Blueprint, Instances, Access Specifiers?

(C++ Classes and Objects: Blueprint, Instances, Access Specifiers के संदर्भ में 'Default visibility' की सही विशेषता कौन सी है?)

(A) Hidden ClassName* const in methods

(B) private in class; public in struct

(C) ReturnType ClassName::method()

(D) sizeof(Empty) is at least 1 byte

Correct Answer: (B)

[EN] 'Default visibility' provides 'private in class; public in struct' — key point: Only syntactic default difference in C++.

[HI] 'Default visibility' का मुख्य विवरण 'private in class; public in struct' है (Only syntactic default difference in C++)।

Q19. In C++ Classes and Objects: Blueprint, Instances, Access Specifiers, identify the correct parameter for the specification: "ReturnType ClassName::method()".

(C++ Classes and Objects: Blueprint, Instances, Access Specifiers में "ReturnType ClassName::method()" विवरण किस विकल्प के लिए सही है?)

(A) class Empty {};

(B) Out-of-line method

(C) this pointer

(D) Default visibility

Correct Answer: (B)

[EN] 'Out-of-line method' corresponds to 'ReturnType ClassName::method()' (Uses :: scope resolution; not implicitly inline).

[HI] 'Out-of-line method' का संबंध 'ReturnType ClassName::method()' (Uses :: scope resolution; not implicitly inline) से है।

Q20. Considering C++ Classes and Objects: Blueprint, Instances, Access Specifiers, what key behavior defines 'this pointer'?

(C++ Classes and Objects: Blueprint, Instances, Access Specifiers में 'this pointer' की मुख्य भूमिका या मान क्या है?)

(A) Hidden ClassName* const in methods

(B) sizeof(Empty) is at least 1 byte

(C) private in class; public in struct

(D) ReturnType ClassName::method()

Correct Answer: (A)

[EN] 'this pointer' represents 'Hidden ClassName* const in methods' (Absent inside static member functions).

[HI] 'this pointer' का अर्थ/मान 'Hidden ClassName* const in methods' है (Absent inside static member functions)।

C++ Classes and Objects: Blueprint, Instances, Access Specifiers — Exam FAQs

Q1: Why is an empty class size 1 byte instead of 0 bytes in C++?

C++ में खाली क्लास का आकार 0 बाइट के बजाय 1 बाइट क्यों होता है?

[EN] If an empty class had size 0, elements of Empty arr[5]; would all share the exact same memory address, breaking pointer identity and pointer arithmetic.

[HI] यदि खाली क्लास 0 बाइट की होती, तो Empty arr[5]; के सभी तत्वों का मेमोरी एड्रेस एक ही हो जाता, जिससे ऑब्जेक्ट पहचान और पॉइंटर गणित टूट जाते।

Q2: Should I define member functions inside the class or outside with ::?

क्या मेंबर फंक्शन्स को क्लास के अंदर परिभाषित करना चाहिए या :: लगाकर बाहर?

[EN] Short one-line getters/setters are fine inside the class (implicitly inline), while longer logic is best separated into .cpp files using ClassName::method() for cleaner headers and faster builds.

[HI] छोटे गेटर्स/सेटर्स को क्लास के अंदर (इनलाइन) लिखना ठीक है, जबकि बड़े मेथड्स को हेडर साफ रखने और तेज कंपाइलेशन के लिए .cpp में ClassName::method() से लिखना बेहतर होता है।

Q3: Can a C++ class contain an object of its own type as a non-static data member?

क्या कोई C++ क्लास अपने ही टाइप का नॉन-स्टैटिक ऑब्जेक्ट अपने अंदर डेटा मेंबर के रूप में रख सकती है?

[EN] No, because that would require infinite recursive size; however, it CAN hold a pointer (Node* next) or reference to its own type, or a static member of its own type.

[HI] नहीं, क्योंकि इससे आकार अनंत हो जाएगा; हालांकि वह अपने ही टाइप का पॉइंटर (Node* next), रेफरेंस या static मेंबर अवश्य रख सकती है।

Related Posts

  • Loading related posts...

Post a Comment

Search Topic Notes & 20 MCQs (Hindi / English)