C++ Structures (struct)

Introduction

Structures in C++ (also known as structs) are user-defined data types that allow the grouping of variables of different types under a single name. This is particularly useful for representing complex data models that consist of multiple related pieces of information. Understanding how to use structures is crucial for organizing and managing data efficiently in your programs.

Defining and Declaring Structures

Syntax for Defining a Structure

struct StructureName {
    dataType1 member1;
    dataType2 member2;
    // additional members
};

Example: Defining and Declaring a Structure

#include <iostream>
using namespace std;

// Define a structure named 'Person'
struct Person {
    string name;
    int age;
    float height;
};

int main() {
    // Declare a structure variable
    Person person1;

    // Initialize structure members
    person1.name = "John Doe";
    person1.age = 30;
    person1.height = 5.9;

    // Access and print structure members
    cout << "Name: " << person1.name << endl;
    cout << "Age: " << person1.age << endl;
    cout << "Height: " << person1.height << endl;

    return 0;
}

Output

Name: John Doe
Age: 30
Height: 5.9

Explanation

  • struct Person defines a structure named Person with three members: name, age, and height.
  • Person person1; declares a variable of type Person.
  • The members of person1 are initialized and accessed using the dot operator (.).

Initializing Structures

Structures can be initialized at the time of declaration.

Example: Initializing a Structure

#include <iostream>
using namespace std;

struct Person {
    string name;
    int age;
    float height;
};

int main() {
    // Initialize a structure variable
    Person person1 = {"John Doe", 30, 5.9};

    // Access and print structure members
    cout << "Name: " << person1.name << endl;
    cout << "Age: " << person1.age << endl;
    cout << "Height: " << person1.height << endl;

    return 0;
}

Output

Name: John Doe
Age: 30
Height: 5.9

Explanation

  • Person person1 = {"John Doe", 30, 5.9}; initializes the structure members at the time of declaration.

Nested Structures

Structures can contain other structures as members, allowing for more complex data models.

Example: Nested Structures

#include <iostream>
using namespace std;

// Define a structure named 'Address'
struct Address {
    string street;
    string city;
    int zipCode;
};

// Define a structure named 'Person'
struct Person {
    string name;
    int age;
    float height;
    Address address; // Nested structure
};

int main() {
    // Initialize a structure variable
    Person person1 = {"John Doe", 30, 5.9, {"123 Main St", "Springfield", 12345}};

    // Access and print structure members
    cout << "Name: " << person1.name << endl;
    cout << "Age: " << person1.age << endl;
    cout << "Height: " << person1.height << endl;
    cout << "Address: " << person1.address.street << ", " << person1.address.city << ", " << person1.address.zipCode << endl;

    return 0;
}

Output

Name: John Doe
Age: 30
Height: 5.9
Address: 123 Main St, Springfield, 12345

Explanation

  • struct Address defines a structure named Address with three members: street, city, and zipCode.
  • struct Person defines a structure named Person with an Address structure as a member.
  • The person1 structure variable is initialized, including the nested Address structure.

Arrays of Structures

You can create arrays of structures to manage multiple instances of a structure.

Example: Arrays of Structures

#include <iostream>
using namespace std;

struct Person {
    string name;
    int age;
    float height;
};

int main() {
    // Declare and initialize an array of structures
    Person people[2] = {{"John Doe", 30, 5.9}, {"Jane Smith", 25, 5.7}};

    // Access and print the members of the structures in the array
    for (int i = 0; i < 2; i++) {
        cout << "Person " << i + 1 << ": " << endl;
        cout << "  Name: " << people[i].name << endl;
        cout << "  Age: " << people[i].age << endl;
        cout << "  Height: " << people[i].height << endl;
    }

    return 0;
}

Output

Person 1:
  Name: John Doe
  Age: 30
  Height: 5.9
Person 2:
  Name: Jane Smith
  Age: 25
  Height: 5.7

Explanation

  • Person people[2] declares an array of Person structures.
  • The array is initialized with two Person structures.
  • A for loop is used to iterate through the array and print the members of each structure.

Example Programs

Example 1: Student Structure

This example demonstrates defining a Student structure and printing student details.

#include <iostream>
using namespace std;

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

int main() {
    // Initialize a structure variable
    Student student1 = {"Alice", 101, 95.5};

    // Access and print structure members
    cout << "Name: " << student1.name << endl;
    cout << "Roll Number: " << student1.rollNumber << endl;
    cout << "Marks: " << student1.marks << endl;

    return 0;
}

Output

Name: Alice
Roll Number: 101
Marks: 95.5

Explanation

  • The program defines a Student structure with members name, rollNumber, and marks.
  • The student1 structure variable is initialized and its members are printed.

Example 2: Book Structure with Nested Author Structure

This example demonstrates defining a Book structure with a nested Author structure and printing book details.

#include <iostream>
using namespace std;

// Define a structure named 'Author'
struct Author {
    string name;
    int age;
};

// Define a structure named 'Book'
struct Book {
    string title;
    float price;
    Author author; // Nested structure
};

int main() {
    // Initialize a structure variable
    Book book1 = {"C++ Programming", 29.99, {"John Doe", 45}};

    // Access and print structure members
    cout << "Title: " << book1.title << endl;
    cout << "Price: " << book1.price << endl;
    cout << "Author: " << book1.author.name << endl;
    cout << "Author Age: " << book1.author.age << endl;

    return 0;
}

Output

Title: C++ Programming
Price: 29.99
Author: John Doe
Author Age: 45

Explanation

  • The program defines an Author structure and a Book structure with an Author structure as a member.
  • The book1 structure variable is initialized, including the nested Author structure.
  • The members of the book1 structure are printed.

Conclusion

Structures in C++ allow you to group variables of different types under a single name, making it easier to manage and organize complex data. This chapter covered how to define, declare, and initialize structures, including nested structures and arrays of structures. It also provided example programs to demonstrate the use of structures in representing and managing data. Understanding how to use structures effectively will help you build more organized and maintainable programs. In the next chapter, we will explore unions in C++.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top