Beginning C++ Through Game Programming, Second Edition

Slides:



Advertisements
Similar presentations
CS 6301 Lecture 2: First Program1. CS Topics of this lecture Introduce first program  Explore inputs and outputs of a program Arithmetic using.
Advertisements

 2003 Prentice Hall, Inc. All rights reserved. 1 Machine Languages, Assembly Languages, and High-level Languages Three types of computer languages 1.Machine.
Chapter 2: Basic Elements of C++
Chapter 2: Introduction to C++.
Chapter 2: Basic Elements of C++
Admin Office hours 2:45-3:15 today due to department meeting if you change addresses during the semester, please unsubscribe the old one from the.
Basic Elements of C++ Chapter 2.
Introduction to C++ Programming
Copyright 2003 Scott/Jones Publishing Brief Version of Starting Out with C++, 4th Edition Chapter 1 Introduction to Computers and Programming.
Guide to Programming with Python Chapter Two Basic data types, Variables, and Simple I/O: The Useless Trivia Program.
1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral.
Chapter 01: Introduction to Computer Programming
COMPUTER SCIENCE I C++ INTRODUCTION
CSCI 1730 January 17 th, 2012 © by Pearson Education, Inc. All Rights Reserved.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Data representation and Data Types Variables.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
Chapter 2: Using Data.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
Lecture 3: The parts of a C++ program Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Week 1 Algorithmization and Programming Languages.
C++ Programming: Basic Elements of C++.
COMPUTER PROGRAMMING. A Typical C++ Environment Phases of C++ Programs: 1- Edit 2- Preprocess 3- Compile 4- Link 5- Load 6- Execute Loader Primary Memory.
1 INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
CIS-165 C++ Programming I CIS-165 C++ Programming I Bergen Community College Prof. Faisal Aljamal.
INTRODUCTION Kingdom of Saudi Arabia Princess Nora bint Abdul Rahman University College of Computer Since and Information System CS240.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Chapter 2 Overview of C++. 2 Overview  2.1 Language Elements  2.2 Reserved Words & Identifiers  2.3 Data Types & Declarations  2.4 Input/Output 
Introduction to C++ Basic Elements of C++. C++ Programming: From Problem Analysis to Program Design, Fourth Edition2 The Basics of a C++ Program Function:
THE BASICS OF A C++ PROGRAM EDP 4 / MATH 23 TTH 5:45 – 7:15.
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Basic Elements of C++
C++ Programming Lecture 3 C++ Basics – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 2: Basic Elements of C++
CHAPTER 1: INTRODUCTION C++ Programming. CS 241 Course URL: Text Book: C++ How to Program, DETITEL & DEITEL, eighth Edition.
PROGRAM ESSENTIALS. TOKENS  SMALLEST UNITS OF A PROGRAM LANGUAGE  Special Symbols  Mathematical Operators  Punctuation  Word Symbols  Key Words.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
Chapter 3 – Variables and Arithmetic Operations. First Program – volume of a box /************************************************************/ /* Program.
CS201 Introduction to Sabancı University 1 Chapter 2 Writing and Understanding C++ l Writing programs in any language requires understanding.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 2: Basic Elements of C++
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
1 A more complex example Write a program that sums a sequence of integers and displays the result. Assume that the first integer read specifies the number.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
Bill Tucker Austin Community College COSC 1315
Chapter 1.2 Introduction to C++ Programming
Chapter 2: Basic Elements of C++
Chapter Topics The Basics of a C++ Program Data Types
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 1: Introduction to computers and C++ Programming
Introduction to C++ Programming
Chapter 2 Introduction to C++ Programming
Completing the Problem-Solving Process
Basic Elements of C++.
Beginning C++ Programming
Chapter 2: Basic Elements of C++
Basic Elements of C++ Chapter 2.
Chapter 2 Elementary Programming
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
Introduction to C++ Programming
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
C++ Programming Basics
Chapter 1 c++ structure C++ Input / Output
Presentation transcript:

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