learning cpp c++

Questions

how to compile:

make a CMakeLists.txt > mkdir build > cd build > cmake .. > cmake --build

what CMakeLists.txt file to use?

single file CMakeLists.txt template:

cmake_minimum_required(VERSION 3.10)
project(PFMCPP_Project1)
add_executable(PFMCPP_Project1 main.cxx)

for JUCE project:
generate it with FRUT or a project template.

cmake --build build && .\build\Debug\App\HelloWorld.exe

tooling

manually compiling can take a whole bunch of commands and files.
ideally one uses a makefile.

other compilers:

pre-formatting > compiling > linking

compiling is taking the c code and turning it into object files.
object files are asm machine code files.
all files are compiled individually (like they are separate programs). and then are linked.

linking is taking all the object files and produces an executable or lib file.

"compilers" are often reffered to tools that do this entire process. instead of just the compilation step.

a lib file is a static library. it's a collection of pre-compiled code, archived in a single file. programs can link these libs to gain access to functionality they provide without duplicating the code themselves.
you include the library's header files in my code to use the libs.

.so files are shared/dynamic libraries files. these are linked at runtime by the operating system. the executable references the lib by name, and the OS loads the shared library from a system location when needed. This keeps the exe size smaller.
I think at runtime means passing the lib URI as an arg when running the executable.

data types

int float double bool char std::string
``
char mystring[] = "hello" or #include <string>
string hello = "hello"

arrays

arrays
have constant size.
arrays are always passed as pointers to a function. we can pass sized and unsized arrays as function params.

dynamic arrays

std::vector
automatically resize themselves and elements are inserted/deleted.

pointers vs references

pointers is a var that hold the mem address of another var. needs to be referenced with the * operator to access the memory location it points to.

pointers are ContiguousIterators, meaning you can use ++ to go to the next item that a pointer is pointing to.

reference var is an alias, another name for an already existing variable. A reference, like a pointer, is also implemented by storing the address of an object. A reference can be thought of as a constant pointer (not to be confused with a pointer to a constant value!) with automatic indirection, i.e., the compiler will apply the * operator for you.

int i = 3; 
// A pointer to variable i or "stores the address of i"
int *ptr = &i; 
// A reference (or alias) for i.
int &ref = i; 

pointers are dereferenced with * to access the object it points to. A reference can be used directly. A pointer to a class uses -> to access its members whereas a reference uses .
Pasted image 20240519204821.png

when to pass function arguments by pointer:

classes

structs can inherit, and have contructors and deconstructors.

struct

User defined data types. can have parameters and methods.
struct MyStruct{}
MyStruct myStruct;
myStruct.initialize();

inheritance:

class SuperHero: public Human{ }

you can overload methods. (redefining the same function multiple times with different parameters). is a form of polymorphism
public and private sections to define variables and functions.

example:

class MainComponent: public juce::Component
{
  public: 
    MainComponent();
    ~MainComponent() override;

    void paitn Graphics& override;
    void resized() override;

  private:
    JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MainComponent)
}

constructors / destructors:

class Human{ public: Human(){} }
Destructor runs code when the class is destroyed.
syntax: ~Human(){}

public / private params

you declare public fields inside a public: block of code
the same for private fields.

class Test {
public:
//code here
private:
// some more code here
}

Static members

one usage example of static members is to keep count of all created objects of a class.

protected members

a member that is not accessible outside the class definition but it is accessible by derived classes (inherited).

virtual functions

header files

defines symbols for compiling/linking.
usually contains documentation of the lib.
ifndef is a header guard for preventing including the header mutiple times, preventing recursive dependencies.

syntax

auto keyword automatically intructs the compiler to automatically deduce the data type of a variable on its init.