Beginning C++ Through Game Programming, Second Edition by Michael Dawson
Types, Variables, and Standard I/O: Lost Fortune Chapter 1 Types, Variables, and Standard I/O: Lost Fortune
Objectives Display output in a console window Perform arithmetic computations Use variables to store, manipulate, and retrieve data Get user input Work with constants and enumerations
Using C++ for Games Fast—built for speed Flexible—supports multiple paradigms Well-supported—many APIs and libraries
Creating an Executable File
Creating an Executable File 1. Editor to write the C++ source code 2. Compiler to compile source into object file 3. Linker to link the object file with external files, creating executable
Dealing with Errors Compile errors—no object file is produced Link errors—reference to external file can’t be found Run-time errors—occur when the executable is run Crash program Logical error
A First C++ Program // Game Over // A first C++ program #include <iostream> int main() { std::cout << "Game Over!" << std::endl; return 0; }
Elements of First C++ Program Comments and whitespace—for humans Header files—iostream main() function—starting point Standard output—std::cout Returning a value—return 0;
std Namespace Namespace—like a an area code using directive using namespace std; Don't have to prefix any elements with std:: All elements in the std namespace become "local" using declarations using std::cout; Don't have to prefix specified elements with std:: Declare exactly which elements become "local"
Arithmetic Operators Like a calculator Rounding for floating point Integer division always yield integers 7 + 3 = 10 7 - 3 = 4 7 * 3 = 21 7 / 3 = 2 7.0 / 3.0 = 2.33333 7 % 3 = 1 7 + 3 * 5 = 22 (7 + 3) * 5 = 50
Variables Represent piece of memory Store, retrieve, and manipulate data Example—keep track of a player’s score
Fundamental Types Values are compiler-dependent
Declaring Variables Set aside memory int score; int health, bonus;
Assigning Values to Variables Assignment statement—expression stored in variable score = 0; Initializing variables—assign value when declared int score = 0; Initialize new variable whenever possible
Naming Variables: Rules An identifier can contain only numbers, letters, and underscores An identifier can’t start with a number An identifier can’t be a C++ keyword
Naming Variables: Guidelines Choose descriptive names Be consistent Follow the traditions of the language Keep the length in check Good names lead to self-documenting code
Using cout and cin Display variable values by sending variables to cout cout << score; Get user input and store values in variables with cin cin >> score;
Arithmetic Operations with Variables Altering the value of a variable score = score + 100; Combined assignment operators score += 100; Increment and decrement operators Prefix increment operator ++lives; Postfix increment operator lives++;
Combined Assignment Operators
Integer Wrap-Around score = 4294967295; ++score;
Constants An unchangeable value you name const int ALIEN_POINTS = 150; Make programs clearer Make changes easy
Enumerations A set of unsigned int constants, called enumerators enum difficulty {NOVICE, EASY, NORMAL, HARD, UNBEATABLE}; Enumerators increase by 1 in sequence By default, enumerators begin at 0 But they can assign literal integer values enum ship {FIGHTER = 25, BOMBER, CRUISER = 50, DESTROYER = 100};
Summary C++ is a fast, high-level language that is the game industry standard A C++ program is a series of C++ statements The standard library is a set of files that you can include in your program files to handle basic functions A function is a group of programming code that can do some work and return a value Every program must contain a main() function, which is the starting point of the program
Summary (cont.) iostream, which is part of the standard library, is a file that contains code to help with standard input and output The std namespace includes facilities from the standard library To access an element from the namespace, you need to prefix the element with std:: or employ using cout is an object, defined in the file iostream, that’s used to send data to the standard output stream (generally the computer screen) cin is an object, defined in the file iostream, that’s used to get data from the standard input stream (generally the keyboard)
Summary (cont.) C++ has built-in arithmetic operators, such as the familiar addition, subtraction, multiplication, and division operators, and even the unfamiliar modulus C++ defines fundamental types for Boolean, single-character, integer, and floating-point values The C++ standard library provides a type of object (string) for strings You can use typedef to create a new name for an existing type A constant is a name for an unchangeable value An enumeration is a sequence of unsigned int constants