Chapter 13: Algorithms

This chapter concludes this book's examination of C++ as a language before progressing to more specialized game related topics. Specifically, this chapter attempts to connect the bridge between C++ as a language and its libraries, which are not an intrinsic part of the language but which themselves are used by C++ programs. Overall this chapter examines the following subjects:

  • Linked lists

  • Stacks

  • Heaps

13.1 Language and Content

Just as with human languages spoken around the globe, the rules of grammar define how valid statements may be written in any language. In C++, keywords such as if, class, struct, and so on define how C++ statements must be structured in order to be understood by a compiler. But the code a programmer writes cannot itself be said to be part of the C++ language any more than the stories a book tells can be said to be part of the English language, or any other language. In short, the instructions of a program might be written in C++, but the instructions themselves are not part of C++.

A programmer might write a class to print text on the screen. To do this, some functions might be created. The functions are written in C++ using the proper rules and valid syntax, but the functions are the creation of the programmer. In this way, a programmer can build up a whole series of independent classes, each used to achieve different tasks such as printing, sorting data, and so on. These classes can also be reused and recycled for any C++ program a programmer might write in the future. For example, the programmer can write a class to print documents. If he then decides to create a word processing program, a photo editing program, and a spreadsheet program, he could write a separate printing class for each one, but this is not the simplest solution. Instead, the programmer could make one general printing class, and then use this class with all the applications he develops. This saves time and money because the classes can be used time and time again.

A collection of classes like this is called a library, and typically programmers store and arrange their libraries in separate source files. These files can then be used in many programs. The code contained in files can be included into any other program using, as we have seen, the #include preprocessor directive at the top of a source file. This directive takes the code from a separate file and treats it as being an integral part of the program whose functions and classes can be used freely by any program that includes them. We have already discussed the inclusion of library files such as iostream and fstream, which are files of the library called the STL (Standard Template Library). But there are many more libraries available to achieve all kinds of tasks, some free and some not.

Introduction to Game Programming with C++
Introduction to Game Programming with C++ (Wordware Game Developers Library)
ISBN: 1598220322
EAN: 2147483647
Year: 2007
Pages: 225
Authors: Alan Thorn © 2008-2017.
If you may any questions please contact us: