How do structures and classes in C++ differ?

Options

  • A. In structures, members are private by default whereas in classes, they are public by default
  • B. In structures, members are public by default whereas in classes, they are private by default
  • C. Structures cannot have private members whereas classes can have
  • D. More than one of the above
  • E. None of the above

Correct Answer (Detailed Explanation is Below)

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

Detailed Explanation

Answer: (B) In structures, members are public by default whereas in classes, they are private by default

The main difference between a struct and a class in C++ is their default access level.

In a struct, members are public by default. Therefore, they can normally be accessed directly from outside the structure.

In a class, members are private by default. Therefore, they cannot normally be accessed directly from outside the class.

Both structures and classes can contain data members, member functions, constructors, private members, public members, and other C++ features. The important difference asked here is the default access specifier.