Comp Programming Flashcards
The object ‘cout’ in C++ refers to what?
Console output, the simplest way to display information on screen.
The stream insertion operator is
«, useful with objects such as ‘cout’, where the object immediately to the right of the operator is sent to ‘cout’ or the screen to display.
Example: cout «_space;“What it do, boo?”;
Information or strings are sent to cout in a continuous stream. Example, you’d think that
cout «_space;“Order Includes”;
cout «_space;“Burger”;
cout «_space;“Fries”;
would be formatted in a clunky list. But if you ran this, it would appear like
“Order IncludesBurgerFries”
To format properly or introduce multiple lines of information, you can break a cout stream up by using endl (End Line, it’s a lowercase l) at the end of a cout statement.
Example:
cout «_space;“Order Includes:” «_space;endl;
cout «_space;“Burger” «_space;endl;
cout «_space;“Fries” «_space;endl;
Another way to cause cout to go to a new line is to insert an escape sequence into the string itself.
We’ve already encountered «_space;endl; to advance a stream to the beginning of a new line.
The alternative is using \n
An escape sequence STARTS with the backslash character and is followed by one or more control characters. This allows you to control the way the output is displayed by embedding commands directly within the string itself.
An example:
cout «_space;“This is a sample list: \n”;
cout «_space;“Item 1 \n:”;
cout «_space;“Item 2 \n”;
return 0;
This would print:
This is a sample list:
Item 1
Item 2
As stated earlier, the letter following the backslash dictates what escape sequence is embedded in the string of text.
\n is called the “newline escape sequence”. Where-ever this appears is immediately advanced to the next line. It operates virtually the same as ‘endl’.
Here are other forms of escape sequences:
\n
Newline
Causes the cursor to go to the next line for subsequent printing.
\t
Horizontal tab
Causes the cursor to skip over to the next tab stop.
\a
Alarm
Causes the computer to beep.
\b
Backspace
Causes the cursor to back up, or move left one position.
\r
Return
Causes the cursor to go to the beginning of the current line, not the next line.
Backslash
Causes a backslash to be printed.
'
Single quote
Causes a single quotation mark to be printed.
"
Double quote
Causes a double quotation mark to be printed.
DO NOT put a space between an escape sequence and its character.
The #include directive
include <iostream></iostream>
The #include directive causes the contents of another file to be inserted into the program.
Therefore, using a directive
is telling the program you’re writing to include the iostream file/library which includes other objects, code, and such to be called.
The header file ‘iostream’ must be included in any program that uses the cout object. This is because ‘cout’ is not part of the “core” C++ language. It is part of the input-output stream library.
The header file, iostream, contains information describe iostream objects. Without it, the compiler will not know how to properly compile a program that uses cout.
This is a preprocessor directive, and these are not C++ statements. They are commands to the preprocessor which runs prior to the compile to set up programs in a way that makes life easier for the programmer.
For example, any program that uses cout object must contain the extensive setup information found in iostream. You -could- write this yourself, or you could copy and paste all the code from iostream into your file/program. However, simply linking the file at the beginning and uses the already defined code and its variables is much easier.
Literals
20
Integer literal
“Today we sold”
String literal
“bushels of apples.\n”
String literal
0
Integer literal
Literals are also called constants. Literals are things you will likely see, such as cout «_space;“The value is “ «_space;“number” «_space;endl;
The bits in quotation marks are literals, even though “number” represents an integer, it is technically a literal returning an integer expression.
In terms of naming variables, they should always be lowerecased, not overlapping with already predefined variables or words in C++ code, such as “int” or “return” or “if” or “namespace” (you get the idea) and if they are two or more nouns/words placed together, to improve readability, you capitalize the second words first letter such as int itemsOrdered;.
Variable names CAN NOT begin with a digit.
Variable names CAN NOT contain characters outside of letters, digits, or underscores.
Using underscores to separate multiple words or phrases in a variable name is also universally accepted.
Variables are classified according to their data type, which specifies what information may be stored within them.
There are two (in a broad sense) “main” data types”: Numeric and Character
Numeric data types deal with numbers and have two additional subtypes: integer and floating point
Integer uses whole numbers like 9, 1, 919, -34
Floating point numbers have a decimal point. Additionally, integer and floating point data types ALSO have subtypes.
Your primary considerations for selecting a numeric data type are:
The largest and smallest numbers that may be stored in the variable
How much memory the variable uses
Whether the variable stores signed or unsigned numbers
The number of decimal places of precision the variable has
The size of a variable is the number of bytes of memory it uses. Typically, the larger a variable is, the greater the range it can hold.
Data Types:
short int
2 bytes
−32,768 to +32,767
unsigned short int
2 bytes
0 to +65,535
int
4 bytes
−2,147,483,648 to +2,147,483,647
unsigned int
4 bytes
0 to 4,294,967,295
long int
4 bytes
−2,147,483,648 to +2,147,483,647
unsigned long int
4 bytes
0 to 4,294,967,295
long long int
8 bytes
−9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
unsigned long long int
8 bytes
0 to 18,446,744,073,709,551,615
short int can be abbreviated as short
unsigned short int can be abbreviated as unsigned short
unsigned int can be abbreviated as unsigned
long int can be abbreviated as long
unsigned long int can be abbreviated as unsigned long
long long int can be abbreviated as long long
unsigned long long int can be abbreviated as unsigned long long
char letter;
char data types can hold only one character a time. You can define char variables by using
char letter;
Where char is the type of variable, and letter is the name of the variable char.
letter = ‘g’;
Notice it is in single quotations and not double. Character variables use single quotations while string literals are enclosed in double quotation marks.
Although the char data type is using for storing characters, it is actually an integer data type that typically uses 1 byte of memory. On some systems it may be larger than 1 byte.
The reason for this is because the characters are stored as integers internally. Each printable character as well as unprintable characters are assigned a unique number.
Externally it prints the character when asked to.
It is important to not confuse string literals with character literals.
Strings are a series of characters stored in consecutive memory locations that can be virtually any length. This means there must be some way for the program to know how long a string is.
In C++, an extra byte of data is appended to the end of string literals when they are stored in memory. In this last byte, the number 0 is stored. It is called the null terminator or null character and marks the end of the string, similar to a STOP codon in a DNA sequence at the end of a reading from.
If you want to print the character 0 in the screen, you use ASCII code 48. If you want to mark the end of the strong, you use ASCII code 0.
Example, the way a the string literal “Sebastian” is stored is S e b a s t i an \0
The addition of this last byte means that although the string “Sebastian” is 9 characters long, it occupies 10 bytes of memory.
The null terminator is another example of something that sits quietly in the background. It doesn’t print on the screen when you display a string.
Consequently, storing a single character as a string literal vs a character becomes inefficient.
You can store ‘A’ a char, which looks like [A] in memory as one byte of data, or you can store it as a string, which looks like [A] [\0] internally, taking up 1 extra byte of data.
include <string></string>
To use the string class, you must define it as a preprocess directive in the form of
Floating-Point Data
Whole numbers are not adequate for many jobs. If you are writing a program that works with dollar amounts or precise measurements, you need a data type that allows fractional values. In programming terms, these are called floating-point numbers.
Internally, floating-point numbers are stored in a manner similar to scientific notation.
Take the number 47,281.97. In scientific notation, this number is 4.728197 × 104. (104 is equal to 10,000, and 4.728197 × 10,000 is 47,281.97.)
The first part of the number, 4.728197, is called the mantissa. The mantissa is multiplied by a power of 10.
Computers typically use E notation to represent floating-point values. In E notation, the number 47,281.97 would be 4.728197E4. The part of the number before the E is the mantissa, and the part after the E is the power of 10.
When a floating-point number is stored in memory, it is stored as the mantissa and the power of 10.
Table 2-8 Floating-Point Data Types on PCs
Data Type
Key Word
Description
Data Type: Single precision
Key Word: float
Description:4 bytes. Numbers between ±3.4E–38 and ±3.4E38
Data Type: Double precision
Key Word: double
Description: 8 bytes. Numbers between ±1.7E–308 and ±1.7E308
Data Type: Long double precision
Key Word: long double
Description: 8 bytes*. Numbers between ±1.7E–308 and ±1.7E308
You will notice there are no unsigned floating-point data types. On all machines, variables of the float, double, and long double data types can store positive or negative numbers.
You can also express floating-point literals in decimal notation. The literal 1.495979E11 could have been written as:
The bool Data Type:
Boolean variables are set to either true or false. Expressions that have a true or false value are called ‘Boolean’ expressions, named in honor of English mathematician George Boole
The bool data type allows you to create small integer variables that are suitable for holding true or false values.
1 // This program demonstrates boolean variables.
2 #include <iostream>
3 using namespace std;
4
5 int main()
6 {
7 bool boolValue;
8
9 boolValue = true;
10 cout << boolValue << endl;
11 boolValue = false;
12 cout << boolValue << endl;
13 return 0;
14 }</iostream>
Program Output:
1
0
As you can see, the value ‘true’ is represented in memory by the number 1, and ‘false’ is represented by the number 0.
The ‘=’ sign is called the assignment operator and is used for assignment statements.