- 01Getting Started
- 02Your First Output (cout)
- 03Variables and Types (int/double/string/bool)
- 04Conditionals (if statements)
- 05Loops (for statements)
- 06Writing a Function
- 07Working with Arrays
- 08Loops (while statements)
- 09Pointer Basics
- 10References
- 11Classes (Object-Oriented Programming)
- 12Constructors and Destructors
- 13Inheritance
- 14Using vector (the Standard Library)
- 15String Manipulation (string)
- 16Structs (struct)
- 17Error Handling (try/catch)
- 18The Ternary Operator
- 19The switch Statement
- 20[Applied] Build a Mini Inventory Management System
Structs (struct)
This lesson covers struct, used to bundle several pieces of data together. It's for anyone searching "C++ struct usage."
struct works very similarly to a class, letting you bundle multiple pieces of data into one type. The main difference from class in C++ is that a struct's members are public by default.
The example defines a struct, struct Point { double x; double y; };, and uses it to represent two coordinates.
A common early mistake is being unsure when to use struct versus class. A common convention: use struct when you just want to bundle plain data, and class when you want to attach a lot of behavior (member functions).
In real projects, structs are used to represent a simple bundle of data โ a coordinate, an RGB color, a set of settings, and so on.
๐งช This site can't compile or run C++ directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ nothing is sent anywhere).
