Nested structures in C++

Nested structures in C++ allow you to define a structure inside another structure, which can be useful for organizing and representing complex data. Here’s an example of how to define and use nested structures in C++:

#include 
#include 

using namespace std;

struct Address {
    string street;
    string city;
    string state;
    string zip;
};

struct Person {
    string name;
    int age;
    Address address;
};

int main() {
    Person p = {"John Smith", 30, {"123 Main St", "Anytown", "CA", "12345"}};

    cout << "Name: " << p.name << endl;
    cout << "Age: " << p.age << endl;
    cout << "Address: " << p.address.street << ", " << p.address.city << ", " << p.address.state << " " << p.address.zip << endl;

    return 0;
}

In this example, two structures are defined: `Address` and `Person`. The `Address` structure contains members for a street, city, state, and zip code. The `Person` structure contains members for a name, age, and an `Address` structure nested inside it. In the `main()` function, a `Person` variable `p` is declared and initialized with values for all the members, including an `Address` structure nested inside it. The values of the `Person` members are then outputted using`cout`, along with the values of the nested `Address` structure members.

Nested structures can be useful for representing complex objects or data structures, such as a person with an address or a car with different parts. By organizing data into nested structures, you can make your code more modular and easier to read and maintain. Additionally, structures can be used in conjunction with other C++ concepts like pointers and arrays to create even more complex data structures.