Using SimpleObject Class

From TRCCompSci - AQA Computer Science
Jump to: navigation, search

Now we have a class for a simple object (SimpleObj) we can use it. This will create a class for your game, and it will create objects of the SimpleObj class.

Game.h

Remember a class in C++ needs a header to declare the variables and to specify the methods it contains. Remember it provides no implementation for the methods:

#pragma once

class Game
{
public:
	Game(); //standard constructor
	~Game(); //standard destructor

		//list the functions we want to have (called methods in C++)	
	void Update();
};

Game.cpp

The Game.cpp file provides the implementation of the methods within the header file. The includes are need to be able to access the header files.

#include "Game.h"
#include "SimpleObj.h"

Game::Game() {}; // just need to exist at the moment
Game::~Game() {};

void Game::Update()
{

	SimpleObj Bobby1; // create a Simple Object withe the class name Bobby1
	SimpleObj Bobby2;
	
	Bobby1.m_MyName = "Bobby1"; // Give Bobby 1 his name
	Bobby2.m_MyName = "Bobby2"; // Give Bobby 2 his name
        // now we will do a loop;
	for (int i = 0; i < 200; i++)
	{
		Bobby1.Update(); // do Bobby1's update
		Bobby2.Update(); // do Bobby2's update
	}
	return; 
};

This code above mainly defines the Update method of the game class, in this case it will create 2 SimpleObj, assign each a name, and then run the update method for the objects 200 times.

Usage

This declares and defines what a game is, we now need to create an object of it in the 'main.cpp' code:

#include "Game.h"


int main(int argc, char *argv[])
{
	Game TheGame; // create an instance of game.

	for (int i = 0; i < 10; i++)
	{
		TheGame.Update();
	}

}