I'd like to learn the differences between a struct and a class in C++. Please discuss the technical differences as well as reasons for choosing one or the other in OO design.

I'll start with an obvious difference:

If you don't specify public: or private:, members of a struct are public by default; members of a class are private by default.

I'm sure there are other differences to be found in the corners of the C++ specification.

ram-kasarla
Ram KasarlaDec 24, 2022, 01:54 AM

Replies

Answers

Loginand verify email to answer
0

The difference between class and struct is a difference between keywords, not between data types. This two

struct foo : foo_base { int x;};
class bar : bar_base { int x; };

both define a class type. The difference of the keywords in this context is the different default access:

foo::x is public and foo_base is inherited publicly
bar::x is private and bar_base is inherited privately
revanth-k
Revanth KDec 24, 2022, 02:34 AM

Replies