cop3530 quiz 1 (c++ refresher)
what are some examples of preprocessor directives?
#include, #define, #ifdef, #ifndef, #endif, #pragma once
how do you declare a variable const?
const <datatype> <name> = value; <datatype> const <name> = value;
what data type(s) can you use for the number 412? a) int b) unsigned int c) signed int d)double
a,b,c
how do you declare 2d arrays?
int grid[4][5];
what exactly are memory leaks and why do we care about them?
leaks are unreferenced pointers to dynamically allocated memory. its wasted memory thats cleaned up by the OS after program execution memory leaks can crash the system if too much memory is leaked
how do you get the memory address of a variable
&x
how do you access the third element of an array of pointers
*(num_ptr + 2)
which of the following are true about templates? 1) We can write one template function that can be used for all data types including user defined types. Like sort(), max(), min(), etc...2) 2)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...3) 3)Templates are an example of overriding.
1+ 2 templates are an example of overloading
what is a pointer and how do you declare?
a variable whose value is the memory address of another variable <datatype> *<name> (whitespace doesnt matter) int* x or int *x
when we pass an array to a function we pass the ____ of the data in memory
address of the start
what does std::array do?
adds additional functionality to arrays, such as size array<int, 5> //array of 5 int elements
pointers and what are closely related
arrays (they can be converted to pointers of the same type) int numbers[5]; int *num_ptr = numbers;
name misc data types
bool (1 byte) void ("0 bytes") std::string
similarities and differences of structs and classes
both are ways of creating objects members of a struct are public by default members of classes are private by default
what are the auto and decltype keywords
can be used to derive the type of variable on the fly to improve readability int x = 5; auto y = x; decltype(x) y = x;
name character data types
char (1 byte) std::string (char array)
how do you declare a const pointer to a const int?
const int * const x int and memory address cannot be changed
how do you declare a pointer to a const int?
const int * x or int const * x int cannot change, but the memory address can
what do templates create and why are they useful?
create generic data type functions and classes useful for any data structures that can have ints, strings, and other data types
how do you declare and initialize arrays?
declare: <datatype> <name>[<size>]; initialize: <datatype> <name>[<size>] = {<value1>, <value2>, ...};
whats the difference between these 2 lines of code? int *yee = new int(3); int *yee = new int[3];
first is a pointer to a single integer, cleaned up by delete second is a array of pointers, cleaned up by delete[]
assume we have a singly linked list.how do we remove node b without causing a memory leak?
have variables for pointers a and b sets a's next to b's next delete b (prevents memory leak)
what does #ifdef, #ifndef, and #endif do?
identifiers that allow sections to be compiled if an identifier is/isnt defined #ifndef NODE_H #define NODE_H // foo header #endif
when declaring an object const what will cause an error
if a const object called a non-const function
if a const is used at the end of a class's function declaration what will cause an error
if the function tries to mutate the state of the object
what are some control actions based on logic?
if/else switch control while loop for loop
what does the const keyword do
indicates that a variable is not allowed to change
when a class member is const, it must be initialized through
initializer list
name number data types
int (4 bytes) unsigned int (4 bytes) long (8 bytes) unsigned long (8 bytes) double (8 bytes) float (4 bytes)
how do you declare a const pointer to a mutable int?
int * const x; //memory address is locked, but int value can change
what does #pragma once do?
makes source file to be included only once in a single compilation
how do you get the memory address and value at the memory address of a pointer?
memory address: x dereference: *x
do you have to prepend the template <typename T>before the class declaration and its internal function declarations?
no only class declaration
whats the difference between pass by value and pass by reference
pass by value: passes copy of the value, does not change main value pass by reference: accesses the variable at the referenced memory location, changes main value
what does #define do?
replaces all instances of identifier with replacement #define NUM_ITEMS 500
what does #include do?
replaces the directive with the content of the specified header or file
what do we have to prepend for templates
template <typename T> before every function or class declaration
what additional thing do we have to do if we want to pass a multidimensional array to a function
the length of every dimension except the first must be specified
how can you initialize variables with unknown memory requirements?
use the "new" keyword int *foo_ptr = new int(3); //delete with delete int *foo_arr = new int[userInput]; //delete with delete[]