Using the scope resolution operator

You can define a method inline in the class statement, but you can also separate the declaration and implementation, so the method is declared in the class statement but it is defined elsewhere. When defining a method out of the class statement, you need to provide the method with the name of the type using the scope resolution operator. For example, using the previous cartesian_vector example:

    class cartesian_vector 
{
public:
double x;
double y;
// other methods
double magnitude();
};

double cartesian_vector::magnitude()
{
return sqrt((this->x * this->x) + (this->y * this->y));
}

The method is defined outside the class definition; it is, however, still the class method, so it has a this pointer that can be used to access the object's members. Typically, the class will be declared in a header file with prototypes for the methods and the actual methods will be implemented in a separate source file. In this case, using the this pointer to access the class members (methods and data members) make it obvious, when you take a cursory look at a source file, that the functions are methods of a class.