Todayโs Outline
- C++ basic syntax
- Variable and Constant
- Operators
- Basic I/O
C++ basic syntax
Tokens in C++
- C++ compiler collects the characters of the program into
tokens
Tokens
are separated by space
Keywords
Keywords
are words reserved by the programming language
- Each
keyword
cannot be used for other purposes
Identifiers
Identifiers
give unique names to various objects in a program like the name of variables, functions, libraries, and namespace
Keywords
cannot be used as identifiers
- An
identifier
is composed of a sequence of letters, digits, and underscore
- e.g. myRecord, point3D, last_file
- An
identifier
must begin with either an underscore or a letter
Variable and Constant
Variable
- memory storage whose value can be changed during program execution
Constant
- memory storage whose value cannot be changed during execution
Every variable/constant has 5 attributes
Address
- location of data in memory storage
Value
- content in memory storage
Name
- identifier of the variable
Type
- C++ is a strictly typed language, variables/constants must belong to a data type
Scope
- it defines the region within a program where the variable/constant can be accessed, and also the conflict domain
Variable Declaration
- Variable/constants must be declared before use
C++ Predefined Data Types
- Numerical
int
short
long
- integer number
float
double
- real number
- Character
- Logic
- Other
int
- Typically, an
int
is stored in 4 bytes(32 bit)
- The most significant bit of an int data type is the sign bit
- 0: positive / 1: negative
- C++ uses
twoโs complement
to encode negative numbers
short, long, and unsigned
long
- 8 bytes
short
- 2 bytes
unsigned
- non-negative 4 bytes
- no sign bit, therefore 0 ~ 2
char
char
takes 1 byte representing up to 256 characters
string
float
sizeof
sizeof
can be used to find the number of bytes needed to store an object (variable or data type)
Type Conversion
Implicit
type conversion
Binary expression
: lower-ranked operand promoted to a higher-ranked operand
Assignment
: right operand is promoted/demoted to match the variable type on the left
Explicit
type conversion (type-casting)
- e.g.
double b = (double) a;
Constants
- Constants must be initialized during the declaration
Scope
- Scope of a variable/constant refers to the region of a program where the variable/constant is visible (can be accessed)
Scope using namespace
Operators
Assignment Operator, =
Increment & Decrement Operators
Precedence & Associativity of Operators
- Precedence - order of evaluation for different operators
- Associativity - order of evaluation for operators with the same precedence
Bitwise Operators
- Bitwise AND
&
- Bitwise OR
|
- Bitwise XOR
^
- Bitwise NOT
~
- Left shift
<<
- Right shift
>>
Basic I/O
cout: Output Operator <<
cout: Change the Width of Output
cout: Other Manipulators