Message Objects
To play around with the idea of member functions, let’s build a simple text message object. A message object will contain some text which it can print to the terminal.
In main we can then create a few text message objects, and get them to print their details to the terminal.
Creating your struct
To start this program, you will need to declare the struct, and code within this the two fields as you would have before. You now have the struct and its data, which we can extend with the two constructors and the print method.
We will provide two constructors in the struct, one that will accept parameters for the text and sender and initialise these values within the struct and a default constructor that will initialise both the text and sender to empty strings. When implementing these remember that the constructor is a special method that has the same name as the struct.
Lastly, add the print
method. This should output the message in any format you desire, as long as it shows the sender and the text. As this is coded within the method, it will have access to sender and text which are also coded within the struct.
Testing your struct
Now we can add in a main to test out this new code. Try creating different message objects, some on the stack and some on the heap (remember to delete them). Get them to print out their messages. For example, the following is part of the test I created for this:
When you get this working, focus on the mechanics of how this comes together. Play around with using this->
within the struct. Try changing the sender or text of a message and printing it again.