Professional Documents
Culture Documents
الفصل الثاني للطباعة PDF
الفصل الثاني للطباعة PDF
1
9/20/2018
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
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
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
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
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
• 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
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
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
• 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
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
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
11
9/20/2018
• 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
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
• 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
14
9/20/2018
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
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
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
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
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
• 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
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