This tutorial is part of a series aimed at introducing C++ through programming of simple games. In Star Empires, this introduces you to some C++ concepts as classes and structs, references, as well as general programming concepts like loops and decision statements. This is not a complete description of all language features but tries to explain what you need in the context of the program.
It's recommended but not obligatory that you look at the Star Empires C tutorials. C++ is a superset (literally C with classes) and the same code that was used to create the C version was used to make the C++ one, although the differences are mainly the encapsulation of many C functions into C++ classes.
These three tutorials linked below are for the same game but programmed in C. You may find it handy to look at those first.
StarEmpires.cpp Source Code
View Star Empires Source Code (As a text file). Note this code is a work in progress to find and fix the bugs and will be updated periodically.
Overview of C++
C++ is a programming languge that was developed and launched in the early 1980s and is a general purpose language being used for everything from low level applications like operating system and games to higher level software like word processors and web servers.
Overview of Star Empires
This is a text game where you have to conquer a galaxy fighting against a simple AI computer player.
- Read More details about the game design.
Star Empires Architecture
In C++ programs,everything is kicked off from a main function (same as in C). Everything in the game is encapsulated in a game object g, which is created in main and the whole game is controlled from a simple loop that repeats until the game is over. But that's getting ahead of explaining what objects or loops are.
Starting With Objects
A class is a definition of what an object will be. It specifies what member variables (numbers or strings or instances of other classes) will be and what the methods (blocks of code similar to functions in C) can operate on. Let's look at a simple example in the game- the starsystem struct.
struct starsystem {
int x,y;
int numships;
int owner;
void addships(int numtoadd) ;
void removeships(int numtoremove) ;
};
A struct in C++ is the same as a class but everything is publicly accessible. In C a struct is a way of holding several variables together which is also true in C++. However a C++ struct can have code (methods) that act on the variables.
The first three lines declare four variables called x,y, numships and owner and these are of type int which is short for integer. It means they hold numbers in the range -2^32..2^32-1. We don't use the full range of values. X and Y go from 0 to 5, numships from 0 to 999 and owner is either 0 or 9.
The two lines void addships( and void removeships are declarations of two methods that will let us add or remove ships to or from a fleet, i.e. affect the value of numships in a controlled manner.
On the next page: Classes and Object Instances

