Hi,
What's the difference between a class in C++ and a struct in C?
They seem to be very similar to each other. If they are so similar why can't OOP be used in C? Then what's the point of C++?
Hi,
What's the difference between a class in C++ and a struct in C?
They seem to be very similar to each other. If they are so similar why can't OOP be used in C? Then what's the point of C++?
It's not a flame bait. I'm new as a c programmer and I started to wonder when I started to read about structs yesterday. Some fast googling seems to show that there's no big difference (except some default thing about private and public).
Just continue with some "fast googling"
A C++ class is a C++ struct, the only functional difference being that default access of members is public for a struct and private for a class.
Access control is not the only difference between C++ struct and a C struct. Others include the option for the programmer of deriving one from another, having member functions, better control of how an instance is initialised (constructors), assigned (assignment operators), and destroyed (destructor). More advanced differences include each C++ struct type being associated with a distinct scope, and that C structs cannot have members which are C++ structs (such code cannot be compiled as C).
All of those features (I'm sure I've missed some) make various programming styles/approaches/techniques significantly easier in C++ and C. Including OOP.
Thank you.
When talking about the differences between a struct and a class in C++, the only real difference is the access mode. However since we are in the C forum there are many other differences that need to be taken into account. First and foremost, there is no "class" in C, this is a C++ construct. C++ allows, and encourages member functions in both classes and structures, C doesn't allow member functions.
Remember C and C++ are quite different languages they share the same base language, but what is considered a good practice in C is not necessarily a good practice in C++, and the reverse is also true.
Jim