Ive been using C++ for a while now, and all of a sudden, CLASSES!!! Literally every single example of a tutorial will be using classes after the tutorial on classes and its really confusing me. I know they are the same as structs, but what benefits do they present over structs? Also, constructors and destructors are confusing me a lot. Can anyone clear these up for me?
There really isn't a big difference between classes and structs, just the default access permissions when talking about them in a programming context. You could use the struct keyword to define all your classes.
There is a difference when people talk about classes vs. structs though. Generally a struct is considered POD (plain old data), or something close to it, so just a neat container that can hold multiple variables.
When people talk about classes, they generally mean an object which holds all of the variables it needs and provides functions which impact those variables. So something like a rectangle class might hold two integers for the width and height, and provide functions like "Area" which returns the area of the rectangle, or "Scale" which scales both height and width by a factor.
A constructor is just a sort of "default" function that is ALWAYS run whenever a new instance of the class is created. So for the rectangle class, it might instantiate height and width to default values.
A destructor is called when the class is destroyed, and is generally used to de-allocate memory and run other clean-up routines on the class.
What they said, but there's also access permissions like public: , private: , and protected: which will let you protect the base values (or important variables) from being inadvertently changed. This may not be too important if you just use a class like a struct, but if you start saving tons of information and/or you're working with a team of programmers the risk of making a mistake that messes up that unprotected information increases.
1. public: is where you store variables that you want to be easily changed, and functions that can access private variables.
2. private: is where you store variables that you only want functions from within your class to have access to them. The user would have to call a function from your class's public: area and you decide how that function interacts with your private variables.
3. protected: is not going to be important until you get into inheritance classes and virtual functions. Pretty much protected acts as private to any other classes that aren't inherited from that class and as public to those that are.
Basically classes start out as structs with attitude and as you get deeper into object oriented coding they can start doing some really cool stuff.