Download presentation
Published byDora Freeman Modified over 9 years ago
1
Beginning C++ Through Game Programming, Second Edition
by Michael Dawson
2
Types, Variables, and Standard I/O: Lost Fortune
Chapter 1 Types, Variables, and Standard I/O: Lost Fortune
3
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
4
Using C++ for Games Fast—built for speed
Flexible—supports multiple paradigms Well-supported—many APIs and libraries
5
Creating an Executable File
6
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
7
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
8
A First C++ Program // Game Over // A first C++ program
#include <iostream> int main() { std::cout << "Game Over!" << std::endl; return 0; }
9
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;
10
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"
11
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 = 7 % 3 = 1 7 + 3 * 5 = 22 (7 + 3) * 5 = 50
12
Variables Represent piece of memory
Store, retrieve, and manipulate data Example—keep track of a player’s score
13
Fundamental Types Values are compiler-dependent
14
Declaring Variables Set aside memory int score; int health, bonus;
15
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
16
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
17
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
18
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;
19
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++;
20
Combined Assignment Operators
21
Integer Wrap-Around score = ; ++score;
22
Constants An unchangeable value you name const int ALIEN_POINTS = 150;
Make programs clearer Make changes easy
23
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};
24
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
25
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)
26
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
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.