Which of the following statements about C++ classes and structures is correct? Consider access control defaults and member capabilities.

Difficulty: Easy

Correct Answer: Structures can have functions as members.

Explanation:


Introduction:
C++ extends the C struct by allowing it to behave like a class. This question checks whether you know that both classes and structures in C++ can contain functions and what their access defaults are.


Given Data / Assumptions:

  • Language: C++ (not C).
  • We compare what members are permitted and their default access.
  • Protected access is available in classes.


Concept / Approach:
In C++, a struct is essentially a class with a different default access level. Both can have data members and member functions, both support private/protected/public sections, and both can be used with inheritance. Therefore, the true statement is that structures can have functions as members.


Step-by-Step Solution:
1) Examine A: false—classes can have protected data members.2) Examine B: true—structs can declare member functions in C++.3) Examine C: false—class members are private by default.4) Examine D: false—struct members are public by default.


Verification / Alternative check:
Write a simple struct with a member function and verify it compiles and works like a class method.


Why Other Options Are Wrong:
A: contradicts the language; protected is a core access specifier.C and D: both reverse the standard C++ defaults.


Common Pitfalls:
Confusing C and C++: in C, structs cannot have functions as members; in C++, they can. Also, forgetting the different defaults leads to access errors.


Final Answer:
Structures can have functions as members.

Discussion & Comments

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