How do structures and classes in C++ differ?

20232023

How do structures and classes in C++ differ?

  1. A.

    In structures, members are private by default whereas in classes, they are public by default

  2. B.

    In structures, members are public by default whereas in classes, they are private by default

  3. C.

    Structures cannot have private members whereas classes can have

  4. D.

    More than one of the above

  5. E.

    None of the above

Attempted by 150 students.

Show answer & explanation

Correct answer: B

In C++, the main difference between structures and classes lies in their default access specifiers.

By default, members of a structure are public, while members of a class are private.

This means that when you define a structure, any data members or member functions are accessible from outside the structure unless explicitly declared as private.

In contrast, in a class, all members are private by default, and you must explicitly use the public keyword to make them accessible outside the class.

Additionally, both structures and classes can have private, public, and protected members when explicitly specified.

Therefore, the key difference is the default access level, not the ability to have private members.

हिन्दी उत्तर:

C++ में संरचना और क्लास के बीच मुख्य अंतर उनके डिफ़ॉल्ट एक्सेस स्पेसिफायर में होता है।

डिफ़ॉल्ट रूप से, संरचना के सदस्य सार्वजनिक होते हैं, जबकि क्लास के सदस्य निजी होते हैं।

इसका अर्थ है कि जब आप एक संरचना परिभाषित करते हैं, तो कोई भी डेटा सदस्य या सदस्य फ़ंक्शन बाहर से उपलब्ध होता है जब तक कि इसे निजी रूप से स्पष्ट रूप से घोषित न किया जाए।

इसके विपरीत, एक क्लास में सभी सदस्य डिफ़ॉल्ट रूप से निजी होते हैं, और आपको बाहर से उन्हें उपलब्ध बनाने के लिए सार्वजनिक शब्द का उपयोग करना होता है।

इसके अलावा, निजी, सार्वजनिक और संरक्षित सदस्य दोनों संरचना और क्लास में स्पष्ट रूप से निर्दिष्ट किए जा सकते हैं।

इसलिए, मुख्य अंतर डिफ़ॉल्ट एक्सेस स्तर में है, न कि निजी सदस्यों की क्षमता में।

Explore the full course: Uppsc Polytechnic Lecturer 2025 Cs