C++ for Embedded · Interview question

What's the difference between struct and class in C++?

A strong answer

Exactly one thing: default access level. In a struct, members (and base classes) are public by default; in a class, they're private by default. Everything else, methods, constructors, inheritance, access specifiers, works identically; you can write a fully encapsulated struct or a wide-open class. The convention is to use struct for passive aggregates of public data (a POD like a config block or a register-map overlay) and class when you have invariants to protect behind a method interface. Choosing the right keyword signals intent to readers even though the compiler treats them almost the same.

What a weak answer sounds like

You know the answer. Do you know what gets you dinged?

Pro breaks down the answer most candidates actually give to this question — and the specific reason an interviewer marks it down. It’s the difference between sounding correct and sounding senior, on all 472 questions.

From the lesson

Classes & Encapsulation

Bundle data with the code that operates on it, and hide the internals behind a clean interface: the C struct-plus-functions pattern, made safe by the compiler.

More Classes & Encapsulation questions

Browse all 472 interview questions