Lecutre_1
.pdfEscape sequence
\n
\t
\r
\a
\\
\'
\''
Escape sequences
Description
Newline. Position the screen cursor to the beginning of the next line.
Horizontal tab. Move the screen cursor to the next tab stop.
Carriage return. Position the screen cursor to the beginning of the current line; do not advance to the next line.
Alert. Sound the system bell.
Backslash. Used to print a backslash character.
Single quote. Use to print a single quote character.
Double quote. Used to print a double quote character.
1 2 3 4 5 6
7
BODY
// Fig. 1.4: fig01_04.cpp |
|
Comments |
|
||
// Printing a line with multiple statements. |
||
#include <iostream> Preprocessor directive |
|
// function main begins program execution int main()
{
std::cout << "Welcome "; std::cout << "to C++!\n";
return 0; // indicate that program ended successfully
} // end function main
Welcome to C++!
1 |
// Fig. 1.5: fig01_05.cpp |
|
|
2 |
// Printing multiple lines with a single statement |
||
3 |
#include <iostream> |
|
|
4 |
|
|
|
5 |
// function main begins program execution |
|
|
Using newline |
|||
6 |
int main() |
||
7 |
{ |
characters to print |
8 std::cout << "Welcome\nto\n\nC++!\n"; on multiple lines.
9
10 |
return 0; // indicate that program ended successfully |
11
12 } // end function main
Welcome to
C++!
Adding Integers
Output:
Standard input stream object:
std::cin
-obtain values typed at the keyboard
-stream extraction operator (>>)
Example:
int number1; int number2;
cin>>number1>>number2;
Variables
-Location in memory where value can be stored
-Common data types
int – integer numbers (0, 7, 876, 1998) char – characters ('a', 'G', '#')
double – floating point numbers (0.2, 3.667, 8.65) Declare variables with name and data type before use
int integer1; int integer2; int sum;
Can declare several variables of same type in one declaration Comma-separated list
int integer1, integer2, sum;
Variables names
Valid identifier
-Series of characters (letters, digits, underscores) that begin with a letter or underscore
-Neither spaces nor punctuation marks or symbols can be part of an identifier
-Cannot match any keyword of the C++
Example:
char abc, _abc, abc3, a3bc, a_3b_c;
-Cannot begin with digit
-Case sensitive
Initialization of variables
When declaring a regular local variable, its value is by default undetermined.
There are two ways to initialize variables in C++:
1.c-like initialization: type identifier = initial_value ;
Example:
int a = 0;
2.constructor initialization: type identifier (initial_value);
Example:
int a(0);
Both ways of initializing variables are valid and equivalent in C++
6
Memory Concepts
-Variable names correspond to locations in the computer's memory.
-Every variable has a name, a type, a size and a value
-When new value placed into variable, overwrites previous value
-Reading variables from memory nondestructive
-Placing a new value into a memory location is said to be destructive