Member Function versus Non-Member Function

A question that many student have is:

What is the difference between a member function and a non-member function?

There are two major differences.
  1. A non-member function always appears outside of a class.

    The member function can appear outside of the class body (for instance, in the implementation file or the .cpp file). But, when you do this, the member function must be qualified by the name of its class. This is to identify that that function is a member of a particular class.

    For instance, if you took our MyArray class and wanted to define the implementation of a new member function called myFunction in the .cpp file.

    You would write:

    int MyArray::myFunction(int a, int b)
    	...//details of this implementation
    	   //note: you need the prototype of myFunction inside the curly brackets
               //of the MyArray class (in the .h file)
    The MyArray:: specifies that the myFunction belongs to the MyArray class.

    By contrast, a non-member function has no need to be qualified. It does not belong to a class. In the implementation file, I could write my non-member function as:

    int myFunction (int a, int b)
    	..//details of this implementation
  2. Another difference between member functions and non-member functions is how they are called (or invoked) in the main routine. Consider the following segment of code:
    int main()
    	int i;
    	MyArray a;  //declare a MyArray object
    	i=myFunction(3,2);  //invoking the non-member function
    	i=a.myFunction(3,2); //invoking the member function

Back to the Operator Overloading and "This" Lab click here