# Structures¶

Cyganek section 3.9

A structure (or struct) is a compound datatype that can hold a mix of data. In C++, a struct shares many similarities to a class – both hold data as well as functions that work on the data (called members).

Consider the following:

struct Planet
{
std::string name{};
double a{};            // semi-major axis
double e{};            // eccentricity
};


If we create Planet object via:

Planet p;


Then we can access the different members of the struct using the “.” operator, e.g. p.name, p.a, and p.e.

This is a structure that can describe some basic properties of a planet, including its name, semi-major axis, and eccentricity of the orbit.

Let’s look at a program that stores the basic properties for the planets in our solar system and then loops over them and computes their orbital period using Kepler’s third law:

$\left ( \frac{P}{\mathrm{year}} \right )^2 = \left ( \frac{a}{\mathrm{AU}} \right )^3$
Listing 12 struct_example.cpp
#include <iostream>
#include <vector>
#include <cmath>

struct Planet
{
std::string name{};
double a{};            // semi-major axis
double e{};            // eccentricity
};

int main() {

const std::vector<Planet> planets {{"Mercury",  0.3871, 0.2056},
{"Venus",    0.7233, 0.0068},
{"Earth",    1.0000, 0.0167},
{"Mars",     1.5237, 0.0934},
{"Jupiter",  5.2029, 0.0484},
{"Saturn",   9.5370, 0.0539},
{"Uranus",  19.189,  0.0473},
{"Neptune", 30.070,  0.0086}};
for (auto p : planets) {
std::cout << p.name << " has a period of " << std::sqrt(std::pow(p.a, 3)) << " years" << std::endl;
}

}


Some notes:

• We put the defintion of Planet outside of the main() function.

• In the initialization of the planets vector, we use list-initialization for each of the planets in its own set of {}.

• Notice that our vector planets is const. This means that we cannot add to it (e.g., via .push_back()).

Tip

A common mistake is to forget the ; after the definition of the struct.

Note

As mentioned above, a class is very similar to a struct in C++. The main difference is that in a struct all of the members are publicly accessible while in a class, they are private unless we make them public.

Note

A common consideration when organizing data that is associated together is whether to do a struct-of-arrays or an array-of-structs. See this discussion on AoS and SoA.

In our planet example above, we created an array of structs. This is a natural organization if we want to loop over planets and access their data individually.