C++ Structures

In C++, a structure is a user-defined data type that allows you to group together multiple data types in a single variable. Structures are useful for organizing data that belongs together and for creating complex data types that can be used in various parts of your program.

Declaring Structures

To declare a structure, you use the keyword “struct” followed by the structure’s name and a set of curly braces {}. Inside the curly braces, you define the variables or fields that make up the structure. Here is an example of a structure that contains two variables:

struct Person {
    string name;
    int age;
};

Creating Structure Variables

Once a structure has been declared, you can create variables of that structure type. To create a variable of a structure, you use the name of the structure followed by the variable name, just as you would with any other data type. Here is an example of creating a variable of the Person structure declared above:

Person person1;

Accessing Structure Elements

To access the elements of a structure, you use the dot operator (.) followed by the name of the element you want to access. Here is an example of setting and accessing the elements of the Person structure:

Person person1;
person1.name = "John Doe";
person1.age = 30;
cout << person1.name << " is " << person1.age << " years old.";

Output:

John Doe is 30 years old.

Initializing Structures

You can also initialize a structure variable at the time of declaration. To do this, you use the same syntax as when creating a variable, but with the variable’s fields enclosed in curly braces {} and separated by commas. Here is an example of initializing a Person structure:

Person person1 = {"John Doe", 30};
cout << person1.name << " is " << person1.age << " years old.";

Output:

John Doe is 30 years old.

Passing Structures to Functions

You can pass a structure variable to a function just as you would with any other variable. The function receives a copy of the structure, so changes made to the structure inside the function will not affect the original structure. Here is an example of passing a structure to a function and modifying its elements:

void increaseAge(Person &p) {
    p.age++;
}

Person person1 = {"John Doe", 30};
increaseAge(person1);
cout << person1.name << " is " << person1.age << " years old.";

Output:

John Doe is 31 years old.

Structures and Classes

In C++, structures are similar to classes, but there are some key differences. One of the main differences is that members of a structure are public by default, while members of a class are private by default. This means that the elements of a structure can be directly accessed and modified, while elements of a class need to be accessed through accessor functions such as getters and setters.
Another difference is that structure does not support inheritance, encapsulation and polymorphism like class does.

In general, structures are used for simple data structures that do not need the additional functionality that classes provide, such as encapsulation and inheritance. Classes, on the other hand, are used for more complex data structures that require the added functionality and security provided by encapsulation and inheritance.

Conclusion

In this article, we have covered the basics of C++ structures. We have discussed how to declare, create, access, and initialize structure variables, as well as how to pass structures to functions. We have also compared structures to classes and discussed when to use each. Understanding the basics of structures is essential for creating complex data types and organizing data in a logical and efficient manner in C++.

Leave a Reply

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