struct objects
An object in C++ is basically any variable type that is made up of a conglomerate of simpler types. The most basic object in C++ is struct
. We use the struct
keyword to glue together a bunch of smaller variables into one big variable. If you recall, we did introduce struct
briefly in Chapter 2, Variables and Memory. Let's revise that simple example:
struct Player { string name; int hp; };
This is the structure definition for what makes a Player
object. The player has a string
for his name
and an integer for his hp
value.
If you'll recall from Chapter 2, Variables and Memory, the way we make an instance of the Player
object is like this:
Player me; // create an instance of Player, called me
From here, we can access the fields of the me
object like so:
me.name = "Tom"; me.hp = 100;
Member functions
Now, here's the exciting part. We can attach member functions to the struct
definition simply by writing these functions inside the struct Player
definition...