1. Computing

Learn about C++ by programming a simple text game


2 of 5

Classes and Instances

To reiterate: A class is a definition of an object but it has to be instantiated to create an object. A house is a good analogy of an Object. The class definition is the architect's plan and only when an instance of it is built (ie the house is built from the plans) does the object exist. Inside the house there can be room objects and each room will contain other objects. Likewise in Star Empires the game object contains other objects.

More Architecture

Let's look at the Game class. Although it controls the game, it does it pretty indirectly through instances of these other classes:

display crt;
messageHandler m;
galaxy Galaxy;

These declare three member variables in the game class. When the game object is created then an instance of these is also created. There is crt which is an instance of a display class, m is an instance of a messageHandler class and Galaxy an instance of a galaxy class. C++ like C is case sensitive so the details about the class are in galaxy, while the instance of it is in Galaxy.

The display class has methods that control the output to the display. The messageHandler class manages all game messages, both storing them and using an instance of the display class to display them. The galaxy class holds all the starsystem instances and fleet instances and is where the bulk of the game's game play logic and functionality takes place.

The game object is instantiated as g in the main() function when the program runs. All of the objects contained in game are separate and know nothing of each other. However, some objects need to know about others to use them so we have to make them aware of those other objects. For instance both the messageHandler m and the galaxy instance Galaxy need to output to the screen so they have to know about the display instance crt.

What is a Function?

This is a block of code that is given a name that can be called from other places. If you have code that is used in many places, it saves space by putting it in a function. It has a name, a return type (the value that it returns when called) and optional parameters.

In Star Empires there are a number of functions which don't belong in a class and are global to all code in the se.cpp file. The function distance below takes an x and y int value and returns an int distance according to Pythagoras. distance = square root (X * X + Y * Y). It calls the built in function sqrt which is made available through the line

#include <cmath>

// returns distance as int. Values are always in the range 1-6
int distance(int x,int y) {
int distsqr= (x*x) + (y*y) ;
return (int)(sqrt((float)distsqr)+0.5) ;

The first line // is a comment which the compiler ignores.

int distance (int x,int y)

This is the function signature for the method distance. It describes what the function returns, what it is known as and what parameters it accepts.

int means the function returns an int. distance is the name of the function. (int x,int y) means this must be called with two int values. It assigns the two value to x and y - effectively it creates two int variables from the values passed in, then it does the calculation which calls the sqrt function. This returns a floating point number (by adding 0.5 it rounds up) then the (int) drops the fractional bit and returns an int.

On the next page: About Constructors

  1. About.com
  2. Computing
  3. C / C++ / C#
  4. Programming Games
  5. Classes and Object Instances

©2014 About.com. All rights reserved.