![]() ![]() ![]() | The program including implementation of friend moving functions. | Lecture 3 - slide 31 : 36 Program 3 |
#include <iostream> #include "point.h" using namespace std; // Friend of Point. Mutate p. void Move1(Point &p, double dx, double dy){ p.x += dx; p.y += dy; } // Friend of Point. Do not mutate p. Return moved copy of Point. Point Move2(const Point &p, double dx, double dy){ return Point(p.x + dx, p.y + dy); // point copied out } int main(){ Point p(1,2), q(3,4), r, s; p.move(1,1); // Mutate p, with the member function move. Move1(q,1,1); // Mutate q by the friend Move 1 - maybe misleading! r = Move2(p,1,1); // Make a moved point by the friend Move2. // Which of the moving abstractions do we prefer? cout << "Point p: " << p << endl; // (2,3) cout << "Point q: " << q << endl; // (4,5) cout << "Point r: " << r << endl; // (3,4) }