Are private class functions available?

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) 
+4
source share
7 answers

The private keyword indicates that these members are only accessible from member functions and friends of the class. Private variables are accessible to the same types of objects even from other instances of the class.

This is not about security, which many people think. This applies to hiding the internal structure of the class from other codes. It is required that the class does not spoil other instances by chance, so there was no point in hiding variables from other instances. (Actually, it would be much harder to implement, and there are no or few reasons for this.)

+7
source

private members outside the class cannot be accessed except friend s, but can be from anywhere inside the class.

+5
source

You can call methods yourself because they are in the public section of the class definition, and they can access private elements because they are part of the class.

 private: double x_, y_; public: Point(double x, double y) Point() double getX() double getY() void setX(double x) void setY(double y) void add(Point p) void sub(Point p) void mul(double a) void dump() 

Fields in private can only be accessed by other members of the class in this case (without friends).

Anyone can access public items.

+3
source

Because access to these variables comes from within the class through its methods; you do not get direct access to variables.

+3
source

You confuse the "class" with the "object". Put it on your way of thinking. An object can access private members of another object if both objects are of the same class.

0
source

Since add(Point p) and sub (Point p) are members of the Point class, they can access the private members of any instance of Point (in this case p ), and not just the private members of this .

0
source

You do not have direct access to private members of the class. You call the add () and sub () method, which is public. They are allowed access to private member variables.

If you tried:

 p.x_ ++; 

which will not be allowed since x_ is a private class of Point

-1
source

Source: https://habr.com/ru/post/1436824/


All Articles