Download presentation
Presentation is loading. Please wait.
1
Introduction to C++ Programming
Week 3 – BITE 1513 Computer Game Programming This template can be used as a starter file for presenting training materials in a group setting. Sections Right-click on a slide to add sections. Sections can help to organize your slides or facilitate collaboration between multiple authors. Notes Use the Notes section for delivery notes or to provide additional details for the audience. View these notes in Presentation View during your presentation. Keep in mind the font size (important for accessibility, visibility, videotaping, and online production) Coordinated colors Pay particular attention to the graphs, charts, and text boxes. Consider that attendees will print in black and white or grayscale. Run a test print to make sure your colors work when printed in pure black and white and grayscale. Graphics, tables, and graphs Keep it simple: If possible, use consistent, non-distracting styles and colors. Label all graphs and tables.
2
New Employee Orientation
Type, Variables and Standard I/O Working with std namespace Using Arithmetic Operators Declaring the variables Performing Arithmetic Operations with variables Working with constants Give a brief overview of the presentation. Describe the major focus of the presentation and why it is important. Introduce each of the major topics. To provide a road map for the audience, you can repeat this Overview slide throughout the presentation, highlighting the particular topic you will discuss next.
3
Introduction C++ This is another option for an Overview slides using transitions.
4
Using C++ for Games There are a variety of reasons why game programmers choose the language.
5
It’s fast. Well-written C++ programs can be blazingly fast. One of C++’s design goals is performance. And if you need to squeeze out even more performance from your programs, C++ allows you to use assembly language—the lowest-level human-readable programming language—to communicate directly with the computer’s hardware.
6
C++ is a multi-paradigm language that supports different styles of programming, including object-oriented programming. Unlike some other modern languages, though, C++ doesn’t force one particular style on a programmer. It’s flexible
7
It’s well supported Because it is the dominant game programming language, there’s a large pool of assets available to the C++ game programmer, including graphics APIs and 2D, 3D, physics, and sound engines—all of which allow a programmer to leverage previous work to greatly speed up the process of writing a new game.
8
# include <header file> … void main( ) { variable declarations
// comments # include <header file> … void main( ) { variable declarations …. function prototype statements } Include library files Main program block
11
Explanations
12
This is a comment
13
It tells the program to call the specific library in order to display the output and making the program work perfectly Including Other Files The next line in the program is a preprocessor directive. You know this because the line begins with the # symbol. #include <iostream> The preprocessor runs before the compiler does its thing and substitutes text based on various directives. In this case, the line involves the #include directive, which tells the preprocessor to include the contents of another file. iostream, is part of the standard library, because it contains code to help us display output (<) and greater than (>) characters to tell the compiler to find the file where it keeps all the files that came with the Compiler. It called a header file.
14
All symbols in that namespace will become visible without adding the namespace prefix. A symbol may be for instance a function, class or a variable. When you make a call to using namespace <some_namespace>; all symbols in that namespace will become visible without adding the namespace prefix. A symbol may be for instance a function, class or a variable. E.g. if you add using namespace std; you can write just cout instead of std::cout when calling the operator cout defined in the namespace std. This is somewhat dangerous because namespaces are meant to be used to avoid name collisions and by writing using namespace you spare some code, but loose this advantage. A better alternative is to use just specific symbols thus making them visible without the namespace prefix. Eg:
15
E.g. if you add using namespace std; you can write just cout instead of std::cout when calling the operator cout defined in the namespace std. When you make a call to using namespace <some_namespace>; all symbols in that namespace will become visible without adding the namespace prefix. A symbol may be for instance a function, class or a variable. E.g. if you add using namespace std; you can write just cout instead of std::cout when calling the operator cout defined in the namespace std. This is somewhat dangerous because namespaces are meant to be used to avoid name collisions and by writing using namespace you spare some code, but loose this advantage. A better alternative is to use just specific symbols thus making them visible without the namespace prefix. Eg:
16
The next line marks the beginning of the function. {
And the very last line of the program marks the end of the function. } The next line marks the beginning of the function. { And the very last line of the program marks the end of the function. } All functions are delimited by a pair of curly braces, and everything between them is part of the function. Code between two curly braces is called a block and is usually indented to show that it forms a unit. The block of code that makes up an entire function is called the body of the function.
17
meaning it’s literally the characters between the quotes.
“Hello World!” is a string—a series of printable characters. Technically, it’s a string literal, meaning it’s literally the characters between the quotes. Displaying Text through the Standard Output The first line in the body of main() displays Game Over!, followed by a newline, in the console window. std::cout << “Game Over!” << std::endl; “Game Over!” is a string—a series of printable characters. Technically, it’s a string literal, meaning it’s literally the characters between the quotes. cout is an object, defined in the file iostream, that’s used to send data to the standard output stream. In most programs (including this one), the standard output stream simply means the console window on the computer screen.
18
We use the output operator (<<) to send the string to cout
We use the output operator (<<) to send the string to cout. We use std to prefix cout to tell the compiler that I mean cout from the standard library. Displaying Text through the Standard Output We use the output operator (<<) to send the string to cout. We use std to prefix cout to tell the compiler that I mean cout from the standard library. Std is a namespace. You can think of a namespace as an area code—it identifies the group to which something belongs. You prefix a namespace using the scope resolution operator (::). We send std::endl to the standard output. endl is defined in iostream and is also an object in the std namespace. Sending endl to the standard output acts like pressing the Enter key in the console window
19
The first line of the function ends with a semicolon (;)
The first line of the function ends with a semicolon (;). That’s because the line is a statement—the basic unit controlling the execution flow.
20
Returning a Value from main()
The last statement in the function returns 0 to the operating system. return 0;
22
Working with the std Namespace
Because it’s so common to use elements from the std namespace, two different methods for directly accessing these elements. This will save you the effort of using the std:: prefix all the time, plus it will make your code a bit cleaner. // Game Over 2.0 // Demonstrates a using directive #include <iostream> using namespace std; int main() { cout << “Game Over!” << endl; return 0; }
23
Working with the std namespace
// Game Over 3.0 // Demonstrates using declarations #include <iostream> using std::cout; using std::endl; int main() { cout << “Game Over!” << endl; return 0; }
24
Data Types Data Type C++ Keyword Bits Range integer int 16
to 32767 long integer long 32 to short integer short 8 -128 to 127 insigned int unsigned 0 to 65535 character char 0 to 225 floating point float approximately 6 digits double floating point double 64 approximately 12 digits
25
Using Arithmetic Operators
Meaning - Subtraction (also unary minus) + Addition * Multiplication ? Divison % Modulus divison -- Decrement ++ Increment
26
Arithmetic Operators Example
More Examples:
27
Declaring Variables All variables in a program must be declared prior to their use. The declaration takes the general form variable_type variable-list; Examples int a, b , c: short small_number; long big-number; char ch; double amout, rate
28
Initializing Variables
int m, n = 10; double rate, total = 0.0; char response = ‘n’; char colour[6] = “green”;
29
Logical Operators Operator Meaning && Logical AND || Logical OR !
Logical NOT x y X || y X && y True False
30
Escape Sequences Code Meaning \a Audible bell \t Horizontal tab \b
Backspace \\ Backslash \f Form feed \’ Single quote \n New line \” Double quote \r Carriage return \0 Null ASCII 0
31
Input/Output
32
Input/Output
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.