Making friends

The problem with the code above is that the cartesian_vector class accesses private members of the point class. Since we have written both classes, we are happy to bend the rules, and so we make the cartesian_vector class a friend of the point class:

    class cartesian_vector; // forward decalartion      class point     {         double x; double y;     public:         point(double x, double y) : x(x), y(y){}         friend class cartesian_point;     };

Since the cartesian_vector class is declared after the point class, we have to provide a forward declaration that essentially tells the compiler that the name cartesian_vector is about to be used and it will be declared elsewhere. The important line starts with friend. This indicates that the code for the entire ...

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.