Structs(Derived Data Types)

Data types give us a provision to store data in a more structured way.

Need for Structs(Derived Data Types):

In C++, the language provides primitive data types where we can store the most common form of data and do operations on the same in order to carry out our tasks involving common and small problems like area/volume, square/cube, calculation etc.

But as we try to solve bigger problems which may involve complex tasks like calculation BMI of a person, calculating age of a person, then it becomes more easier of we have our own custom data types where we can have the freedom to create our own way to store data.

This new way is defined as structures and these are built on top of our primitive data types. That's the reason they are known as derived data types.

In C++, a structure (commonly known as struct) is a user-defined data type that allows you to group variables of different types under a single name. Structures are used to represent a record, where each element of the structure can be of a different type, much like a class but with some differences in behavior and usage.

Defining a Structure

A structure is defined using the struct keyword followed by the structure name and a block of code that defines its members.

Basic Structure Syntax

struct StructureName {
    // Members of the structure
    DataType member1;
    DataType member2;
    // ...
};

Example

Here’s a simple example that defines a structure to represent a Person:

Key Points About Structures:

  1. Members:

    • A structure can have multiple members, and each member can be of a different type.

    • Members are variables that are defined within the structure.

  2. Accessing Members:

    • Members of a structure are accessed using the dot (.) operator.

    • If you have a pointer to a structure, you use the arrow (->) operator to access members.

  3. Initialization:

    • You can initialize the members of a structure individually as shown in the example.

    • You can also use aggregate initialization:

  4. Comparison with Classes:

    • By default, all members of a struct are public. This is the key difference between a struct and a class, where members are private by default.

    • Structs in C++ can have member functions, constructors, destructors, and can be used with inheritance, much like classes.

  5. Memory Layout:

    • The memory layout of a structure is sequential, meaning that the members are stored in the order they are declared. However, the compiler might introduce padding between members to align data properly in memory.

  6. Size and Alignment:

    • The size of a structure is the total size of all its members, including any padding introduced by the compiler.

Advanced Usage

  • Structures and Functions: Structures can be passed to functions by value or by reference.

  • Arrays of Structures: You can create arrays of structures, which allows you to store multiple records of the same type.

  • Nested Structures: Structures can contain other structures as members.

Summary

Structures in C++ are powerful constructs that allow you to group different data types together under one name. They are particularly useful for modeling real-world entities like records, objects, and other composite data types. Structures support many of the features provided by classes, but their members are public by default. They play a crucial role in organizing and managing data efficiently in C++ programs.

Last updated