Structures, Union & Enum in C++

So far, we have used variables to store individual values and arrays to store multiple values of the same type. But what if you want to keep different types of information together?

For example, a student may have a name, roll number, and marks. Creating separate variables for each student can become difficult to manage. Structures in C++ provide a simple way to keep related information together.

In this tutorial, we will look at structures, nested structures, arrays of structures, structures with functions, unions, enum, and enum class.

Structure in C++

A structure allows you to group different types of data under one name.

For example, you can create a structure for a student:

struct Student
{
    string name;
    int rollNo;
    float marks;
};

Now you can create a variable of this structure:

Student s1;

s1.name = "Aman";
s1.rollNo = 101;
s1.marks = 85.5;

To access a structure member, use the dot (.) operator.

cout << s1.name;
cout << s1.marks;

Output

Aman
85.5

A C++ Structure is useful when several pieces of information belong to the same thing.


Nested Structure

A structure can contain another structure as one of its members. This is called a nested structure.

struct Address
{
    string city;
    int pin;
};

struct Student
{
    string name;
    Address address;
};

Now you can create a student and access the nested information:

Student s1;

s1.name = "Aman";
s1.address.city = "Delhi";
s1.address.pin = 110001;

Here, address is itself a structure inside the Student structure.


Array of Structures

You can create an array where every element is a structure. This is useful when you have information about multiple objects of the same type.

Student students[2];

students[0].name = "Aman";
students[0].marks = 85;

students[1].name = "Riya";
students[1].marks = 92;

You can use a loop to display the information:

for (int i = 0; i < 2; i++)
{
    cout << students[i].name << endl;
    cout << students[i].marks << endl;
}

This is much easier than creating separate variables for every student.


Structure with Functions

You can also pass a structure to a function.

void display(Student s)
{
    cout << s.name << endl;
    cout << s.marks << endl;
}

Now you can call the function like this:

display(s1);

The function receives the structure and can access its members using the dot operator.


Union in C++

A Union in C++ is similar to a structure because it can contain different types of members. The main difference is how memory is used.

In a structure, each member gets its own memory. In a union, all members share the same memory location.

Example

union Data
{
    int number;
    float price;
    char letter;
};

You can use one member at a time:

Data d;

d.number = 10;

cout << d.number;

A union is useful when you need to store different types of data but only need one of them at a time.


Enum in C++

An enum is used when a variable should contain one value from a fixed set of choices.

For example, instead of using numbers to represent days, you can give them meaningful names.

enum Day
{
    Monday,
    Tuesday,
    Wednesday,
    Thursday,
    Friday
};

Now you can write:

Day today = Wednesday;

This makes the code easier to understand than using numbers such as 0, 1, or 2.


Enum Class in C++

An Enum Class in C++ is a safer and more modern version of an enum.

enum class Color
{
    Red,
    Green,
    Blue
};

To use a value, you write the enum name followed by ::.

Color color = Color::Red;

This keeps the values inside their own scope and helps prevent naming conflicts.

For example, two different enum classes can both have a value named Red without causing a conflict.


Object-Oriented Programming

Structures, unions, and enums are useful ways to organize data. C++ also supports Object-Oriented Programming (OOP), where data and functions can be organized together using classes and objects.

For example, a Student class can contain student information along with functions that work with that information.

OOP is a much larger topic, so classes, objects, inheritance, polymorphism, encapsulation, and other OOP concepts are covered separately.

Understanding Structures in C++, unions, and enums gives you a good foundation for working with more organized and complex C++ programs.


Frequently Asked Questions (FAQs)

Q1. Why are structures used in C++?

Structures in C++ group related data of different types under one name, making it easier to represent entities such as students, employees, and products.

Q2. Can a structure contain another structure in C++?

Yes. A structure can contain another structure as a member. This is called a nested structure.

Q3. How can I store multiple structures in C++?

You can create an array of structures to store multiple records of the same type, such as several student or employee records.

Q4. When should I use a union in C++?

Use a Union in C++ when different members need to share the same memory space and only one member is used at a time.

Q5. Are structures used in object-oriented programming?

Yes. C++ structures support features such as member functions, constructors, and access control, although classes are more commonly used for OOP.

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top