0% found this document useful (0 votes)
74 views22 pages

الفصل الثاني للطباعة PDF

Uploaded by

Ay at
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views22 pages

الفصل الثاني للطباعة PDF

Uploaded by

Ay at
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

9/20/2018

Chapter 2: Introduction to C++


Topics
2.1 The Parts of a C++ Program
Starting Out with C++ 2.2 The cout Object
Early Objects
2.3 The #include Directive
Eighth Edition
2.4 Standard and Prestandard C++
by Tony Gaddis, Judy Walters, 2.5 Variables, Literals, and the Assignment
and Godfrey Muganda Statement
2.6 Identifiers
2.7 Integer Data Types
2.8 Floating-Point Data Types
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-2

1
9/20/2018

Topics (continued) 2.1 The Parts of a C++ Program


2.9 The char Data Type
2.10 The C++ string Class // sample C++ program comment

2.11 The bool Data Type #include <iostream> preprocessor directive

2.12 Determining the Size of a Data Type using namespace std; which namespace to use

2.13 More on Variable Assignments and int main() beginning of function named main

Initialization { beginning of block for main

cout << "Hello, there!"; output statement


2.14 Scope
return 0; send 0 back to operating system

2.15 Arithmetic Operators } end of block for main

2.16 Comments
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-3 Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-4

2
9/20/2018

Special Characters
2.1 The Parts of a C++ Program
Statement Purpose
Character Name Description
// sample C++ program comment
// Double Slash Begins a comment
#include <iostream> preprocessor directive
using namespace std; which namespace to use # Pound Sign Begins preprocessor directive
int main() beginning of function named main < > Open, Close Brackets Encloses filename used in
{ beginning of block for main #include directive
cout << "Hello, there!"; output statement ( ) Open, Close Parentheses Used when naming function
return 0; send 0 back to the operating system
} end of block for main { } Open, Close Braces Encloses a group of statements
" " Open, Close Quote Marks Encloses string of characters

; Semicolon Ends a programming statement

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-5 Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-6

3
9/20/2018

Important Details 2.2 The cout Object

• C++ is case-sensitive. Uppercase and


lowercase characters are different • Displays information on computer screen
characters. ‘Main’ is not the same as • Use << to send information to cout
‘main’. cout << "Hello, there!";
• Can use << to send multiple items to cout
• Every { must have a corresponding }, and
cout << "Hello, " << "there!";
vice-versa.
Or
cout << "Hello, ";
cout << "there!";

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-7 Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-8

4
9/20/2018

Escape Sequences – More Control Over


Starting a New Line Output

• To get multiple lines of output on screen


- Use endl
cout << "Hello, there!" << endl;
- Use \n in an output string
cout << "Hello, there!\n";

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-9 Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-10

5
9/20/2018

2.3 The #include Directive 2.4 Standard and Prestandard C++

• Inserts the contents of another file into the Prestandard (Older-style) C++ programs
program • Use .h at end of header files
• Is a preprocessor directive #include <iostream.h>
– Not part of the C++ language • Do not use using namespace convention
– Not seen by compiler No ; goes
here • May not use return 0; at the end of function
• Example: main
#include <iostream> • May not compile with a standard C++ compiler

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-11 Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-12

6
9/20/2018

2.5 Variables, Literals, and the


Assignment Statement Variables
• Variable – If a new value is stored in the variable, it
– Has a name and a type of data it can hold replaces the previous value
variable – The previous value is overwritten and can no
data type name
char letter; longer be retrieved
int age;
– Is used to reference a location in memory where a
value can be stored age = 17; // age is 17
cout << age; // Displays 17
– Must be defined before it can be used
age = 18; // Now age is 18
– The value that is stored can be changed, i.e., it can cout << age; // Displays 18
“vary”

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-13 Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-14

7
9/20/2018

Assignment Statement Constants


• Uses the = operator Literal
• Has a single variable on the left side and a – Data item whose value does not change
value on the right side during program execution
• Copies the value on the right into the – Is also called a constant
variable on the left 'A' // character constant
item = 12; "Hello" // string literal
12 // integer constant
3.14 // floating-point constant

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-15 Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-16

8
9/20/2018

2.6 Identifiers Multi-word Variable Names

• Programmer-chosen names to represent parts of the • Descriptive variable names may include multiple words
program, such as variables • Two conventions to use in naming variables:
• Name should indicate the use of the identifier – Capitalize all but first letter of first word. Run words together:
quantityOnOrder
• Cannot use C++ key words as identifiers totalSales
• Must begin with alphabetic character or _, followed – Use the underscore _ character as a space:
by alphabetic, numeric, or _ . Alphabetic characters quantity_on_order
may be upper- or lowercase total_sales

• Use one convention consistently throughout program

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-17 Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-18

9
9/20/2018

Valid and Invalid Identifiers 2.7 Integer Data Types

IDENTIFIER VALID? REASON IF INVALID • Designed to hold whole (non-decimal)


totalSales Yes numbers
total_Sales Yes • Can be signed or unsigned
total.Sales
12 -6 +3
No Cannot contain period
• Available in different sizes (i.e., number of
4thQtrSales No Cannot begin with digit
bytes): short, int, and long
totalSale$ No Cannot contain $ • Size of short ≤ size of int ≤ size of long

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-19 Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-20

10
9/20/2018

Signed vs. Unsigned Integers Defining Variables

• Variables of the same type can be defined


• C++ allocates one bit for the sign of the
- In separate statements
number. The rest of the bits are for data.
int length;
• If your program will never need negative int width;
numbers, you can declare variables to be - In the same statement
unsigned. All bits in unsigned numbers int length,
are used for data. width;
• A variable is signed unless the unsigned • Variables of different types must be defined
keyword is used. in separate statements
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-21 Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-22

11
9/20/2018

Integral Constants 2.8 Floating-Point Data Types


• To store an integer constant in a long
memory location, put ‘L’ at the end of the • Designed to hold real numbers
number: 1234L 12.45 -3.8

• Constants that begin with ‘0’ (zero) are • Stored in a form similar to scientific notation
octal, or base 8: 075 • Numbers are all signed
• Constants that begin with ‘0x’ are • Available in different sizes (number of bytes):
float, double, and long double
hexadecimal, or base 16: 0x75A
• Size of float ≤ size of double
≤ size of long double
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-23 Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-24

12
9/20/2018

Floating-point Constants Assigning Floating-point Values to


Integer Variables
• Can be represented in
- Fixed point (decimal) notation:
If a floating-point value is assigned to an
31.4159 0.0000625
integer variable
- E notation:
– The fractional part will be truncated (i.e.,
3.14159E1 6.25e-5 “chopped off” and discarded)
• Are double by default – The value is not rounded
• Can be forced to be float 3.14159F or int rainfall = 3.88;
long double 0.0000625L cout << rainfall; // Displays 3

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-25 Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-26

13
9/20/2018

2.9 The char Data Type Character Literal

• Used to hold single characters or very small • A character literal is a single character
integer values
• Usually occupies 1 byte of memory • When referenced in a program, it is
enclosed in single quotation marks:
• A numeric code representing the character
is stored in memory cout << 'Y' << endl;
SOURCE CODE MEMORY

char letter = 'C'; letter


• The quotation marks are not part of the
literal, and are not displayed
67
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-27 Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-28

14
9/20/2018

String Literals A character or a string literal?


• A character literal is a single character,
• Can be stored as a series of characters in enclosed in single quotes:
consecutive memory locations 'C'
"Hello" • A string literal is a sequence of characters
• Stored with the null terminator, \0, enclosed in double quotes:
automatically placed at the end "Hello, there!"
H e l l o \0 • A single character in double quotes is a
string literal, not a character literal:
• Is comprised of characters between the " "
"C"

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-29 Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-30

15
9/20/2018

2.10 The C++ string Class


2.11 The bool Data Type

• Must #include <string> to create and


use string objects • Represents values that are true or false
• Can define string variables in programs • bool values are stored as integers
string name;
• false is represented by 0, true by 1
• Can assign values to string variables with the
assignment operator bool allDone = true; allDone finished

name = "George"; bool finished = false; 1 0

• Can display them with cout


cout << "My name is " << name;

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-31 Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-32

16
9/20/2018

2.13 More on Variable Assignments and


2.12 Determining the Size of a Data Type Initialization

The sizeof operator gives the size in


Assigning a value to a variable
number of bytes of any data type or variable
– Assigns a value to a previously created variable
double amount; – A single variable name must appear on left side
cout << "A float is stored in " of the = symbol
<< sizeof(float) << " bytes\n"; int size;
cout << "Variable amount is stored in " size = 5; // legal
5 = size; // not legal
<< sizeof(amount) << " bytes\n";

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-33 Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-34

17
9/20/2018

Variable Assignment vs. Initialization 2.14 Scope

• The scope of a variable is that part of the


Initializing a variable program where the variable may be used
– Gives an initial value to a variable at the time • A variable cannot be used before it is defined
it is created int num1 = 5;
– Can initialize some or all of the variables cout >> num1; // legal
being defined cout >> num2; // illegal
int num2 = 12;
int length = 12;
int width = 7, height = 5, area;

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-35 Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-36

18
9/20/2018

2.15 Arithmetic Operators Binary Arithmetic Operators

• Used for performing numeric calculations SYMBOL OPERATION EXAMPLE ans


+ addition ans = 7 + 3; 10
• C++ has unary, binary, and ternary
operators - subtraction ans = 7 - 3; 4

– unary (1 operand) -5 * multiplication ans = 7 * 3; 21


– binary (2 operands) 13 - 7 / division ans = 7 / 3; 2
– ternary (3 operands) exp1 ? exp2 : exp3 % modulus ans = 7 % 3; 1

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-37 Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-38

19
9/20/2018

/ Operator % Operator

• C++ division operator (/)performs integer • C++ modulus operator (%) computes the
division if both operands are integers
remainder resulting from integer division
cout << 13 / 5; // displays 2
cout << 2 / 4; // displays 0 cout << 9 % 2; // displays 1
• If either operand is floating-point, the result • % requires integers for both operands
is floating-point cout << 9 % 2.0; // error
cout << 13 / 5.0; // displays 2.6
cout << 2.0 / 4; // displays 0.5

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-39 Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-40

20
9/20/2018

2.16 Comments Single-Line Comments

• Are used to document parts of a program • Begin with // and continue to the end of line
• Are written for persons reading the source int length = 12; // length in inches
code of the program int width = 15; // width in inches
– Indicate the purpose of the program int area; // calculated area
– Describe the use of variables
// Calculate rectangle area
– Explain complex sections of code
area = length * width;
• Are ignored by the compiler

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-41 Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-42

21
9/20/2018

Chapter 2: Introduction to C++


Multi-Line Comments

• Begin with /* and end with */ Starting Out with C++


Early Objects
• Can span multiple lines Eighth Edition
/*----------------------------
Here's a multi-line comment
by Tony Gaddis, Judy Walters,
----------------------------*/ and Godfrey Muganda
• Can also be used as single-line
comments
int area; /* Calculated area */

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-43 Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

22

You might also like