Discussion 1: C++ Refresher Flashcards
preprocessor directives
top of file with a #, processed before the compiler, #include, #define, (#ifdef, #ifndef, #endif), #pragma once
include
include replaces the directive with the content of the specified header or file
define (
define replaces all instances of identifier with replacement.
ifdef, ifndef, and #endif
identifiers allows sections to be complied if an identifier is/isnt defined
pragma once
makes source file to be included only once in a single compilation
char
1 byte | primitive type
std::string( char array )
char array
int (How many bytes, range)
4 bytes | -2.147 bil - 2.147 bil
unsigned int (How many bytes, range)
4 bytes | 0 to 4.294 bil
long (how many bytes, range)
8 bytes | -910^18 to 910^18
unsigned long (bytes, range)
8 bytes | 0 to 18*10^18
double (bytes, use case)
8 bytes | used for decimals
float (bytes, use case)
4 bytes | used for decimals
float vs double
float uses 4 bytes , double uses 8 bytes.
Use case: double is more precise
bool
1 byte | True or False
void
0 bytes | function not returning anything
std::string( char array ) vs std::string
std::string(char array) is an array of characters while string is not
auto (keyword)
derives the type of a variable on the fly
decltype (keyword)
derives the type of variable by using another variable
pointers
variable whose value is the memory address of another variable
pointer (syntax)
*
Address-of Operator
& | gets the memory address of a variable
Dereference Operator
- | gets the value at the memory address of a pointer
pointer of array
points at the first element of the array
array vs vector
array is just a pointer to its first elements. It has no knowledge of where it ends
new (keyword)
dynamically allocates memory in the heap, and returns a pointer
delete (keyword)
used to deallocate heap memory. delete or delete[] , pointer is not destroyed but the item the pointing is referencing is.
memory leak
dynamic memory (heap) that is no longer being referenced and not deleted
passing into functions in c++
pass by pointer, or reference, if not it will create a copy of passed in argument
structs vs class
both create object, structs are public by default, while class is private by default
templates
used when we don’t know the type it will receive. usually used at very low level
T o F: (Templates) We can write one template function that can be used for all data types including user defined types. Like sort(), max(), min(), etc
TRUE
T o F: (Templates) We can write one template class or struct that can be used for all data types including user defined types. Like LinkedList, Stack, Queue, etc…
TRUE
T o F: (Templates) Templates are an example of overriding
FALSE
const
variable is NOT allowed to change
What is mutable? int* const x
the memory address is locked, but the int value can change
What is mutable? const int* x or int const * x
The int cannot change, but the memory address can be changed.
What is mutable? const int * const x
The int and memory address cannot be changed.
const objects, can only be called by
const functions