Language: EN

cpp-structs

What are `structs` in C++ and how to use them

In C++, a struct is a data structure that allows grouping several variables under a single name.

structs are particularly useful when working with simple data types that have a predictable size and are mainly used to represent data that needs to be grouped.

In C++, a struct is practically the same as a class
The only difference is the default visibility, which is public in structs and private in classes.

Syntax of structs

The basic syntax for defining a structure in C++ is as follows:

struct StructureName {
    // Field definitions
    DataType Field1;
    DataType Field2;

    // Method, property definitions, etc.
};
  • StructureName: Is the unique name given to the structure.
  • DataType: Specifies the data type of the fields within the structure.

Basic Example

Here is a basic example of how to define and use a structure in C++:

#include <iostream>

struct Point {
    int X;
    int Y;

    Point(int x, int y) : X(x), Y(y) {}

    void PrintCoordinates() const {
        std::cout << "Coordinates: (" << X << ", " << Y << ")" << std::endl;
    }
};

int main() {
    Point point(3, 5);
    point.PrintCoordinates(); // Prints "Coordinates: (3, 5)"
    return 0;
}

Using structs

Declaration and Initialization

Structures are declared and initialized similarly to variables of other data types.

Point point;
point.X = 3;
point.Y = 5;

// Initialization using the constructor
Point anotherPoint(7, 8);

Accessing Fields

The fields of a structure are accessed using the dot notation (.).

int x = point.X;
int y = point.Y;

Practical Examples

Representation of a point in a Cartesian plane

In this example, a structure is defined that represents a point in a Cartesian plane.

#include <iostream>

struct Point {
    int X; // X coordinate of the point
    int Y; // Y coordinate of the point

    Point(int x, int y) : X(x), Y(y) {}
};

int main() {
    Point point(3, 4);
    std::cout << "Point: (" << point.X << ", " << point.Y << ")" << std::endl; // Prints "Point: (3, 4)"
    return 0;
}

Representation of an RGB color

In this example, a structure is defined that represents a color in RGB format.

#include <iostream>

struct ColorRGB {
    unsigned char Red; // Red component of the color
    unsigned char Green; // Green component of the color
    unsigned char Blue; // Blue component of the color

    ColorRGB(unsigned char red, unsigned char green, unsigned char blue)
        : Red(red), Green(green), Blue(blue) {}
};

int main() {
    ColorRGB color(255, 0, 0); // Red color
    std::cout << "RGB Color: (" << (int)color.Red << ", " << (int)color.Green << ", " << (int)color.Blue << ")" << std::endl; // Prints "RGB Color: (255, 0, 0)"
    return 0;
}

Representation of a date

In this example, a structure is defined that represents a date.

#include <iostream>

struct Date {
    int Day; // Day of the month
    int Month; // Month of the year
    int Year; // Year

    Date(int day, int month, int year) : Day(day), Month(month), Year(year) {}
};

int main() {
    Date date(5, 6, 2023);
    std::cout << "Date: " << date.Day << "/" << date.Month << "/" << date.Year << std::endl; // Prints "Date: 5/6/2023"
    return 0;
}

Representation of a rectangle

In this example, a structure is defined that represents a rectangle and includes a method to calculate its area.

#include <iostream>

struct Rectangle {
    int Width; // Width of the rectangle
    int Height; // Height of the rectangle

    Rectangle(int width, int height) : Width(width), Height(height) {}

    int CalculateArea() const {
        return Width * Height;
    }
};

int main() {
    Rectangle rectangle(5, 10);
    int area = rectangle.CalculateArea();
    std::cout << "Area of the rectangle: " << area << std::endl; // Prints "Area of the rectangle: 50"
    return 0;
}