codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
⚡

C++ Programming

28 / 87 topics
28Structures (struct)29Structure and Function30Pointers to Structure31Enumerations (enum)32Unions
Tutorials/C++ Programming/Structures (struct)
⚡C++ Programming

Structures (struct)

Updated 2026-05-12
30 min read

Structures (struct)

In this tutorial, we'll explore the concept of structures in C++, which are a powerful way to group related data together. Understanding how to define and use structures is crucial for organizing complex data models in your programs.

Introduction

Structures allow you to create composite data types that can contain multiple variables (members) of different types. This makes it easier to manage related data as a single unit, similar to how you might organize your belongings into different drawers or boxes. Structures are especially useful when dealing with real-world entities that have multiple attributes.

Defining a Structure

To define a structure in C++, you use the struct keyword followed by the structure's name and a set of curly braces {} containing its members. Here's a simple example:

define_struct.cpp
1#include <iostream>
2using namespace std;
3
4// Define a struct named 'Car'
5struct Car {
6 string make;
7 string model;
8 int year;
9};
10
11int main() {
12 // Create a variable of type 'Car'
13 Car myCar;
14
15 // Assign values to the members
16 myCar.make = "Toyota";
17 myCar.model = "Corolla";
18 myCar.year = 2020;
19
20 // Output the member values
21 cout << "Make: " << myCar.make << endl;
22 cout << "Model: " << myCar.model << endl;
23 cout << "Year: " << myCar.year << endl;
24
25 return 0;
26}
Output
Make: Toyota
Model: Corolla
Year: 2020

In this example, we defined a structure named Car with three members: make, model, and year. We then created an instance of the Car structure called myCar and assigned values to its members. Finally, we printed out these values.

Accessing Members

You can access the members of a structure using the dot operator (.). The dot operator is used to specify which member you want to access from a particular structure variable.

access_members.cpp
1#include <iostream>
2using namespace std;
3
4struct Car {
5 string make;
6 string model;
7 int year;
8};
9
10int main() {
11 Car myCar = {"Honda", "Civic", 2018};
12
13 cout << "Make: " << myCar.make << endl;
14 cout << "Model: " << myCar.model << endl;
15 cout << "Year: " << myCar.year << endl;
16
17 return 0;
18}
Output
Make: Honda
Model: Civic
Year: 2018

In this example, we initialized myCar with values directly in the declaration and accessed its members using the dot operator.

Array of Structures

You can also create an array of structures, which is useful for managing multiple instances of a structured data type. Here's how you can do it:

array_of_structs.cpp
1#include <iostream>
2using namespace std;
3
4struct Car {
5 string make;
6 string model;
7 int year;
8};
9
10int main() {
11 // Create an array of 3 Car structures
12 Car cars[3] = {
13 {"Ford", "Mustang", 2019},
14 {"Chevrolet", "Camaro", 2020},
15 {"Tesla", "Model S", 2021}
16 };
17
18 // Loop through the array and print each car's details
19 for (int i = 0; i < 3; ++i) {
20 cout << "Car " << i + 1 << ": " << cars[i].make << ", "
21 << cars[i].model << ", " << cars[i].year << endl;
22 }
23
24 return 0;
25}
Output
Car 1: Ford, Mustang, 2019
Car 2: Chevrolet, Camaro, 2020
Car 3: Tesla, Model S, 2021

In this example, we created an array of Car structures and initialized it with three different cars. We then used a loop to iterate through the array and print out the details of each car.

Nested Structures

Structures can also contain other structures as members. This is known as nested structures and is useful for modeling complex relationships between data types.

nested_structs.cpp
1#include <iostream>
2using namespace std;
3
4// Define a struct for Address
5struct Address {
6 string street;
7 string city;
8 string state;
9 int zip;
10};
11
12// Define a struct for Person that includes an Address
13struct Person {
14 string name;
15 int age;
16 Address address; // Nested structure
17};
18
19int main() {
20 // Create a Person and initialize its members, including the nested Address
21 Person person1 = {
22 "John Doe",
23 30,
24 {"123 Elm St", "Springfield", "IL", 62704}
25 };
26
27 cout << "Name: " << person1.name << endl;
28 cout << "Age: " << person1.age << endl;
29 cout << "Address: " << person1.address.street << ", "
30 << person1.address.city << ", "
31 << person1.address.state << ", "
32 << person1.address.zip << endl;
33
34 return 0;
35}
Output
Name: John Doe
Age: 30
Address: 123 Elm St, Springfield, IL, 62704

In this example, we defined a nested structure where the Person struct contains an Address struct as one of its members. We then created a Person instance and initialized all its members, including the nested Address.

Struct vs Class

While structures and classes in C++ are similar, there are some key differences:

  • Access Control: By default, members of a structure are public, whereas members of a class are private.
  • Inheritance: Classes support inheritance by default, while structures do not (though you can enable it using the class keyword with struct).
  • Usage: Structures are typically used for passive data storage, while classes are used to encapsulate both data and functions that operate on that data.

Here's a comparison table:

Featurestructclass
Default AccessPublicPrivate
InheritanceNo (unless specified)Yes
PurposePassive data storageActive data and functions
struct_vs_class.cpp
1#include <iostream>
2using namespace std;
3
4// Define a struct
5struct Point {
6 int x;
7 int y;
8};
9
10// Define a class
11class Rectangle {
12private:
13 int width;
14 int height;
15public:
16 void setDimensions(int w, int h) {
17 width = w;
18 height = h;
19 }
20 int area() {
21 return width * height;
22 }
23};
24
25int main() {
26 Point p1;
27 p1.x = 3;
28 p1.y = 4;
29
30 cout << "Point: (" << p1.x << ", " << p1.y << ")" << endl;
31
32 Rectangle rect;
33 rect.setDimensions(5, 6);
34 cout << "Area of rectangle: " << rect.area() << endl;
35
36 return 0;
37}
Output
Point: (3, 4)
Area of rectangle: 30

In this example, we defined a Point structure and a Rectangle class. The Point struct has public members, while the Rectangle class has private members that can only be accessed through public methods.

Practical Example

Let's create a practical example where we manage a list of employees using structures. Each employee will have a name, ID, and department.

employee_management.cpp
1#include <iostream>
2using namespace std;
3
4// Define an Employee struct
5struct Employee {
6 string name;
7 int id;
8 string department;
9};
10
11int main() {
12 // Create an array of 3 employees
13 Employee employees[3] = {
14 {"Alice", 101, "HR"},
15 {"Bob", 102, "Engineering"},
16 {"Charlie", 103, "Marketing"}
17 };
18
19 // Loop through the array and print each employee's details
20 for (int i = 0; i < 3; ++i) {
21 cout << "Employee " << i + 1 << ": Name - " << employees[i].name
22 << ", ID - " << employees[i].id
23 << ", Department - " << employees[i].department << endl;
24 }
25
26 return 0;
27}
Output
Employee 1: Name - Alice, ID - 101, Department - HR
Employee 2: Name - Bob, ID - 102, Department - Engineering
Employee 3: Name - Charlie, ID - 103, Department - Marketing

In this example, we defined an Employee structure and created an array of three employees. We then used a loop to print out the details of each employee.

Summary

  • Structures in C++ are user-defined data types that group related variables together.
  • You define a structure using the struct keyword followed by its name and members.
  • Access members of a structure using the dot operator (.).
  • Structures can contain arrays, nested structures, and even other classes.
  • Structures vs. classes: default access control, inheritance, and usage differ.

What's Next?

Now that you understand how to define and use structures in C++, the next step is to learn about Structure and Function. This will help you write more modular and reusable code by passing structures as arguments to functions and returning them from functions.


PreviousStrings & the C++ String ClassNext Structure and Function

Recommended Gear

Strings & the C++ String ClassStructure and Function