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

Get Beginning C++ Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.