anotherlolwut

anotherlolwut t1_ixdatkw wrote

Start with a Struct. That's the most basic part.

A Struct is kind of like an array. It's a bundle of data all stored right next to each other in memory, but it doesn't all have to be the same kind of data. You can have a character array, an int, and a float all be part of a single Struct.

My Reddit info can be represented as a Struct (though you wouldn't do it unless you were working in plain C). I have a user name (character string), a number of posts (int) and an account age (double, representing years).

A Class is like a Struct, but it can perform functions. The functions are part of the data stored in that object. Classes have done other features that Structs don't, but basically, think of it as Struct with functions.

As a Reddit user, I can make a post. You could resident this as RedditUser.makePost(). The benefit of this over just using regular functions in C is that makePost() can know the data in RedditUser, so I don't always need to pass it things like my user name.

Now, Reddit has a bunch of users. Like a dozen or so. When someone logs in and wants to make a post, Reddit has to create an object for them to gather their profile info and allow them to do stuff. It might load user profile data from a big database and read it through a Class called RedditUser. The function that does that is a Constructor, and it is part of the RedditUser Class.

Basically, RedditUser knows how to accept some set of data and assign it to the properties of the Class (the character string for my user name, the int for the number of posts I've written, etc.).

A single Class can have lots of Constructors to be ready for any kind of incoming data, like if it gets to read the big Reddit database or if I'm making a post as an unregistered guest.

2