how to create a structure c++

To create a structure in C++, you need to follow these steps:

  1. Define the structure: Start by defining the structure using the struct keyword, followed by the name of the structure. Inside the structure, you can declare variables of different data types that represent the different attributes or properties of the structure. Here's an example:
struct Person {
    int age;
    std::string name;
    float height;
};

In this example, we define a structure named Person with three attributes: age (an integer), name (a string), and height (a float).

  1. Declare structure variables: After defining the structure, you can declare variables of that structure type. This allows you to create multiple instances of the structure. Here's an example:
Person person1;
Person person2;

In this example, we declare two variables person1 and person2 of type Person.

  1. Accessing structure members: To access the individual members of a structure, you use the dot operator (.) followed by the member name. Here's an example:
person1.age = 25;
person1.name = "John Doe";
person1.height = 5.11;

In this example, we assign values to the members of person1.

  1. Using structure variables: Once you have declared and assigned values to the structure variables, you can use them just like any other variables in your program. Here's an example:
std::cout << "Name: " << person1.name << std::endl;
std::cout << "Age: " << person1.age << std::endl;
std::cout << "Height: " << person1.height << std::endl;

In this example, we print the values of the members of person1.

That's it! You have successfully created a structure in C++ and used it to store and access related data.