The C++Course provides a general introduction to programming in C++. It is based on A.B. Downey's book, How to Think Like a Computer Scientist. Click here for details.


Member and Nonmember Functions

There are two types of functions in C++, called nonmember functions and member functions. So far, every function we have written has been a nonmember function. Member functions are declared inside class defintions. Any function declared outside of a class is a nonmember function.

Although we have not written any member functions, we have invoked some. Whenever you invoke a function "on" an object, it's a member function. Also, the functions we invoked on pstrings in Chapter 7 were member functions.

Anything that can be written as a nonmember function can also be written as a member function, and vice versa. Sometimes it is just more natural to use one or the other. For reasons that will be clear soon, member functions are often shorter than the corresponding nonmember functions.


Last Update: 2005-11-21