C++ Structures

A C++ structure, written with struct, is a user-defined type that groups related values under one name. Structures are often used for simple data objects where the members should be easy to read and write.

If you already know classes, structures will feel familiar. In C++, a struct can have member variables, member functions, constructors, and access specifiers. The main beginner-facing difference is the default access level.

Struct Syntax

A structure definition starts with struct, followed by the structure name and a body inside braces. Like a class definition, it must end with a semicolon.

#include <iostream>
#include <string>

struct Student {
    std::string name;
    int id;
    double grade;
};

int main(void) {
    Student student = {"Ava", 1042, 91.5};

    std::cout << student.name << std::endl;
    std::cout << student.id << std::endl;
    std::cout << student.grade << std::endl;

    return 0;
}

Output:

Ava
1042
91.5

The Student structure groups three related values: name, id, and grade. The variable student is one Student object. The dot operator, ., accesses each member.

Public By Default

Members of a struct are public by default. Members of a class are private by default. This is the key syntax difference most programmers rely on.

Because structure members are public by default, this is valid without writing public: inside the structure:

  • student.name = "Ava";
  • student.id = 1042;
  • student.grade = 91.5;

You can still write public, private, or protected inside a structure when you need to control access. For simple data grouping, public members are common and clear.

Aggregate Initialization

A simple structure can often be initialized with braces. This is called aggregate initialization. The values are assigned to members in the order the members are declared.

For Student student = {"Ava", 1042, 91.5};, the string goes into name, the integer goes into id, and the double goes into grade. If you change the member order in the structure, the initializer order must match the new order.

Structs Can Have Methods

A structure is not limited to plain data. It can also contain member functions, just like a class. This is useful when a small data object needs a calculation or helper behavior that naturally belongs with the data.

#include <iostream>

struct Rectangle {
    int width;
    int height;

    int area() const {
        return width * height;
    }

    bool isSquare() const {
        return width == height;
    }
};

int main(void) {
    Rectangle box = {6, 4};
    Rectangle tile = {5, 5};

    std::cout << "Box area: " << box.area() << std::endl;
    std::cout << "Box is square: " << std::boolalpha << box.isSquare() << std::endl;
    std::cout << "Tile area: " << tile.area() << std::endl;
    std::cout << "Tile is square: " << tile.isSquare() << std::endl;

    return 0;
}

Output:

Box area: 24
Box is square: false
Tile area: 25
Tile is square: true

The area() and isSquare() functions are member functions of Rectangle. They can read width and height directly because those values belong to the object that calls the method.

Struct Or Class?

In C++, struct and class are almost the same language feature. Use a struct when the type is mainly a small collection of related public data. Use a class when the type should protect its data behind methods and enforce rules about how the object is used.

For example, a Point, Color, or Rectangle may be a good structure. A BankAccount is usually better as a class because code should not freely change its balance without validation.

Common Mistakes

  • Forgetting the semicolon after the closing brace of the structure definition.
  • Putting brace initializer values in the wrong order.
  • Assuming struct means the type cannot have methods.
  • Using public structure members for data that should be protected by validation.

Takeaway: a C++ structure is best for grouping related data, and it can also include simple behavior when that behavior naturally belongs with the data.