Structures and Enums in C++
Imagine a student ID card. It holds different types of information together: a name, age, class, roll number, and more. You wouldn’t store all this in separate boxes, you keep it in one card, neatly organized. C++ lets us do the same using structures (structs). And when you want to store a fixed list of choices like Grades, Days, or Levels, C++ gives you enums.
Today, your program learns how to group different pieces of data into one meaningful unit.
A Digital Student ID Card
You’ll create a small program that stores:
- Student name
- Age
- Class
- Grade (using enum)
Then print it as a clean ID card:
1 2 3 4 5 6 7======================== STUDENT ID CARD Name: Ali Khan Age: 15 Class: 10 Grade: A ========================
This project teaches you how structs and enums help organize real-world data.
What Is a Structure?
A structure is a custom data type that groups variables of different types under one name.
It’s like creating a container for related information.
Perfect for storing complex records like students, books, or players.
Example
1 2 3 4 5struct Student { string name; int age; int classNo; };
Creating Structure Variables
Once you define a structure, you can create variables of that type.
Each variable stores its own set of grouped information.
Example
1 2Student s1; s1.name = "Ali";
Accessing Structure Members
Use the dot (.) operator to access values inside a structure.
It lets you read or update each field individually.
Example
1cout << s1.age;
What Is an Enum?
An enum (enumeration) defines a set of named constants. It allows you to store fixed options like Grade A, B, C in readable form. Great for menus, states, or categories.
Example
1enum Grade {A, B, C, F};
Using Enums with Structures
Enums inside a struct make data organized and meaningful. You can store a grade not as a number but as a readable label.
Example
1s1.grade = A;
Let’s Build the Student ID Card
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30#include <iostream> using namespace std; enum Grade {A, B, C, F}; struct Student { string name; int age; int classNo; Grade grade; }; int main() { Student s; s.name = "Ali Khan"; s.age = 15; s.classNo = 10; s.grade = A; cout << "========================" << endl; cout << " STUDENT ID CARD" << endl; cout << " Name: " << s.name << endl; cout << " Age: " << s.age << endl; cout << " Class: " << s.classNo << endl; cout << " Grade: " << s.grade << endl; cout << "========================" << endl; return 0; }
How It Works
- The Student struct groups name, age, class, and grade.
- The Grade enum provides predefined grade values.
- The program prints all details in a formatted card.
Test instantly using the DevsCall AI Code Runner.
Learn Together with AI
Try prompts:
- “Explain when to use structs instead of classes for beginners.”
- “Create a struct for a Book with title, author, pages, and price.”
- “Add an enum for BookCategory and use it inside the struct.”
- “Show me how to store multiple students using an array of structs.”
AI can help create larger, more realistic examples.
Practice Time
Write a C++ program that:
- Defines a struct Car with fields: brand, model, year, and type (enum).
- Uses an enum CarType {SEDAN, SUV, TRUCK}.
- Stores values inside a Car variable.
- Prints:
1 2 3 4 5 6 7======================== CAR DETAILS Brand: Toyota Model: Corolla Year: 2020 Type: SEDAN ========================
Ask AI if needed:
“How do I use enums inside a struct in C++?”
Frequently Asked Questions
A structure (struct) is a custom data type that groups different variables under one name, making it easier to store related information like student or product details.
Use a struct when multiple pieces of related information belong together, such as a name, age, and grade for a student.
An enum is a list of named constant values. It’s used when a variable should only hold one choice from a fixed set like Grade A, B, C, F.
Enums make code more readable and prevent invalid values. Instead of using numbers, you use meaningful names.
Yes! You can include enums inside structs to categorize items, like assigning a student grade or a car type.
No. You can run all struct and enum examples directly in the DevsCall AI Code Runner without installing anything.
Still have questions?Contact our support team