Member Functions Overview
So far we have worked with instructions and data as separate aspects within our programs. Instructions have been organised into functions and procedures, while data is structured mainly using structs.
Structs are great, as they provide a context when accessing fields. When you have a variable of the struct’s type, it has all the things for that particular thing in the one place.
What would happen if we could add functions and procedures into the struct?
This is what we will explore in this chapter. We will use these ideas to enhance our dynamic array, giving each array the data and functionality to needed to make them work smoothly within our programs. We will also see how this can change the way that we code the entities in our programs.
The results of this will feed into the next part, which will unpack the object-oriented paradigm that grew out of this simple idea of combining functionality and data within a single code abstraction.
Learning Goals
By the end of this chapter you will be able to:
- Describe how member functions allow you to create entities that know and can do things
- Create and use structs that include constructors and methods as well as fields