CIS-60 Home http://www.c-jump.com/CIS60/CIS60syllabus.htm
objects: regions of memory
variables: access to objects
C++ types: size + behavior
declarations: type + name
Consider
x = y + 2;
x and y declared:
double x; int y = 7; x = y + 2;
Type determines operations
double x; // declaration of x int y = 7; // initialization of y x = y + 2; // addition and assignment
// Example: void main() { double x; // declaration of x int y = 7; // initialization of y x = y + 2; // addition and assignment }
bool: true or false
char: characters
int: integer numbers
double: floating-point (real) numbers
Logical operations:
int a = 2; int b = 2; bool is_equal = ( a == b );
Here, double equal sign operator ( a == b ) is making a decision:
( a == b ) // Is a equal to b?
The result (true in our case) is stored in boolean variable named is_equal.
Boolean types can be converted to plain integer numbers and back:
void main() { int i = -1; bool b = i; // b will be true int x = b; // x will be set to 1 x = 0; b = x; // b will be set to false }
Note: no assumptions should be made regarding the size of bool type. On many systems it takes 8 bits to store a boolean variable.
C++ character is a single character of a character set:
char ch = 'a';
Typical character set:
8 bits in size
max 256 values
Uses
Typical character set includes:
' \' ' single quotes
'0', '1', '2', ... '9' digits
'A' to 'Z', 'a' to 'z' alphabetic
punctuation
control characters
'\n' newline
'\t' horizontal tab
#include <iostream> void main() { char c; // Instantiate character variable std::cin >> c; // Input character from the user std::cout << c; // Echo character back to the user std::cout << '='; // Display equal sign std::cout << int( c ); // Display character code std::cout << '\n'; // Display new line }
3 forms of integer numbers exist:
"plain" int
signed int
unsigned int
Integer numbers can have 3 sizes:
short int 16 bit
"plain" int 32 bit
long int 64 bit
|
|
Notations
decimal point:
7.33
0.0975
float type declares single-precision number
double type declares double-precision number
Example:
double pi = 3.14;
More info: floating-point precision
C++ sizes are multiples of char:
size of char is 1
sizeof ( bool )
sizeof ( int )
sizeof ( double )
All variables must be declared before they can be used in a program:
int counter; int month; int day; int year;
Note: C++ declarations end with a semicolon (;)
Declaration initializer is an optional part of the variable declaration.
Initializer provides initial value to our variables:
int counter = 1000; const double pi = 3.14;
Note: const transforms a variable into a constant value...
...once initialized, const variable can never change again.
Declaration initializer is the only way to set value of a const variable.
Name (identifier) consists of
letters ABC... abc...
digits 123...
underscores _
first character must be letter or underscore
distinct UPPERCASE and lowercase letters
no keywords: const, void, double, int, etc.
void main() { const char LF = '\n'; bool FLAG_BUSY; int social_security_number; double form1040a_field_33; bool FLAG_BUSY = false; // Error: variable already declared bool flag_busy = false; // OK, but extremely bad style double 1040a_total; // Syntax error: first character must // be a letter or underscore }
Declaration placement determines the scope
Scope establishes visibility of the variable
Scope limits the lifetime of the variable
|
|
|
|