Chapter 2 intro to c++ Flashcards
parts of a c++ program messy
//comment # include (#preprocessor directive( using namespace std; (which namespace to use) int main() ( beginning of function called main) { (beginning of block for main) cout << "hello"; (output statement) return 0; (send 0 to operating system) } (end of block for main)
parts of a c++ program
//comment #beginning of preprocessor directive < > include file name in #include ( ) used when naming a function { } encloses a group of statements " " encloses a string of characters ; end of a programming statement
cout object
display output on computer screen
You use the stream insertion operator «_space;to send output to cout:
cout «_space;“Programming is fun!”;
cout object2
Can be used to send more than one item to cout: cout << "Hello " << "there!"; Or: cout << "Hello "; cout << "there!";
endl manipulator
cout “hello” «< “world”;
«< “hello \n”
cout «_space;“world”
the #include directive
Inserts the contents of another file into the program
This is a preprocessor directive, not part of C++ language
#include lines not seen by compiler
Do not place a semicolon at end of #include line
variable
a storage location in memory
Has a name and a type of data it can hold
Must be defined before it can be used
literal
a value that is written in to a program’s code
such as a string literal”hello world”
or simply an integer literal like 11
identifier
An identifier is a programmer-defined name for some part of a program: variables, functions, etc.
C++ keywords cannot be used as identifiers
identifier rules
variable names should be meaningful
first character must be alphabetic character or underscore_
after first character you may use both cases, numbers, or underscores
NO WHITE SPACE
upper and lowers cases are distinct from each other.
Integer data types
integers can hold just that, integers: positive and negative whole numbers.
variables of same type may be defined on same( separated by commas, or different lines
different types must be on different lines
integer literals
An integer literal is an integer value that is typed into a program’s code
itemsOrdered = 15;
15 is an integer literal
integer literals 2
Integer literals are stored in memory as ints by default
To store an integer constant in a long memory location, put ‘L’ at the end of the number: 1234L
Constants that begin with ‘0’ (zero) are base 8: 075
Constants that begin with ‘0x’ are base 16: 0x75A
char data type
Used to hold characters or very small integer values
Usually 1 byte of memory
Numeric value of character from the character set is stored in memory
char literals
must be enclosed in single quotation marks
‘A’
character string
A series of characters in consecutive memory locations:
“Hello”
Stored with the null terminator, \0, at the end
Comprised of the characters between the “ “
string class
Special data type supports working with strings #include Can define string variables in programs: string firstName, lastName; Can receive values with assignment operator: firstName = "George"; lastName = "Washington"; Can be displayed via cout cout << firstName << " " << lastName;
floating point data types
The floating-point data types are:floatdoublelong double
They can hold real numbers such as:
12.45 -3.8
Stored in a form similar to scientific notation
All floating-point numbers are signed
types of floating point
single precision: 4 bytes
double: 8 bytes
long double: 8 bytes
floating point literal
Can be represented in Fixed point (decimal) notation: 31.4159 0.0000625 E notation: 3.14159E1 6.25e-5 Are double by default Can be forced to be float (3.14159f) or long double (0.0000625L)
bool data type
boolean
represents value of true or false
bool variables are stored as small integers
false is represented by 0, true by 1
determining size of data type
The sizeof operator gives the size of any data type or variable: double amount; cout << "A double is stored in " << sizeof(double) << "bytes\n"; cout << "Variable amount is stored in " << sizeof(amount) << "bytes\n";
variable assignment
An assignment statement uses the = operator to store a value in a variable.
boxes = 15
this stores the value of 15 to the boxes variables
variable must be left side
variable initialization
To initialize a variable means to assign it a value when it is defined:
int boxes = 15
may initialize some, none or all variables