learning cpp c++
Questions
-
why are virtual functions defined like this: ?
virtual const std::string& getName() = 0; -
why is the bitwise shift left << operator used for like printing?
cout << "test"
operator overloading syntax
the cout function is overloaded to allow it to be more flexible and be able to print more datatypes. rather than having a print() function that only takes in string parameters.
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
-
clangd LSP server
-
clang-formatter formatter
formatting options are edited in a.clang-format
file in the root of the project. -
search specifically for "Build Tools for Visual Studio" https://visualstudio.microsoft.com/downloads/. These include a whole bunch of build tools, not just the c++ ones.
-
CMake (cross-platform make) is an abstraction above compilers. Cmake generates make files.
cmake --build
detects what build tool I have installed on my machine and automatically calls it. -
ninja super small and fast. I installed this as well
manually compiling can take a whole bunch of commands and files.
ideally one uses a makefile.
other compilers:
- clang
- gcc (gnu c compiler) makefile files
- MSBuild, msvc
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 .
- use reference:
- in function parameters and return types.
- use pointers:
- if pointer arithmetic or passing a NULL pointer is needed. For example, for arrays (which need pointer arithmetic).
- to implement data structures like linked lists and their algs. In order to point to another cell we have to use the concept of pointers.
use references when you can and pointers when you have to.
when to pass function arguments by pointer:
- to modify the local var of the function.
- for passing large sized arguments: since ptr is only an address, it is more efficient. In this case it is recommended to use const pointer.
- when you want to return multiple values.
classes
- class vs struct keyword
struct members are public by default, class members are private by default?
struct is mainly still in cpp for compatability with C.
use struct for plain old data structures without class-like features.
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
-
static members (attributes)
a static members of a class are not associated with the objects of the class.
meaning all objects of the class will share the same variable/value. -
static class functions / static methods
static methods are available without having to instantiate a class.
can be accessed like so:Class::foor();
static member functions can access other static members and functions inside or outside the same class.
static methods can't acces thethis
pointer. bc it's not associated to an object.
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
- what is the virtual keyword?
declare virtual functions, member functions that can be overriden in derived classes
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.
- why do some includes use <> and others "" ?
<> is used for standard library header files and system specific header files
"" is used for used defined header files.
syntax
auto keyword automatically intructs the compiler to automatically deduce the data type of a variable on its init.
- what is the ~ syntax?
bitwise complement, bitwise inversion of a value
also class destructor