techmore.in

C++ Structures

In C++, structures (or struct) are user-defined data types that allow you to group different types of variables under a single name. They are used to represent a collection of related data in a meaningful way.

Defining a Structure

To define a structure, use the struct keyword followed by the structure name and its members (variables) enclosed in curly braces {}.

Basic Syntax of a Structure

cpp
struct StructureName { data_type1 member1; data_type2 member2; // other members };
  • StructureName: The name of the structure.
  • member1, member2: The data members (variables) of the structure.

Example of Defining and Using a Structure

cpp
#include using namespace std; // Defining a structure for a 'Person' struct Person { string name; int age; float height; }; int main() { // Creating a structure variable Person person1; // Assigning values to the structure members person1.name = "John"; person1.age = 30; person1.height = 5.9; // Accessing and displaying structure members cout << "Name: " << person1.name << endl; cout << "Age: " << person1.age << endl; cout << "Height: " << person1.height << " feet" << endl; return 0; }

Explanation:

  • The structure Person has three members: name (string), age (int), and height (float).
  • In the main() function, a variable person1 of type Person is created.
  • The values for person1's members are assigned, and they are accessed using the dot (.) operator.

Initializing a Structure

You can initialize a structure in two ways:

Method 1: Using Dot Operator

You can initialize members after defining the structure variable:

cpp
person1.name = "Alice"; person1.age = 25; person1.height = 5.5;

Method 2: Using Initialization List (C++11 and later)

cpp
Person person2 = {"Alice", 25, 5.5};

This directly initializes the members in the order they are declared in the structure.

Array of Structures

You can create an array of structures to store multiple records of the same type.

cpp
#include using namespace std; struct Person { string name; int age; float height; }; int main() { // Array of structures Person people[2] = { {"John", 30, 5.9}, {"Alice", 25, 5.5} }; // Accessing array elements for (int i = 0; i < 2; i++) { cout << "Name: " << people[i].name << ", Age: " << people[i].age << ", Height: " << people[i].height << " feet" << endl; } return 0; }

Nested Structures

Structures can also contain other structures as members. This is known as nested structures.

cpp
#include using namespace std; struct Address { string city; string state; int zip; }; struct Person { string name; int age; Address addr; // Nested structure }; int main() { Person person = {"John", 30, {"New York", "NY", 10001}}; // Accessing nested structure members cout << "Name: " << person.name << endl; cout << "Age: " << person.age << endl; cout << "City: " << person.addr.city << endl; cout << "State: " << person.addr.state << endl; cout << "ZIP: " << person.addr.zip << endl; return 0; }

Pointers to Structures

You can create pointers to structures and access their members using the -> operator.

cpp
#include using namespace std; struct Person { string name; int age; float height; }; int main() { Person person = {"John", 30, 5.9}; // Pointer to structure Person* ptr = &person; // Accessing structure members using pointer cout << "Name: " << ptr->name << endl; cout << "Age: " << ptr->age << endl; cout << "Height: " << ptr->height << " feet" << endl; return 0; }

Structures vs. Classes

In C++, both struct and class are used to define user-defined data types. The key difference between them is:

  • By default, all members of a struct are public.
  • By default, all members of a class are private.

Other than this difference, struct and class are almost the same in C++.

Example:

cpp
struct Person { string name; // Public by default int age; };
cpp
class Person { public: // Must specify 'public' string name; int age; };

Key Points:

  1. Structure is a user-defined data type in C++.
  2. Members of a structure can be of different data types.
  3. Structures allow grouping related variables under a single name.
  4. You can create arrays of structures, nested structures, and pointers to structures.
  5. Structures and classes are similar in C++, except for the default access specifiers.

Structures in C++ are a powerful tool for organizing and managing related data in an efficient and meaningful way.