Show Menu
Cheatography

C++ Exam 1 Cheat Sheet (DRAFT) by

Cheat sheet for COP3503C Exam 1

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Vectors and Arrays

Method­/Types
Vector
Array
Create
vector­<ty­pe> var(num)
or
vector­<ty­pe> var{el­ement, element…}	
type var[num]
or
type var[] = {element, element…}
Find number of elements
var.size()
sizeof­(va­r)/­siz­eof­(va­r[0])
Access an element
var.at­(index)
var[index]
Modify an element
var.at­(index) = element
var[index] = element
Add an element
var.pu­sh_­bac­k(e­lement)
or
var.in­ser­t(v­ar.b­eg­in(­)+i­ndex, element)
n/a
Remove an element
var.po­p_b­ack()
or
var.er­ase­(va­r.b­egi­n()­+index)
n/a
for loop
for (int i = 0; i < var.si­ze(); i++) 
{cout << var.at­(i);}
for (int i = 0; i < sizeof­(va­r)/­siz­eof­(va­r[0]); i++)
{cout << var[i];}
Enhanced for loop
for (type i : var) {cout << i}
for (type i : var) {cout << i}
Common compatible types
integer, double, boolean, strings
int, double, boolean, strings

The Big Three

Copy Constr­uctor Used to construct an object from another, existing object
classN­ame­(const className &o­rig­inal) {
// copy over everything from
original to this }
Copy Assignment Operator Used to copy one object into another object
ClassN­ame­& operat­or=­(const ClassN­ame­& 
original) {
//same as copy constructor
return *this }
Destructor Used when an object is destro­yed­—when it falls out of scope, or when delete is called on a pointer to an object
~Examp­leC­lass(){ 
delete item_name
//or more compli­cated code }

Deep and Shallow Copy

Deep Copy
copies the data itself - allocate more space and clean it up (use destru­ctors)
Deep copy takes two steps:
1. Allocate space for the duplicate data
int* deep = new int[5];

2. Copy the data values from the original location

for (int i = 0; i < 5; i++) {
deep[i] = ptr[i]; }
Shallow Copy
copies pointers
Create the array
int* ptr = new int[5]; 
for (int i = 0; i < 5; i++) {
ptr[i] = i * 2;
// Set values to 0, 2, 4, 6, 8

Shallow copy the array
int* shallow = ptr;

Vectors and Arrays

What does
.push_­back()
do in a vector?
What does
.at()
do in a vector?
What does
[ ]
do in a vector?
How do you sort an array/­vector?

Vectors

Vectors are dynamic, meaning you can make changes to them while the program is running. Vectors are partic­ularly helpful when you don’t know how large your collection of elements will become.
In order to use vectors, you must include
#include <ve­cto­r>
in the header of your program.
vector structure :
vector­<in­t> vec_na­me(3);
name.p­ush­_ba­ck(­data)
adds whatever is in the parant­heses to the end of the vector
To add an element to a specific index in the vector:
vec_na­me.i­ns­ert­(ve­cto­r.b­egi­n()+1, 50);
adds 50 to index 1
To remove an element from the end of a vector:
vec_na­me.p­op­_back()
To erase a specific index:
vec_na­me.e­ra­se(­vec­_na­me.b­eg­in(­)+1);
erases the element at the index 1
vec_na­me.a­t(3)
accesses the element at index 3, use this to modify specific elements
 

Simple (Return types, Loops, & Condit­ionals)

How do you create functions in C++?
returnType Functi­on(int var_na­me){}

put
ClassN­ame::
beforehand if working in source file
What are the return types of these functions?
void, string, int, unsigned int, vector­<data type> (usually done with pointer), boolean, array (usually done with pointer)
Is it possible to return any data type?
yes, as long as it is declared in the beginning of the function
How do you write if condit­ionals and for loops in C++?
if(con­dit­ion){} 
for(int i = 0; i < condition; i++){}
What is the difference between if, else if, and while loops?
What are some of the algebraic and comparison operators in C++?
+, -, =, <, >, ==, !=, &&, ||
How do you use them with different data types?
must be used to compare 2 of the same data type, unless you use an operator overload

Pointers and references functions

What are pointers?
Pointers tell us the location of something else. A pointer is a variable that holds the memory address of another variable. A pointer needs to be derefe­renced with the * operator to access the memory location it points to.
int* name
creates a pointer to an int
What are refere­nces?
References act as a stand-in (or alias) for another variable. A reference is the variable that it refere­nces. Any changes to a reference change the original. They don’t use memory addresses, no derefe­ren­cing.
How do you pass objects by references and pointers?
using `FuncN­ame(int &v­ar_­name){}
What do you need to take into account when creating a function that intends to swap two integers, a and b?
make sure to pass by reference so the values can be changed
What does
->
do in pointers?
Indirect membership operator (For pointers to objects)