# Struct (structure) ###### tags: `Teaching material`, `C++`, `structure` ## Why struct? Grouping data ## Declare ``` // purely struct point { int x; int y; struct point operator + (struct point& p) { return { this->x + p.x, this->y + p.y }; } }; struct point p1 = {1, 0}; struct point p2 = {0, 1}; struct point p3 = p1 + p2; // {1, 1} ``` ``` // with typedef struct point { int x; int y; }; typedef struct point point; point p = {1, 0}; ``` ``` // with typedef (shorter) typedef struct point { int x; int y; } point; point p = {1, 0}; ``` ## Initialize ``` point p1; p1.x = 1; p1.y = 0; point p2 = {1, 0}; point p3 = { .x = 1, .y = 0 }; ``` ## Interact with function, array pass by value!! (same as primitive type) ``` struct point plusOne(struct point) { return { point.x + 1, point.y + 1}; } struct point plusOne(struct point) { point.x++; point.y++; return point; } ```