Structures in C++


We often encounter situations where we need to keep a data group, whether they are the same data types or different data types. We saw similarities in C ++ used to store the same kinds of data in compact memory locations.

Unlike Arrays, Properties in C ++ are user-defined data types used to store a group of different data types.

What is a structure?

The structure is the type of data defined by the user in C / C ++. The structure creates a kind of data that can collect objects of different types in one form. Properties in C ++ may contain two types of members:

Data Member: These are the most common members of the C ++ variable. We can build a structure with various types of data in C ++.

Member functions: These members are standard C ++ functions. We can also include functions within a property announcement in line with the flexibility.

Syntax

struct structure_name{ member1; member2; member3; . . . memberN; };

Example:

#include <iostream> using namespace std; struct Point { int x, y; }; int main() { // Create an array of structures struct Point arr; // Access array members arr.x = 10; arr.y = 20; cout << arr.x << " " << arr.y; return 0; }

OUTPUT:

string type