Skip to content

Structs

Before diving fully into classes, let’s take a quick look at structs in C++. A struct is a way to group related variables (often called members) under one name, making it easier to manage and pass around complex data. In many ways, structs are similar to classes—so similar, in fact, that the main difference is that struct members are public by default, whereas class members are private by default.

A struct in C++ is declared similarly to a class. Here’s the basic syntax:

struct StructureName {
// Data Members
// Member Functions
};

You can explicitly specify access specifiers—such as public, private, or protected—just as you would in a class. However, if you omit them, the contents of a struct default to public access:

struct Example {
public:
int x; // Public by default
void DoSomething() {
// Function implementation
}
private:
int y; // This member is explicitly private
};

You can then create an object of the struct to access its data members and functions. If you are using a structure or a reference to a structure, you use the dot operator to access its data and functions:

Example ex;
ex.DoSomething();
cout << ex.x << endl;

If you are using a pointer to a structure, then you use the arrow operator to access its data and functions:

Example* ptr;
ptr->DoSomething();
ptr->x;