42 Common Core CPP Module 01
- OOP (Object-Oriented Programming) in C++
 - Memory Allocation
 - Pointers and references
 - Switch statements
 - File operations
 
- Memory Allocation
 - Stack vs Heap
 - Pointers and references
 - Pointers to members
 - Files and Streams
 - Switch Statement
 - More info
 
Dynamic memory in C++ is handled with new and delete.
- 
new:
- Allocates enough memory for an object
 - Calls the Constructor to initialize it
 - Returns a pointer to the newly created object
 - Similar to malloc(), but also initilizates the objet
 
 - 
delete:
- 
Calls the destructor of the object
 - 
Frees previously allocated memory with new
int main(void) { int *ptr; ptr = new int; //similar to malloc(sizeof(int)) *ptr = 42; std::cout << "ptr value: " << *ptr << std::endl; delete ptr; return (0); } 
 - 
 
Allocating arrays:
	int main (void)
	{
		int *ptr;
		ptr = new int[10]; //similar to malloc (10 * sizeof(int))
		delete [] ptr; //must be delete[] for arrays
		return (0);
  }
| Aspect | Stack | Heap | 
|---|---|---|
| Lifetime | Auto (ends with scope) | Manua (new/delete) | 
| Speed | Fast | Slower | 
| Use case | Temporary/local variables | Dynamic objects | 
| Example | Zombie z(); | Zombie* z = new Zombie() | 
| Management | Handled by compiler | Handled by programmer | 
- 
Pointers: a pointer is a variable that stores the memory address of another variable.
int x = 42; int *ptr = &x; //ptr stores the address of x *ptr = 21; //dereferencing changes x to 21 std::cout << x; //prints 21 
| Code | What it does | 
|---|---|
| * | Declares a pointer or dereferences one | 
| int *ptr; | Declares a pointer to int | 
| *ptr = 42 | Assigns vale through dereferencing | 
| & | "Address of" operator | 
Pointers can also be used with class instances:
MyClass obj;
MyClass *ptr = &obj; //pointer to object
- References: a reference is an alias for an existing variable. Unlike pointers:
- 
A reference must always refer to a valid object (cannot be NULL)
 - 
It cannot be reseated to refer to something else
 - 
It is always implicitly dereferenced
int x = 10; int &ref = x; //alias to x ref = 20; //modifies x directly std::cout << x; // prints 20 std::cout << ref; //prints 20 
 - 
 
Pointers to members allow access to attributes or methods of a class indirectly.
- 
Pointers to Data Members:
class Sample { public: int foo; void bar() const { std::cout << "bar() called" << std::endl; } }; int main() { Sample instance; Sample *instancep = &instance; int Sample::*p = nullptr; // pointer to int member of Sample p = &Sample::foo; // points to member foo instance.*p = 21; // set foo using object instancep->*p = 42; // set foo using object pointer std::cout << "foo = " << instance.foo << std::endl; } - 
Pointers to Member Functions:
void (Sample::*f)(void) const; // declaration f = &Sample::bar; //assignment Sample obj; (obj.*f)(); // call via object (obj->*f)(); // call via pointer to object 
| Code | What it does | 
|---|---|
| int Sample::*p | Pointer to int member of Sample | 
| obj.*p = 42 | Set member value via pointer | 
| void (Sampe::*f)() const | Pointer to const member function | 
| (obj.*f)() | Call method via object | 
| (obj->*f)() | Call method via pointer | 
C++ provides for file operations.
- 
ofstream: create/write files
ofstream myFile("filename.txt") myFile << "content"; myFile.close(); - 
ifstream: read files
std::string line; ifstream myReadFile("filename.txt"); while (getline (myReadFile, myText) { std::cout << line << std::endl; } myReadFile.close(); - 
fstream: read/write files
 
Switch is an alternative to multiple if/else if chains.
 switch (value)
  {
	case 1:
 		//code
 		break;
	case 2:
		//code
		break ;
	default:
		//code if no match
 }
Use break to prevent fall-through.