Skip to content

Instantly share code, notes, and snippets.

@zhhailon
Created September 1, 2020 22:31
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save zhhailon/2d74373aa8dcd17d9ba3a9ad372c4b99 to your computer and use it in GitHub Desktop.
C++ Review
#include "utils.h"
using namespace std;
int main(int argc, char *argv[]) {
// C++ basics
//////////////////////////////////////////
// 0. Include, namespace, main, etc.
//////////////////////////////////////////
//////////////////////////////////////////
// 1. Values
//////////////////////////////////////////
// integers (1, 2, 0, -1, 999, ...)
// reals (0.0, 0.1, -0.009, ...)
// charaters ('a', 'b', ' ', '\n', '\t', ...)
//////////////////////////////////////////
// 2. Primitive data types
//////////////////////////////////////////
// integers: int
// reals: float, double
// characters: char
// struct, class
//////////////////////////////////////////
// 3. Variables
//////////////////////////////////////////
// Each variable corresponds to a memory location that stores a value, has a name/identifier, and must be declared with a desired type
int a;
a = 1;
double c = 0.2;
char d = 'x';
// string, vector, map, ...
//////////////////////////////////////////
// 4. Pointer and reference data types
//////////////////////////////////////////
// A pointer stores the address of another variable
int anInteger = 1;
cout << "anInteger:\t" << anInteger << endl;
cout << "&anInteger:\t" << &anInteger << endl;
int *intPtr = &anInteger; // define a pointer that points to anInteger
cout << "intPtr:\t\t" << intPtr << endl;
// *intPtr; // 1
*intPtr = 2;
cout << "*intPtr:\t" << *intPtr << endl;
cout << "anInteger:\t" << anInteger << endl;
// A reference is a short-hand for pointers
int &x = anInteger;
x = 3;
cout << "anInteger:\t" << anInteger << endl;
cout << "*intPtr:\t" << *intPtr << endl;
//////////////////////////////////////////
// 5. Function calls
//////////////////////////////////////////
// void name(...) { ... }
incrementByValue(anInteger);
cout << "by value:\t" << anInteger << endl;
incrementByReference(anInteger);
cout << "by reference:\t" << anInteger << endl;
//////////////////////////////////////////
// 6. Classes
//////////////////////////////////////////
ds::IntCell ic(1);
cout << "init:\t\t" << ic.read() << endl;
ic.write(5);
cout << "curr:\t\t" << ic.read() << endl;
// ic.storedVal;
//////////////////////////////////////////
// 7. Class templates
//////////////////////////////////////////
ds::Cell<int> intCell(1);
ds::Cell<char> charCell('a');
cout << "init:\t\t" << charCell.read() << endl;
charCell.write('c');
cout << "curr:\t\t" << charCell.read() << endl;
//////////////////////////////////////////
// 8. Function templates
//////////////////////////////////////////
printCell(intCell);
printCell(charCell);
//////////////////////////////////////////
// 9. Array
//////////////////////////////////////////
int arr[5] = { 1, 2, 3, 4, 5}; // [0, 4]
printArray(arr, 5);
// cout << arr[6] << endl;
//////////////////////////////////////////
// 10. Dynamic memory
//////////////////////////////////////////
int *anotherInt = new int; //malloc
*anotherInt = 7;
cout << "*anotherInt:\t" << *anotherInt << endl;
delete anotherInt; //free
anotherInt = NULL;
ds::IntCell *icPtr = new ds::IntCell(1);
delete icPtr;
icPtr = NULL;
int size = 5;
int *anotherArr = new int[size];
delete[] anotherArr;
anotherArr = NULL;
// for (int i = 0; i < size; i++) {
// anotherArr[i] = i + 1;
// }
return 0;
}
//////////////////////////////////////////
// 11. What is a data structure?
//////////////////////////////////////////
// In short, a data structure is a way in which data is stored in computer;
// Involves data organization, management, and storage
//////////////////////////////////////////
// 12. Why do we need data structures?
//////////////////////////////////////////
// Example: We'd like to search for the definition of "sprint" in a dictionary.
// Words are "organized" and "stored" in the dictionary in lexical order.
// We first find the page for words starting with "s", then look for words starting with "sp", then "spr", and so on.
// ---> efficient access and modification, in terms of *space* and *time*
//////////////////////////////////////////
// 13. Some general questions to ask
//////////////////////////////////////////
// What are the operations?
// Can data be deleted?
// Is random access allowed, or are data processed in some well-defined order?
// ...
//////////////////////////////////////////
// Next week: Lists
//
// HW1: Manipulating Arrays
//////////////////////////////////////////
#include <iostream>
using namespace std;
void incrementByValue(int foo) {
foo = foo + 1;
// int a = 1;
}
void incrementByReference(int &foo) {
foo = foo + 1;
}
void incrementByConstReference(const int &foo) {
// foo = foo + 1; // COMPILATION ERROR!!!
// cout << "foo:\t\t" << foo << endl;
}
namespace ds {
class IntCell {
private:
int storedVal;
public:
IntCell(int x) : storedVal(x) { }; // constructor
~IntCell() { } // destructor
void write(int x) {
storedVal = x;
}
// int b; // intCell.b
int read() {
return storedVal;
}
}; // <----- semicolon
template <typename E> class Cell {
private:
E storedVal;
public:
Cell(E x) : storedVal(x) { }; // constructor
~Cell() { } // destructor
void write(E x) {
storedVal = x;
}
E read() {
return storedVal;
}
}; // <----- semicolon
template <typename E> void printCell(Cell<E> cell) {
cout << "cell value:\t" << cell.read() << endl;
}
}
void printArray(int arr[], int size) {
cout << "array:\t\t<";
for (int i = 0; i < size; i++) {
cout << " " << arr[i];
}
cout << " >" << endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment