I've been looking over classes, but I just can't understand a few things.
1) Why would you make variable private and use functions to set and read them? Whats the benefit?
2) Since I don't know what the benefit of making variables private, whats the point of using a class versus a struct?

Any help would be appreciated.

1) You have more control over who accesses your data and how they access it. For example, let's say you have a telephone number in the class represented by a string.

If the variable is public, anyone can set it to anything. This means that your class can't assume the phone number to be valid at any given time and has to constantly validate it. It also means that if the number is invalid at a bad time, you have to figure out how to fail gracefully. This is messy and it makes your job harder.

Contrast that with if the variable is private and can only be accessed using your provided interface. You have a single location where the value is checked and errors are easy to throw. You also don't have to worry about the value elsewhere because the only way the it can be changed is through your interface, which guarantees that it's correct. It's cleaner, easier, and requires less effort on your part.

2) The only difference between a class and a struct is the default accessibility. If you have a class where the private members don't use or need the protection that private accessbility provides, you can use a struct and save yourself a bit of typing.

Yes, in time you will understand the magic of OOP (object oriented programming).

Also, as to the struct/class thing: While that is the technical difference between them, the reason they both exist is because structs are left-over functionality from C, and classes are the object-oriented, C++ alternative. Initially, structs (if I am correct), didn't allow methods, and perhaps other nice class things, like inheritance and access-level specifiers. When classes came around and got all of that, they added the functionality to structs.

Also, a teacher of mine once said that structs are merely templates (used in the English sense, not the C++ sense) for multi-type arrays.

>structs are left-over functionality from C
Structures are inherited from C, yes.

>classes are the object-oriented, C++ alternative
It's probably closer to say that Stroustrup prefers the class terminology and created something new to avoid breaking changes with the "C with classes" preprocessor. Lo and behold, that early decision gave C++ both classes and structures, for better or worse.

>a teacher of mine once said that structs are merely templates [...] for multi-type arrays
That's a rather dangerous explanation, because arrays support iteration while structures can have internal padding that would throw you into the realm of undefined behavior. I'd describe structures (in C) as templates for heterogeneous data records.

This article has been dead for over six months. Start a new discussion instead.