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
Astruct is a blueprint. It doesn't use memory until you actually create a variable (an instance) of that type.
cpp
4. Arrays of Structures
If you have 100 students, you can create an array ofStudent structs.
cpp
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
6. Enumerations (enum)
An enum assigns names to integer constants to make a program easier to read and maintain.
cpp
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
8. Mini Project: RPG Character Stats
cpp
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-bytechar 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.nameinstead ofptr->namewhen dealing with pointers to structs.
11. Exercises
-
1.
Define a
Rectanglestruct withwidthandheight. Write a function that takes aRectangleas a parameter and returns its area.
-
2.
Define an
enum classfor 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?
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)?
13. Interview Questions
- Q: What is Structure Padding and Data Alignment in C++?
-
Q: Explain the difference between
enumandenum class(Scoped Enums).
-
Q: In C++, is there any actual difference between a
structand aclass? (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 astruct and supercharge it by adding functions directly inside the data structure, introducing you to Classes and Objects.