I have been learning C ++ for some time now. I recently came across the following code snippet:
#include <iostream> using namespace std; class Point { private: double x_, y_; public: Point(double x, double y){ x_ = x; y_ = y; } Point() { x_ = 0.0; y_ = 0.0; } double getX(){ return x_; } double getY(){ return y_; } void setX(double x){ x_ = x; } void setY(double y){ y_ = y; } void add(Point p){ x_ += p.x_; y_ += p.y_; } void sub(Point p){ x_ -= p.x_; y_ -= p.y_; } void mul(double a){ x_ *= a; y_ *= a; } void dump(){ cout << "(" << x_ << ", " << y_ << ")" << endl; } }; int main(){ Point p(3, 1); Point p1(10, 5); p.add(p1); p.dump(); p.sub(p1); p.dump(); return 0; }
And for life, I canβt understand why the void add(Point P) and void sub( Point p ) methods work.
Should I get an error like "cannot access private properties of class Point" or something when I try to use add or sub ?
The program is compiled with gcc version 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5) . When launched, it produces:
(13, 6) (3, 1)
source share