Classes and Structs

Classes and structs are the same in C++, except the default access of a struct is public, whereas it is private in classes.

Creating Classes/Structs

  1. Direct (automatic) instantiation
    • Allocated on the stack
    • Disappear at then end of defining scope (like normal variables)
    • We can access fields with dots
    Coord c {};  
    c.x = 10;  
    c.y = 20;
    
  2. Dynamic instantiation
    • className* cPtr= new className;
    • Space allocated on the heap, and persists until deleted by the programmer
    • Use the arrow to access (pointer)
    Coord* p = new Coord {};  
    p->x = 10;  
    p->y = 20;  
    
    delete p;