Skip to main content
C++ Fundamentals for Beginners to Advanced
CHAPTER 13 Beginner

Structures and Enumerations in C++

Updated: May 17, 2026
5 min read

# CHAPTER 13

Structures and Enumerations

1. Introduction

An array can store 50 integers, but what if you want to store a student's Name (string), Age (int), and GPA (float) together? Arrays cannot hold mixed data types. To solve this, C++ provides Structures (struct), which allow you to create your own custom, complex data types.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Define and instantiate a struct.
  • Access and modify structure members using the dot (.) operator.
  • Understand nested structures.
  • Create Arrays of structures.
  • Use Enumerations (enum) to improve code readability.

3. Creating a Structure

A struct is a blueprint. It doesn't use memory until you actually create a variable (an instance) of that type.
cpp
123456789101112131415161718192021222324
#include <iostream>
#include <string>
using namespace std;

// 1. Define the blueprint
struct Student {
    string name;
    int age;
    float gpa;
}; // ALWAYS end struct definitions with a semicolon!

int main() {
    // 2. Create an instance (variable) of the struct
    Student s1;

    // 3. Access members using the dot (.) operator
    s1.name = "Alice";
    s1.age = 20;
    s1.gpa = 3.9;

    cout << "Name: " << s1.name << " | GPA: " << s1.gpa << endl;

    return 0;
}

4. Arrays of Structures

If you have 100 students, you can create an array of Student structs.
cpp
1234567
Student classList[3];

classList[0].name = "Alice";
classList[0].gpa = 3.9;

classList[1].name = "Bob";
classList[1].gpa = 2.5;

5. Pointers to Structures (-> Operator)

If you have a pointer to a struct, you cannot use the dot operator directly. You must use the arrow operator (->).
cpp
1234567
Student s1;
s1.name = "Alice";

Student* ptr = &s1;

// Using the arrow operator to access a member through a pointer
cout << ptr->name << endl; // Prints "Alice"

6. Enumerations (enum)

An enum assigns names to integer constants to make a program easier to read and maintain.
cpp
123456789101112131415161718
#include <iostream>
using namespace std;

// By default, Monday=0, Tuesday=1, Wednesday=2...
enum Day { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday };

int main() {
    Day today = Wednesday;

    if (today == Wednesday) {
        cout << "It&#039;s the middle of the week!" << endl;
    }
    
    // Enums are basically integers under the hood
    cout << "Today is day number: " << today << endl; // Prints 2

    return 0;
}

7. Modern C++: Scoped Enums (enum class)

In traditional enums, if you have enum Color { Red } and enum Fruit { Red }, the compiler throws an error because the name Red clashes. Modern C++ fixes this with enum class.
cpp
1234
enum class Color { Red, Green, Blue };
enum class Fruit { Apple, Red, Banana }; // Perfectly valid!

Color carColor = Color::Red; // Must use the scope resolution operator ::

8. Mini Project: RPG Character Stats

cpp
1234567891011121314151617181920212223242526272829
#include <iostream>
#include <string>
using namespace std;

struct Stats {
    int health;
    int mana;
    int attack;
};

struct Character {
    string name;
    string jobClass;
    Stats attributes; // Nested Structure!
};

int main() {
    Character hero;
    hero.name = "Arthur";
    hero.jobClass = "Knight";
    hero.attributes.health = 100; // Accessing nested members
    hero.attributes.mana = 20;
    hero.attributes.attack = 35;

    cout << "Hero: " << hero.name << " (" << hero.jobClass << ")\n";
    cout << "HP: " << hero.attributes.health << " | ATK: " << hero.attributes.attack << endl;

    return 0;
}

9. Memory-Level Explanation

A struct's memory size is (roughly) the sum of its members. However, due to CPU architecture, compilers perform Memory Padding. If a struct has a 1-byte char and a 4-byte int, the size might be 8 bytes instead of 5 to align memory addresses for faster CPU access.

10. Common Mistakes

  • Forgetting the semicolon: Struct definitions must end with };. Forgetting this causes bizarre compiler errors on the next line.
  • Dot vs Arrow: Using ptr.name instead of ptr->name when dealing with pointers to structs.

11. Exercises

  1. 1. Define a Rectangle struct with width and height. Write a function that takes a Rectangle as a parameter and returns its area.
  1. 2. Define an enum class for TrafficLights (Red, Yellow, Green) and use it in a switch statement.

12. MCQ Quiz with Answers

Question 1

What keyword is used to create a structure?

Question 2

Which operator is used to access a struct's members from an instance?

Question 3

Which operator is used to access a struct's members through a POINTER?

Question 4

What is a common mistake when defining a struct?

Q5. Can a struct contain another struct as a member? a) Yes b) No Answer: a) Yes (Nested structures)
Question 6

What does an enum do?

Question 7

What is the default underlying integer value for the first item in an enum?

Question 8

Why use an enum class instead of a regular enum?

Question 9

If struct Point { int x; int y; };, what is the minimum memory size of an instance (assuming 4-byte ints)?

Q10. Can you create an array of structs? a) Yes b) No Answer: a) Yes

13. Interview Questions

  • Q: What is Structure Padding and Data Alignment in C++?
  • Q: Explain the difference between enum and enum class (Scoped Enums).
  • Q: In C++, is there any actual difference between a struct and a class? (Answer: Yes, the default access modifier for struct is public, while for class it is private. Otherwise, they are identical).

14. Summary

Structures allow you to group variables of different data types into a single, cohesive unit. This is the stepping stone to Object-Oriented Programming. Enumerations map integers to human-readable names, making code much easier to understand.

15. Next Chapter Recommendation

In Chapter 14: Object-Oriented Programming Basics, we will take the concept of a struct and supercharge it by adding functions directly inside the data structure, introducing you to Classes and Objects.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·