You are on page 1of 12

guide.

md 10/20/2021

comments

//single line

/*
multiline
*/

variable types

//int - integer
int num = 10;

//double - float
double num = 10.12;

//const - keep variable constant


const int num = 10; //error throw if attempt to change

//string - collection of chars (words)


string word = "word"; //need to #include <string>

//char - individual letters


char letter = 'a'; //single quotes for char

type conversions

int num = 10;


double numDouble = static_cast<double>(num); //10 -> 10.0

escape sequences

"\n" //newline
"\t" //tab
"\'" //single quote
"\"" //double quote
"\\" //backslash

1 / 12
guide.md 10/20/2021

math shit

int num1 = 10;


int num2 = 6;

//integer division
num1 / num2; //-> 1 | truncates number

//modulo operator %
num1 % num2; //-> 4 | evaluates remainder

//random number
rand() % 10; //-> 0 to 9

(rand() % 11) + 20; //-> 20 to 30

//given a range a to b
(rand() % (b - a) + 1) + a

operators and their precidence

() //parenthesis shit first


! //not operator next
* / % + - //airthmetic operators next
< <= > >= //relational operators next
== != //equality and inequality
&& //AND
|| //OR

2 / 12
guide.md 10/20/2021

if statement

//if
if(condition) {
//expression
}

//if-else
if(condition) {
//expression1
} else {
//expression2
}

//cursed if else
(condition) ? //expression1 : expression2;

//no brackets
if(condition) /*expression*/;

loops

//for
for(int i = 0; i < 5; ++i) {
//loop from | i = 0 -> i = 4
}

//while
int i = 0;
while(i < 5) {
//loop from | i = 0 -> i = 4
++i;
}

3 / 12
guide.md 10/20/2021

switch statement

int a = 10;
switch (a) {
case 5:
//a = 5
break;
case 10:
// a = 10
break;
default:
// a is not any of the cases
break;
}

break and continue

//break
for(loop condition) {
if(condition) {
break; //exits loop if condition is met
}
}

//continue
for(loop condition) {
if(condition) {
continue; //immediate jump to loop condition
}
}

4 / 12
guide.md 10/20/2021

functions

basic definitons

1. function call - invocation of function name causing the statements to execute


2. function definition functions name and block of statements belonging to function
3. paremeter - function input specified in the funciton definition
4. argument - value provided to a function parameter

void print(int num); //function declaration

//function definition
void print(int num) {
std::cout << num << std::endl;
}

//argument
int number = 10;

//function call
print(number);

unit testing

basic definitions

1. unit testing - individually testing smaller parts of a program


2. testbench - seperate program used for testing if a function returns correct output given some input
3. test vector - set of input for function we are testing

int funcToTest(parameters) {
//contents
}

assert(funcToTest(testVector) == <desired output>);

5 / 12
guide.md 10/20/2021

exception handling

basic definitions

1. try - block surrounds code, is exited if throw executes


2. throw - inside try block, if reached -> jump to end of block
3. catch - immediatly follows try block, if catch reached AND exception type matches, handle code
executes

try {
//if error detected
throw exceptionType;
}
catch (exceptionType excptObj) {
//handle exception
}

pass by refrence

void passTest(int &a) {


a += 10;
//no return needed
}

int main() {
int a = 0;
passTest(a);
std::cout << a << std::endl; //a = 10;
}

6 / 12
guide.md 10/20/2021

reading data from file

#include <iostream>
#include <fstream>

int main() {
std::ifstream inFS;
int readNum;

//open file
inFS.open("filetest");

//check that it was opened successfully


if(!inFS.is_open()) {
//file not open
}

//read data from file stream


inFS >> readNum;

//close file
inFS.close();

return 0;
}

commandline argumens basic definitions

1. argc - number of commandline arguments


2. argv - array of strings

int main(int argc, char* argv[]) {


argv[1]; // first input
argv[2]; // second input
//etc.

7 / 12
guide.md 10/20/2021

recursion basics

void countDown(int num) {


//base case
if (num <= 0) {
cout << "done!";
return;
} else {
//recursive case
countDown(num - 1);
}
}

countDown(5);
/*
5 4 3 2 1 done!
*/

sorting algorithms

Bubble sort - O(n^2)

1. step through list


2. if adjacent items are out of order
3. swap
4. repeat untill done

Selection sort - O(n^2)

1. set min to location 0


2. search min element in list
3. swap with value at index MIN
4. increment MIN to next element
5. repeat untill list is sorted

Insertion sort - O(n^2)

1. if at first element, list sorted


2. pick next element
3. compare with all elements in sorted sublist
4. shift all elements in the sorted sublist that are greater than the value to be sorted
5. insert value
6. repeat untill list is sorted

8 / 12
guide.md 10/20/2021

Quicksort - O(n^2)

1. Choose the highest index value has pivot


2. Take two variables to point left and right of the list excluding pivot
3. left points to the low index
4. right points to the high
5. while value at left is less than pivot move right
6. while value at right is greater than pivot move left
7. if both step 5 and step 6 does not match swap left and right
8. if left ≥ right, the point where they met is new pivot

merge sort - O(n log n)

1. if it is only one element in the list it is already sorted, return.


2. divide the list recursively into two halves until it can no more be divided.
3. merge the smaller lists into new list in sorted order.

9 / 12
guide.md 10/20/2021

class stuffs

basic definitions

1. class - a type that combines data and functions to form an object


2. public / private data member - the variables in the private or public sections of the class
3. inline member functions - a function defined within the class
4. mutators - functions that modify the class data members
5. accessors - functions that access data but do not modify it
6. constructor - special member function to intialize class data members
7. destructor - special member function that is called automatically when variable of class type is
destroyed

class A {
public:
A(); //default constructor

//additional constructor
A(int num) { this->privateDataMember = num; }

~A(); //destructor

void publicMemberFunc () {
std::cout << privateDataMember << std::endl
}

int publicDataMember;
private:
void privateMemberFunc ();
int privateDataMember;

int main() {
int num = 10;

//class object
A objA(10);

//using class object


objA.publicMemberFunc();
}

10 / 12
guide.md 10/20/2021

pointer basics

int num = 10;


int* pointVal;

//using address of operator &


pointVal = &num; //val pointer is given/pointed to assigned the memory address of
num

std::cout << pointVal << std::endl; //pointer -> memory adress

//using the derfrencing operator *


std::cout << *pointVal << std::endl; //pointer -> contents at memory adress

using new and delete

//assingin the pointer a mem location


int* ptr = new int;

//giving that mem location sum data


*ptr = 10;

//freeing that memory


delete ptr;

11 / 12
guide.md 10/20/2021

using copy constructor and destructor

class Num {

public:
int getNum();
Num( int value ); // constructor
Num( const Num &obj); // copy constructor
~Num(); // destructor

private:
int *ptr;
};

Num::Num(int value) {
ptr = new int;
*ptr = value;
}

Num::Num(const Num &obj) {


ptr = new int;
*ptr = *obj.ptr;
}

Num::~Num() {
delete ptr;
}

int Num::getNum() {
return *ptr;
}

void display(Num obj) {


cout << "number : " << obj.getNum() <<endl;
}

int main() {
Num line(10);

display(line);

return 0;
}

12 / 12

You might also like