CMSC 202 Exam 1
Logical operators
&& [and] || [or] ! [not]; and, or has less precendence than not; part of flow of control
Null terminator
'/0'; used in c-string initialization to indicate the end of a c-string
Comparison operators
(==, !=) has less precedence over (>, <, >=, <=); part of flow of control
Ternary operator
(x > y ? x : y) If x greater than y, then x. else, y. Simplified syntax for if statements; part of flow of control
<fstream> commands
.open(filename); to read, use inputStream >> var; .close(filename)
Assignment operators
=, +=, -=, *=, /=, %=; has less precedence to comparison and logical; part of flow of control
Pass by value parameters
A copy of the original variable is made inside function and it is only changed inside of the function's local scope. If returned, copy is sent back to main(), else, it will die in the function
Void function
A function that does not return anything
Class
A group of objects with similar properties and behaviors. Has states/attributes (variables) and behaviors (methods). Automatically private unless changed.
Memory address
A hexademical value
Variable
A memory location to store data
If statements
A statement that executes based on the condition (boolean). If one statement in the entire thing, no need for braces; part of flow of control
Array indexing
Access elements in array using indices; array[2]
Function prototype
Also called function declaration, includes function name, parameters, and return type. Place above main().
C-string
An array of characters; works like an array in general
Object
An instance of a class
While loop
As long as condition is true, loop will run. Can run from 0 to infinity times. Condition is checked before the body is executed
Do-while loop
Can run 1 to infinity times. Condition is checked after the body (do-part) is executed.
Constants
Cannot change values during execution. All data types can be made into constants
Switch statement
Controls multiple branches, does same thing as if statements but more convienent. Controlling expression must return boolean values
Post-condition of function
Details what the function is expected to return/do if executed
Cout-ing Void functions
Do not cout void functions because they do not return anything
Preprocessor directive
Executes before the compiler and simply copies library files to your file
Pointers as function parameters
Functions can be void when passing in pointers
Function overloading
Functions that complete similar tasks, have same name but different parameter lists. Based on function signatures (parameters must be in same order)
<iostream>
Holds cin, cout, cerr. Part of preprocessor directives.
Data type: boolean
Holds up to 1 byte. True (1) or False (0)
Data type: character
Holds up to 1 byte. Use single quotes
Data type: integer
Holds up to 4 bytes
Data type: float
Holds up to 4 bytes. Can have precision set to 7 decimal places
Data type: double
Holds up to 8 bytes. Can have precision set to 15 decimal places
Displaying output: floating-points
If floating-point results in .0, output will only show whole number, not the decimals.
cin.clear()
If the input that is being tested through cin.fail() does not match the declared date type, cin.clear() function then clears the input.
Function signature
Includes function name and the order of parameters. No return type.
<iomanip>
Input output manipulator. Holds setw(), cout.fill(), setprecision()
Setw()
Sets field width to n
Break statement
Terminates a loop. Must be used in switch statements
Array memory address
The first element of an array is the array's memory address
Function definition
The implementation of the code (the actual body). Place under main().
Indentifier
The name of the variable, function, class, structure, etc. Case-sensitive, camel-case, do not use keywords like int.
Pass by reference parameters
Use & before parameter name. Original value of variable will change permanently in the function. int &money;
Deferencing pointer
Use * again for the already declared pointer to get the value of variable it is pointing at.
Pointer intialization
Use * to declare pointer, and assign it with a variable's address. Pointer type must match variable type. int *ptr = &x
Initializing a variable
Use an assignment operator
getline(cin, variable)
Used to take the whole length of an input when using cin. Assigns that input to cin. another ex: cin.getline(char, 80) <-- char[80] char array of size 80
Literals
Values that were literally typed in.
Overloading resolution
When user program runs and finds overloaded functions being called. 1st: exact match, no argument conversion required 2nd: compatible match; looks for most compatible signature and does automatic type conversion
Escape sequences
\n, \t, \\, \"
Array
a collection of related data items. Allocates memory addresses for the data. int array[size];
Class name and methods
always begin with capital letter
Default argument
an argument that is passed automatically to a parameter if the argument is missing on the function call. Only found in function prototype; must be at the end of the list of parameters.
Multi-dimensional array
an array that uses more than one index to specify a stored value. 2d_array[x][y]
Access specifier type: public
anything that has access to the object has access to all public member variables and functions. Have at least one public member
Access specifier type: protected
can only be accessed by member functions of the class.
Using pointers for c-strings
char *word is another way to pass a c-string because c-strings are arrays, and arrays are like pointers (they hold memory addresses).
Namespace
collection of name definitions
Structure
collection of values of different types, no memory is allocated, define struct as global variable. Has member variables and a semicolon at the end. Automatically public
strcmp()
compares two c-strings
Debugging methods
cout statements, compiler debugger, stubs and drivers
Scope
curly braces define scope
Pre-condition of function
details what is required for the function to work properly
cin.fail()
determines whether the current input variable matches the declared data type. Combine with cin.clear()
Stubs
each function is designed, coded, and tested separately. ensures validity of each unit. Develop incrementally. Combine techniques with a driver
Comma operator
evaluates list of expressions, returns the value of the last expression. mostly used in for-loops
For loop
executes an explicit number of times. For (int i = 0; i > 3; i++)
<fstream>
file stream, handles data being read from a file as input. declare input stream: ifstream inputStream;
Input stream
flow of bytes from the device to main memory (i.e. keyboard)
Output stream
flow of bytes from the memory to the device (i.e. screen)
Setprecision()
formats floating-point data types. Will show n number of decimal places. Two ways: cout << setprecision(n) OR cout.precision(n)
Argument
function(argument); refers to function calls
Parameter
function(parameter); refers to function prototype
<ctime>
has time(); use time(NULL) to get current computer time. Can use for srand()
Libraries
holds the commands for certain commands. Are preprocessor directives
Parts of a variable
identifier, memory address, value of the variable, and its scope
<cstdlib>
includes srand(), rand()
Protecting pass by reference parameters
make the original variable passed as a constant
Arrays as function parameters
must put array type, and array size
Struct and Class data
must start member variables names with m_
Separate file compilation
once classes are defined, separate into a header file and cpp file.
Array length
program does not array length, so user must get the length of array so it will not go out bounds.
Auto-initialization
program will automatically allocate space for an array size even if the user does not declare it explicity. int arr = {2, 3}; has size of 2.
Automatic type conversion
promotes or demotes variable types. Promotion results in no data loss while demotion might. Promotion: int, float, char --> double Demotion: double --> int, float
Cerr
provides mechanism for distinguishing between regular output and error output. Works like cout
cin.peek()
returns next character without removing
Sizeof()
returns the size of a variable. can be used to find size of an array (in data bytes)
Random number geenrator (RNG)
seed randomizer using srand() once, then can call rand(). rand() % 1; (0-1 range)
Abstraction
separates code use from code implementation; cin is a command you can use but you do not know the code behind it.
cout.fill(c)
sets "fill" character to c
Scope resolution operator for classes
specifies which class the function belongs to Class::myFunction()
Encapsulation
the hiding of implementation details; data and functions that are located in a class is considered the encapsulation. protects data
cin.ignore(number, character)
this funtion tells the cin object to skip characters in the keyboard buffer. ex: cin.ignore(256, '\n') ignores 256 characters until it finds a new line
makeFile for classes
to compile each class, add rules to makeFile to combine the separated files
Return statement
transfer control back to the calling function i.e. main() most times; required for all types of functions except void; multiple returns can be used
Pointer
type of variable whose value is a memory address
Dot operator
used to access member variables and methods.
Driver
used to test code independently
Cin
uses extraction operator >>. No literals for cin, must be a variable
Cout
uses insertion operator <<
Arrays as function arguments
using single element as argument, element is passed by value. Using whole array as argument, array is passed by reference
Local scope
variables live and die inside of their corresponding function