Member functions are written just like any other functions.
One difference, though, is they can use the data member names as is.
Thus, the member functions in
ch_stack
use
top
and
s
in an unqualified manner.
When
invoked on a particular object of type
ch_stack
, the member functions act on a specified member of that object.
Member functions that are defined within the
struct
are implicitly inline.
As a rule, only short, heavily used member functions should be defined with the
struct
.
Other member functions should be defined external to the
struct
. We will look at defining external member functions in the next module.
By default, all members of a class declared with the class keyword have private access for all its members.
Therefore, any member that is declared before one other class specifier automatically has private access. For
example:
At the level of programming, a class is a data structure with
- data members for representing the various properties of the different object instances of the class, and
- with member functions for representing the behavior of the class.
Click the Exercise link to add a member function to the
ch_stack struct
.
C++ Member Function - Exercise