C++ Structures
🧩 C++ Structures (struct)
A structure (struct) in C++ is a user-defined data type that allows you to group different data types under a single name.
It is very useful for representing real-world entities like students, employees, products, etc.
 1. Why Use struct?
An array can store only one data type, but a structure can store:
-
int -
float -
string -
char -
etc.
all together in one unit
 2. Defining a Structure
👉 This only defines the structure (no memory allocated yet).
 3. Creating Structure Variables (Objects)
 4. Accessing Structure Members (. operator)
 5. Structure Initialization
 6. Complete Example Program
 7. Array of Structures
8. Structure with User Input
9. Nested Structures
 10. struct vs class
struct |
class |
|---|---|
| Members are public by default | Members are private by default |
| Used for data grouping | Used for OOP |
| Simple | More control |
📌 Summary
-
structgroups different data types -
Members accessed using
.operator -
Useful for real-world data modeling
-
Arrays of structures are very powerful
