-
Namespace :
- To group entities under a name (Classes, Objects, Functions)
- To divide the global scope in sub-scopes
- Each one with its own name
-
namespace identifier { int a; int b; }
- access with identifier::a
using identifier::a;
=> can use a without identifierusing namespace identifier;
=> can use a, b without identifiernamespace new_name = current_name;
=> namespace alias
-
Imperative vs. Declarative language
- Imperative language : expressing how the program should accomplish
- Sequence of commands for the commputer to perform
- Declarative language : expressing what the proram should accomplish
- Imperative language : expressing how the program should accomplish
-
Object-Oriented Programming
- Modeling real-world objects in software
- Modeling communication among objects
- Encapsulating attributes and operations
- Information hiding
- Communication through well-defined interfaces
-
C++ :
- Six phases of C++ Programs
- Edit : Writing program (and storing source code on disk) (.cpp)
- Preprocess : Performing certain manipulations before compilation (.cpp)
- Compile : Translating C++ programs into machine languages (.cpp -> .o)
- Link : Linking object code with missing functions and data (.o -> executable)
- Load : transferring executable image to memory
- Execute : executing the program one instruction at a time
- Six phases of C++ Programs
-
Separating interface from implementation
- A class's interface consists of the class's public member functions (services)
- Define member functions outside the class definition
- Implementation details are hidden for the client
-
C vs C++ : static
- static in both C and C++
- exists for the lifetime of the translation unit
- Global static variable : resides in file scope
- Local static variable : still have in block scope (although they exist from the time the program begins execution)
- exists for the lifetime of the translation unit
- static in C++
- Static variables / functions are bound to the class type rather than the instance
- static in both C and C++
-
Function definitions
- Prototype vs. Signature
- Prototype : The full declaration of function (including return value)
- Signature : part of the prototype that the compiler uses to perform overload resolution (excluding return value)
- Function overloading is done by signature type
- Prototype vs. Signature
-
Storage Classes
- auto : automatic storage duration (default in modern C++, so deprecated)
- register : hints the compiler to store variable in a register (compilers are smarter than us, so deprecated)
- static : (see above)
- extern : when using the same global variable on multiple files
-
Call stack / activation record
- Stack frame holds these three things:
- Arguments
- Return address
- Local variables
- Function call이 계속 되면서 stack은 어느 방향으로 증가하는지 (see code way down below)
- Stack frame holds these three things:
-
Inline Function
- copies code in the place of function call
- any change to an inline function require all clients of the function to be recompiled
- should only be used with small, frequently used functions
-
References
- References / constants have to be initialized on definition and cannot be changed
- Difference between changing the object bound to a reference (Ok) and changing the reference itself (cannot be done)
-
Default Argument
- When using default arguments in the constructor, then it can act as a default constructor
-
Function overloading and templates
- Example
-
Recursion
- Advantages:
- Easier to think of (naturally mirrors the program)
- Disadvantages (compared to iteration)
- Overhead of repeated function calls
- Each recursive call causes another copy of the function's variables
- Extra memory assignment
- Advantages:
-
C++ Pointers
- The difference between two pointers is the number of elements in between (in array)
-
Copy constructor
- Syntax :
class Person { public: Person(); Person(const Person &person); }
- const is needed because we are not modifying the original object at all
- enables pass-by-value for objects
- Compiler provides a default copy constructor
- Performs a shallow copy
- When data members contain pointers to dynamically allocated memory.. Dangerous!
- Create our own copy constructor that performs a deep copy
- Also needs a destructor and operator= (Rule of Three)
- Syntax :
-
const
- read from right to left
- There is no such thing as a const reference (ex. Person& const person)
-
const member function
- cannot mutate the object
- specified as const both in the prototype and definition
- not allowed for constructors and destructors
- can be overloaded with a non-const version
- compiler chooses the function based on the constness of the object on which the function is invoked
-
Initializing via member initializer list
-
Friend functions and Classes
- Violate OOP principles
- making public member / allowing access to private member
- Don't use friend too much for large projects
-
Static data member
- only one copy of a variable shared by all objects of a class
- have class scope
- exists even when no objects of the class exists
-
Static member function
-
Inheritance
- About default constructors (Example)
class BaseClass { public: BaseClass() { x = 1; } // 1 BaseClass(int a) { x = a; } // 2 private: int x; } class DerivedClass : public BaseClass { public: DerivedClass() { y = 2; } private: }
- Several possibilities
- If 1 is commented: ERROR
- When some other constructor is defined, no synthesized default constructor
- If 1, 2 is commented: OK
- Synthesized default constructor created
- If nothing is commented: OK
- Can use our explicit default constructor
- If 1 is commented: ERROR
-
Polymorphism
- Invoked functionality depends on type of the handle used to invoke the function, not the type of the object to which the handle points
- When the virtual keyword is absent, the handle ptr/ref's function (base class function) is called
- (just adding for myself) Declare destructors virtual in polymorphic base classes!
-
Topics related to systems programming
-
Things that happen when the executable size becomes too large
- Paging : memory management scheme to retrieve data from secondary storage in same-size blocks called pages
- Letting programs exceed the size of available physical memory on a multitasking system
- Page fault : invalid reference to a page not currently in main memory
- Swapping : Paging for modern system architectures
- Moving a program or its part between main memory between a dedicated swap space
- Paging : memory management scheme to retrieve data from secondary storage in same-size blocks called pages
-
Static vs Shared (Dynamic) libraries
- Static libraries: collection of object files that are linked into the program during the linking phase of compilation, and are not relevant during runtime
- Disadvantage : more paging & swapping
- Shared Libraries: collections of object files that are linked into the program at runtime
- Advantages:
- Reduced size of each executable file
- Easy library-functions replacement
- Disadvantages
- Dynamic linking overhead
- Advantages:
- Static libraries: collection of object files that are linked into the program during the linking phase of compilation, and are not relevant during runtime
-
-
Programs
- Stack increase order
#include <iostream> using namespace std; int s1; int s2; int s3; int s4; int t1 = 1; int t2 = 2; int t3 = 3; int t4 = 4; int main() { int a1 = 1; int a2 = 2; int a3 = 3; int a4 = 4; int *b1 = new int(1); int *b2 = new int(2); int *b3 = new int(3); int *b4 = new int(4); cout << &a1 << " " << &a2 << " " << &a3 << " " << &a4 << endl; cout << b1 << " " << b2 << " " << b3 << " " << b4 << endl; cout << &s1 << " " << &s2 << " " << &s3 << " " << &s4 << endl; cout << &t1 << " " << &t2 << " " << &t3 << " " << &t4 << endl; return 0; } ```cpp - Animal polymorphism (with abstract class) ```cpp #include <iostream> using namespace std; class Animal { public: virtual void speak() = 0; virtual void cry() { cout << "I hate this programming class..." << endl; } }; class Panda : public Animal { public: void speak() override { cout << "I just want to go home..." << endl; } void cry() override { cout << "Teach modern C++, damnit!" << endl; } }; class Monkey : public Animal { public: void speak() override { cout << "I just can't take shit anymore..." << endl; } void cry() override { cout << "Why use C Strings???" << endl; } }; int main() { Animal* animal = new Panda(); animal->speak(); animal->cry(); delete animal; animal = new Monkey(); animal->speak(); animal->cry(); // if the animal is a monkey... cry again if (dynamic_cast<Monkey*>(animal) != nullptr) { animal->cry(); } delete animal; return 0; }
Last active
April 27, 2016 06:36
-
-
Save lasagnaphil/a6bed5489f7ae03094c3d1b1ee3bbc27 to your computer and use it in GitHub Desktop.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment