Tutorial 7 – Classes

So we’ve created some structs which are an easy way to organise data. But what about if we want to do something more interesting and not so lightweight e.g. Have an object which can perform some kind of function. This can be done with structs, but there’s normally a better way to do it; Enter Classes!
In one of your visual studio projects, go to the solution explorer window. Right click on the project and click “Add -> Class…”. Choose to add a new C++ class and call it “MyObject”. You should notice that 2 files get named and created from this the “.h” or header file, and the .cpp or code file. You should end up with something that looks like this:
The “header” file:

class MyObject

The “code” file:

#include "MyObject.h"



Header and Code Files

You might be asking yourself what header or code files actually are. In C++ a header file is just another file containing code, but in particular it contains the forward declarations of the functions and objects used in the code file. The code file therefore contains the implementation for these forward declarations.

In the header you can see that a class called “MyObject” is defined and that within this class there are 2 public functions defined “MyObject” and “~MyObject”. We’ll get onto what these mean in a bit. At the top of the code file you can see the line

#include "MyObject.h"

This means check the forward declarations given in the header file so that we can use/implement the objects and methods declared there.

The Code

So we know we have declared a class with 2 functions and that there is a code file ready to implement those functions, so let’s take a look at the syntax:

//This means that the following function belongs to the MyObject class

//This is the name of the function (in this case it's a special
//function called the constructor) and it takes no arguments

//The implementation of the function goes in between these brackets

The other function has exactly the same syntax i.e. it is a function belonging to MyObject that takes no parameters, the only difference is the ‘~’ character which means that this function is the destructor.

When you create an instance of this class the constructor is called and when the object is destroyed it’s destructor is called. Let’s have a look at this in action.

Change your “MyObject” code file to look like this

#include "MyObject.h"



Remember from tutorial 2 that you’ll have to use the ‘#include ‘ line in your header file in order to use the printf functions in your code file.
Now that you’ve done that, go back to your main function and make it look like this:

#include "MyObject.h"

int main()
    MyObject testObject;
    return 0;

This means; Create a new instance of a “MyObject” called “testObject”. When the main function exits testObject will be destroyed because it has fallen out of scope.

The best way to run this and see the output is to open up a command prompt window, navigate to where your executable is and run it. You should see the following output:

So classes take a bit more code to set up than structs but now that we’ve built one, we can do something more interesting with it…

  1. No comments yet.
  1. No trackbacks yet.

Tell me what you think!

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: