A Quick Guide to Structs in C++

Introduction

If you’re new to C++ you may have wondered why C++ has both classes and these weird things called structs. The difference between a class and a struct is that an instance of a struct is a variable, while an instance of a class is an object. You can think of a struct as just another data structure like an array. The only difference is that you are making the data structure yourself this time. Structs are generally used if you plan on keeping the instance variables within in it constant. Another important difference between structs and classes is that classes are private by default, while structs are public by default. This means that structs are generally less secure. In this blog I’m going to give you a quick example of how you’d make a struct, so that you can start using them in your C++ programs.

Defining a Struct

Let’s say I am making a video game and I want to make a custom data structure that contains all the information about the player. Of course I could use a class to do so, but for the sake of this tutorial we are going to use a struct. To define our struct we will need the keyword struct followed by the name we want our struct to have like so:

After this you’re going to want to give your struct some instance variables. These can be anything from floats to strings, and you create these to hold information about your new player. So if I create a new player they will be defined with these properties:

You can also give your struct different functions it can perform like so:

Here I am making two different functions. One is TakeDamage() which changes the health of the player, and the other is GetLevel() that returns the level of the player and outputs something if it is greater than 10.

Creating An Instance Of Your Struct

In order to use the struct we have just created we will need to create an instance of it. To create an instance of the struct we will first need to name it, and then we will need to include values for all of its instance variables. In our case we will need to create values for Level, Health, Damage and Stamina like so:

Here I am defining a new Player struct called “ryan”, with all of the values I’d like it to have. So this player’s level will be an int of 11, it’s health a float of 100 etc.

Using Your Struct

Now that we have an instance of our struct we can retrieve information from it, and more importantly we can call its functions. For example, if we wanted to output ryan’s level we could call GetLevel() on it like so:

This will output ryan’s level and tell us that its level is over 10.

Conclusion

Congrats you have just created your first struct! I hope that this blog has helped clarify what a struct is and how to make one. Thanks for reading!

Sources:

https://www.geeksforgeeks.org/structure-vs-class-in-cpp/

Full-Stack Engineer with a background in Javascript, React.js, React Native, Redux, Ruby, and Ruby on Rails.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store