In C++, which statement about classes and structures is correct? Assume standard C++ (not C) access defaults and capabilities.

Difficulty: Easy

Correct Answer: class data members are private by default while that of structure are public by default.

Explanation:


Introduction:
C and C++ use the words “struct” and “class” differently. In modern C++, both can contain functions and data, but their default access differs. This question verifies that you know the precise, standard defaults and capabilities for C++ classes versus structures.


Given Data / Assumptions:

  • Language: C++ (not C).
  • We consider member access defaults and whether member functions are allowed.
  • We consider whether pointers to these types are allowed.


Concept / Approach:
In C++, class and struct are almost identical except for default access. In a class, members are private by default; in a struct, members are public by default. Both can have member functions and both support pointers and references. Hence, the only correct statement among the options is the one describing these default access rules.


Step-by-Step Solution:
1) Rule out A: structures can absolutely have member functions in C++.2) Rule out B: it reverses the defaults.3) Rule out C: pointers to classes/structs are valid and common.4) Select D: it correctly states the defaults—class: private, struct: public.


Verification / Alternative check:
Write minimal examples showing that omitting access specifiers in class makes members inaccessible (private), while in struct they are accessible (public) by default.


Why Other Options Are Wrong:
A: incorrect in C++—structs can have methods.B: flips the defaults.C: pointers are allowed to any object type.


Common Pitfalls:
Confusing C behavior (structs have no member functions) with C++ behavior, where structs are full-fledged classes with different defaults.


Final Answer:
class data members are private by default while that of structure are public by default.

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion