Loading...

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:

text
7 lines
|
31/ 500 tokens
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

cpp
5 lines
|
17/ 500 tokens
1
2
3
4
5
struct Student {
    string name;
    int age;
    int classNo;
};
Code Tools

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

cpp
2 lines
|
7/ 500 tokens
1
2
Student s1;
s1.name = "Ali";
Code Tools

Accessing Structure Members

Use the dot (.) operator to access values inside a structure.
It lets you read or update each field individually.

Example

cpp
1 lines
|
4/ 500 tokens
1
cout << s1.age;
Code Tools

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

cpp
1 lines
|
6/ 500 tokens
1
enum Grade {A, B, C, F};
Code Tools

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

cpp
1 lines
|
4/ 500 tokens
1
s1.grade = A;
Code Tools

Let’s Build the Student ID Card

cpp
30 lines
|
148/ 500 tokens
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;
}
Code Tools

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:

  1. Defines a struct Car with fields: brand, model, year, and type (enum).
  2. Uses an enum CarType {SEDAN, SUV, TRUCK}.
  3. Stores values inside a Car variable.
  4. Prints:
cpp
7 lines
|
32/ 500 tokens
1
2
3
4
5
6
7
========================
   CAR DETAILS
   Brand: Toyota
   Model: Corolla
   Year: 2020
   Type: SEDAN
========================
Code Tools

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