Which statement is correct regarding the following C++ declarations?

Options

  • A. A struct cannot contain a member function
  • B. A class cannot contain public data members
  • C. Both struct and class can contain public and private members
  • D. More than one of the above
  • E. None of the above

Correct Answer (Detailed Explanation is Below)

C. Both struct and class can contain public and private members

Detailed Explanation

Answer: (C) Both struct and class can contain public and private members

Both struct and class support access specifiers such as public, private, and protected.

The important difference is only their default access level.

For a struct, members are public by default. For a class, members are private by default.

However, a class can explicitly declare public members, and a struct can explicitly declare private or protected members.

Therefore, it is incorrect to say that classes cannot have public data members or that structures cannot have private members.