C++ Tutorial: Classes

Facebooktwittergoogle_plusredditpinterestlinkedinmail

Classes are how programmers group things. Some good examples for programming a game with classes would be players, AI, and items. In these classes, programmers can write functions that will be used for that entity. Take player class, for example, a programmer might write functions such as setHealth(), setPosition, getHealth(), getPosition(), etc. Functions such as these can be used to set and get the variables we need for the entity or object in our game. We’re going to do this very shortly.

 

 

Declaring a Class

The example shown below is the syntax for declaring a function.

class name_of_class{
access_specifier:
member;
} name_of_objects;

First, we give our class a name, then insert the access specifier. The access specifier allows programs to give access rights to members of that class. These access rights are private, public, or protected. Inside of the access specifiers, programmers can write members of the class, including variables and functions. Before I can explain what the access specifiers do, let’s write a program.

 

In this lesson, we will write a class with three members; geek, nerd, and bookworm; and we will place them each in separate access specifiers; public, private and protected.

 

 

Coding

Write, copy, paste, do whatever you want with this code:

 

When you run the code, your program will have a few errors. The reason why is some of the members don’t have access outside of their class. Private members can only be used inside their class. Protected members can only be used in a class derived from it. Public members can be used anywhere.

 

To make the program work, just comment out the lines that have no access. Once you have done this correctly, your program will run and not show anything, because we haven’t told the program to pause or print anything on the screen.

 

 

What Happened?

We created a class named Programming and gave it three members; geek, nerd, and bookworm. Then we place each on in three separate access specifiers; public, private, and protected.

 

 

Then we created a derived class called daDerivedClass, which is another class, but we made it into a derived class by adding “: public Programming”, giving the protected member; bookworm, access to this class. We then created a function called doStuff(), and all that does is stuff. I’m kidding, we set each variable equal to 7. The line that has no access will cause an error, so we comment that out.

 

 

Last, we use our programming class in our main function. If we’re not using the members in a derived class, we have to create an object for the class. It’s a lot like creating a variable; first, you type the name of the class, then you give the object a name. In this case, I’m naming it person, because the members are each individual person in the class. You can name it whatever you want, however.

After creating an object, we call out our member by typing the name of the object, followed by a dot, and type the name of the member. Private and Protected are commented out because they have no access.

 

Facebooktwittergoogle_plusredditpinterestlinkedinmail

About Justin

Computer programmer and owner of kauergames.com.
Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *