1 hour ago, Acosix said:
I have read in the Bjarne Stroustrup's(the inventor of C++) The C++ Programming Language(4th Edition) which also explains 2011 standard, called C++11 ISO standard.
A struct is basically meant for making data structures, whereas a class is meant for making user-defined data types(language-defined data types are int, bool, etc. ... in other words the IDE bolded font data types).
Looks like Green_Baron covered it, but since this is a common (and surprisingly persistent) misconception, I'll go ahead and offer some further reinforcement.
Here are a couple links to sources that I think are credible:
https://en.cppreference.com/w/cpp/language/class
The above is a reference page for class declarations that says the class and struct keywords are identical except for default member and base class access.
https://isocpp.org/wiki/faq/classes-and-objects#struct-vs-class
The above is an entry from a popular C++ FAQ that (according to the Wiki at least) includes material from Bjarne Stroustrup, Marshall Cline, and perhaps others. It states that default visibility is public for struct and private for class, but that the two keywords are otherwise functionally equivalent.
Keep in mind that if you find instances of people talking about using classes and structs differently (including perhaps in Stroustrup's book, based on what you've said), they're most likely just talking about convention, not language specification.
If the references and quotes I and others have provided aren't sufficient, here are a couple ways you can challenge your current understanding of things:
- Try to find a credible C++ reference or source that doesn't just describe how class and struct are often used conventionally, but specifies actual technical differences between them other than default visibility.
- Write a test program using classes that uses a variety of features such as explicit constructors and destructors, private and public data and methods, inheritance, and so on. Once it builds and runs successfully, do a find-and-replace and replace all instances of 'class' with 'struct' (you may have to fix some things, for example if you're using the class keyword with templates). After the find-and-replace, build and run again. Everything should work the same as it did before.
If you find a credible reference that says something different from what we've been saying here, or come up with a code example that shows that the two keywords aren't functionally equivalent, please post the info here, as it'd be interesting and informative to see. (Although I'm confident in what I've said here, I always leave open the possibility that I've overlooked something.)