Classes Lesson

Pages: 123
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.

Thanks,
Tyler Petresky

tyler.is.number.one@gmail.com
Yeah, it was, heh. What part are you getting confused at?
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.
Okay, got any good examples of classes?

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).

A simple employee class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Employee {
    std::string GivenName,
               FamilyName;
    int Salary;
    unsigned int 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(unsigned int); // 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
    return this->GivenName + " " + this->FamilyName;
}

inline void workedDays(unsigned int 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.

EDIT: Fixed code tags.
Last edited on
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
Last edited on
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?
Sure, I don't care.
Cool, thanks
Thanks man :D
Are you stalking me or something!? It's actually quite creepy how you copy and paste all of my posts and then put your little website after it.
WOAH!@firedraco: your post isnt showing up and neither is the stalkers. weird.
Actually I deleted it when I refreshed the page and found that his post was deleted. I think twicker is deleting his posts right now. :D
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...
Wander wrote:
little creeper...

No, you don't want to call twicker like that...
no i was talking about dilliu or whatever his name was
Ah, lol... I got it now. Yeah, I noticed him spamming too.
Pages: 123