## Configuring VSCode ### IntelliSense Configurations 1. Go to the desired directory 2. View > Command Palette (Ctrl+Shift+P) > C++ Edit configuration UI 3. Compiler path = g++ (for C++) 4. C++ standard = 20 5. Ctrl+S (To save) ### Build task 0. Open and select a cpp file 1. Terminal > Configure Default Build Task 2. In Task.json 1. in "args" section add "-Wall", "-Wall", "-std=c++20", ### Debugging 0. Select cpp file 1. Run> Add configuration ## Interesting Sources Interesting opencv doc: [Here](https://docs.opencv.org/4.x/d2/d96/tutorial_py_table_of_contents_imgproc.html) [CppCoreGuidelines](https://isocpp.github.io/) ## The Basics ### References and pointers If you want obvious "no-copy" semantics: Use pointers (T*) when you want it explicit: “This function takes an address.” Use references (T&) only when you want to guarantee it can’t be null and can’t change which object it refers to. For large objects, you can use const T& to avoid copies while still signaling "read-only." ## The rule of three/five/zero ### Rule of three If a class requires a user-defined destructor, a user-defined copy constructor, or a user-defined copy assignment operator, it almost certainly requires all three. Because C++ copies and copy-assigns objects of user-defined types in various situations (passing/returning by value, manipulating a container, etc), these special member functions will be called, if accessible, and if they are not user-defined, they are implicitly-defined by the compiler. The implicitly-defined special member functions should not be used if the class manages a resource whose handle is an object of non-class type (raw pointer, POSIX file descriptor, etc), whose destructor does nothing and copy constructor/assignment operator performs a "shallow copy" (copies the value of the handle, without duplicating the underlying resource). ### Rule of five Because the presence of a user-defined (include = default or = delete declared) destructor, copy-constructor, or copy-assignment operator prevents implicit definition of the move constructor and the move assignment operator, any class for which move semantics are desirable, has to declare all five special member functions: ```C++ class rule_of_five { char* cstring; // raw pointer used as a handle to a // dynamically-allocated memory block public: explicit rule_of_five(const char* s = "") : cstring(nullptr) { if (s) { cstring = new char[std::strlen(s) + 1]; // allocate std::strcpy(cstring, s); // populate } } ~rule_of_five() { delete[] cstring; // deallocate } rule_of_five(const rule_of_five& other) // copy constructor : rule_of_five(other.cstring) {} rule_of_five(rule_of_five&& other) noexcept // move constructor : cstring(std::exchange(other.cstring, nullptr)) {} rule_of_five& operator=(const rule_of_five& other) // copy assignment { // implemented as move-assignment from a temporary copy for brevity // note that this prevents potential storage reuse return *this = rule_of_five(other); } rule_of_five& operator=(rule_of_five&& other) noexcept // move assignment { std::swap(cstring, other.cstring); return *this; } } ``` Unlike Rule of Three, failing to provide move constructor and move assignment is usually not an error, but a missed optimization opportunity. ### Rule of zero Classes that have custom destructors, copy/move constructors or copy/move assignment operators should deal exclusively with ownership (which follows from the Single [Responsibility Principle](https://en.wikipedia.org/wiki/Single_responsibility_principle)). Other classes should not have custom destructors, copy/move constructors or copy/move assignment operators[1]. ## Resource Acquisition Is Initialization (RAII) Resource Acquisition Is Initialization or RAII, is a C++ programming technique which binds the life cycle of a resource that must be acquired before use (allocated heap memory, thread of execution, open socket, open file, locked mutex, disk space, database connection—anything that exists in limited supply) to the lifetime of an object. RAII guarantees that the resource is available to any function that may access the object (resource availability is a class invariant, eliminating redundant runtime tests). It also guarantees that all resources are released when the lifetime of their controlling object ends, in reverse order of acquisition. Likewise, if resource acquisition fails (the constructor exits with an exception), all resources acquired by every fully-constructed member and base subobject are released in reverse order of initialization. This leverages the core language features (object lifetime, scope exit, order of initialization and stack unwinding) to eliminate resource leaks and guarantee exception safety. Another name for this technique is Scope-Bound Resource Management (SBRM), after the basic use case where the lifetime of an RAII object ends due to scope exit. RAII can be summarized as follows: encapsulate each resource into a class, where the constructor acquires the resource and establishes all class invariants or throws an exception if that cannot be done, the destructor releases the resource and never throws exceptions; always use the resource via an instance of a RAII-class that either has automatic storage duration or temporary lifetime itself, or has lifetime that is bounded by the lifetime of an automatic or temporary object. ## C++ Classes In C++, a class is a user-defined data type that encapsulates data (member variables) and behavior (member functions) into a single unit. It supports object-oriented programming (OOP) principles like encapsulation, inheritance, polymorphism, and abstraction. Classes are blueprints for creating objects (instances). Key components of a class: Members: Data members (variables) and member functions (methods). Access Specifiers: public (accessible from anywhere), protected (accessible in class and derived classes), private (accessible only within the class). Default is private for classes (vs. public for structs). Constructors/Destructors: Special functions for initialization (ClassName(...)) and cleanup (~ClassName()). Special Members: Compiler-generated if not defined (e.g., default constructor, copy constructor, move constructor, assignment operators) follow the Rule of Zero/Three/Five for resource management. Static Members: Belong to the class, not instances (e.g., static int count;). static members belong to the class itself, not to any individual object. There is exactly one copy of a static data member per class (per type), and static member functions operate without a this pointer. One storage slot shared by all instances of the class. Not part of object layout (so sizeof(obj) does not include it). Has static storage duration — constructed once and lives until program termination (or thread termination if thread_local). Forgetting to define the static data member (pre-C++17) Friend Functions/Classes: Grant access to private members. ### Classes can be: Concrete: Fully implemented and instantiable. A concrete class is a fully defined class with all member functions implemented (no pure virtual functions). It can be instantiated directly to create objects. Concrete classes are the most common and represent tangible entities with complete behavior. Abstract: Contain pure virtual functions; not instantiable, used as base classes. An abstract class cannot be instantiated because it contains at least one pure virtual function (declared with = 0). It serves as an interface or base for derived classes, enforcing a contract for subclasses to implement. Abstract classes promote polymorphism and abstraction. Part of hierarchies via inheritance.