I've been looking at the tutorials and documentation on this forum. I understand most everything until I got to classes. Whoever wrote the lesson on classes wrote it in a way that I couldn't easily grasp. (I know its probably the same person that wrote the rest, but I guess I just don't get classes at all.) So, I'm trying to find a different line of tutorials that dumbs down the topic of classes a bit so I can understand it. :) Any suggestions would be appreciated.
I just don't understand the whole concept. haha. What they used for. How to use them. These member functions to create. How to use member functions that you create. Correct formatting for a class. Etc. If you plan on helping me. Please shoot me an email. :) I don't wanna stick to this forum all night long haha.
Thanks
If you understood what a struct is, classes are basically the same except you can also have methods (member functions). In fact, in C++, a struct is just another to write class except that the default privacy level is "public."
You use them to represent basic "things." For example, you could have a class representing an employee in your business or a character in a game. Many things in the standard library, including strings, cin/cout (i/ostreams), vectors, maps, etc, are all classes.
You call member functions by putting the object name, a . (period), then the function name exactly like you would a normal function. Say for example you have an employee class instance called myEmployee with a method to get their name called getName(). You would call it like so: myEmployee.getName();
I'm not sure what you mean by "correct formatting."
I think seeing examples of classes in use would help you to understand better.
The way someone first taught me what a class was was by using an RTS game as an example. They said that each unit type in an RTS had its own class for the stats and such. I think thats what confused me. After they said that, I can't stop trying to compare unit stats with things in a normal class like string and cin/cout.
How do you create a member function?
By correct formatting, I mean like indentations and what comes first, what comes after what, etc.
Indentation is usually done like with a function; things inside a class are indented once, with braces' positioning/indentation varying based on particular styles.
Here are some examples (note that they are incomplete).
class Employee {
std::string GivenName,
FamilyName;
int Salary;
unsignedint DaysWorked;
// etc.
public:
// A couple methods
// A member function that gets the full name
std::string getName() const; // The const means it doesn't change the class
// Method to increment the DaysWorked variable
void workedDays(unsignedint); // Not const since it changes the class
};
inline std::string Employe::getName() const {
// All non-static methods have a this pointer to the class they are called with
returnthis->GivenName + " " + this->FamilyName;
}
inlinevoid workedDays(unsignedint Number) {
this->DaysWorked += Number;
}
A simple string class:
1 2 3 4 5 6 7
class String {
// The internal array data we are wrapping
char* data;
public:
void append(char* AppendMe);
// et cetera
};
There is obviously a lot of extra things that could be added to both of these classes, but hopefully they help you see sort of what they can do.
Okay thank you. One question though. What is this: this->
Must've missed that tutorial haha.
EDIT: OH WAIT! Does that have to do with pointers? Thats the other thing I don't get along with the throw/catch thing. I haven't gone to school for this stuff yet. I'm running off of tutorials and what I taught myself. haha
this is a pointer pointing to the currently referenced object. So basically, if you had an employee that had GivenName == "John" and FamilyName == "Doe", then calling getName() would give you the string "John Doe", since this->GivenName would be "John", likewise for FamilyName. Hopefully that's understandable...it's kind of hard to explain well. XD
Yeah I understand better now. :D I'm gonna bookmark this page. Thanks a lot! You've been a huge help! Do you mind staying subscribed to this page in case I have any more questions on this topic?
oh haha. thank god. hes commented on two of my threads and just copy and pasted my words and then stuff his little website at the end. little creeper...