Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is accessing private variable allowed here?

Tags:

c++

I have this class declared. I observed that in the method distance(Point b), how is it possible to access the private members of Point - b.x and b.y? If I try to access b.x and b.y in main, it is not allowed.

#include <iostream>
#include <cmath>

using namespace std;

class Point {
private:
    int x, y;
public:
    Point() {
        cout << "Constructor called" << endl;
        x = 0; y = 0;
    }

    ~Point() {
    }

    void set(int a, int b) {
        x = a;
        y = b;
    }

    void offset(int dx, int dy) {
        x += dx;
        y += dy;
    }

    void print() {
        cout << "(" << x << "," << y << ")" << endl;
    }

    // HERE
    double distance(Point b) {
        return (sqrt(pow(x-b.x, 2)+pow(y-b.y, 2)));
    }
};

int main() {
Point p, q;

p.print();
p.offset(4, 3);
p.print();

q.set(10, 2);

cout << "Distance: " << p.distance(q) << endl;

return 0;
}

NOTE: I have compiled and ran the program on ideone.com

like image 356
aakash Avatar asked May 01 '26 08:05

aakash


1 Answers

The concept of access specifiers such as private, public etc. applies to classes, not just objects of classes. If a variable is private in a class, and an object A of that class has a function that takes another object B of the same class, A has access to B's private members since A and B belong to the same class.

Copy constructors rely on this:

#include <iostream>                                                                

using namespace std;                                                            


class A {                                                                       
  public:                                                                       

     A(){val = 1.0;}                                                            

     //copy constructor                                
     A(const A& _other) {                                                       
       val = _other.val; //accessing private member of _other                   
     }                                                                          

    double dist(A _a) {return val - _a.val;} //accessing private member of _other

  private:                                                                      
    double val;                                                                 
};                                                                              


int main() {                                                                    

A a;                                                                            
A b;                                                                            

cout << a.dist(b) << endl;                                                      

}
like image 113
maditya Avatar answered May 03 '26 22:05

maditya



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!