Bold Aesthetic Creative Games

An Indie Game Studio Developing Bold, Aesthetic & Creative Video Games

C++ Programming Series: Structures and Data Types

C++ Programming Series: Structures and Data Types

During that non-blogging time, I learned one thing: Always finish what you have started.

And so, here I am continuing my C++ Programming Series.

C++ got primitive data types like integers, floats and doubles. They are called primitive for a reason.

We can create our own data types by combining other data types. We can also include an array/pointer. For example:

struct Vehicle
    int tires;
    string color;
    int horsePower;
    float weight;

We have created a structure/data type called Vehicle. Note that string is not a primitive data type.

Now, using it is pretty simple.

First, you need to create an object out of it.

Vehicle car; = 4;
car.color = "black";
car.horsePower = 1200;
car.weight = 804.6; //in kilograms

As you can tell, we created a definition and then, set the value of each component variable within our own defined type variable.

To access the components in our variable, we used full stop(dot) immediately after the name of it.

Why use structures when we can simply make variables and use them? When we make structures out of variables, it makes our code cleaner and easier to read. The more we make abstractions out of our variables, the easier it is to work with the code and the more our code will have a high-level paradigm.

We can create functions to easily manipulate the structures. We don’t need to pass all components of the structure variable. Instead, we can simply pass the variable itself and then, open it inside the function. Example below:

void setVehicle( Vehicle& vehicle, int t, string c, int hp, float w )
{ = t;
    vehicle.color = c;
    vehicle.horsePower = hp;
    vehicle.weight = w;

When you look at our Vehicle datatype, it is pretty basic and there is no practical use to it. But what if it is something like this:

struct Vehicle
    const int MAX_TIRES = 20;
    Tire tires[MAX_TIRES];
    Exterior exterior;
    Engine engine;

…where the Tire, Exterior and Engine are also some complex user-defined data types. Maybe, those are also simplified just like Vehicle and contain some other user-defined data types.

My point is that things can get very simple if data structures are made and used within each other and they have separate functions for their manipulation as well.

I hope this is good enough to push you ahead into using structures efficiently.

Leave a Reply

Your email address will not be published. Required fields are marked *