aakash
aakash

Reputation: 761

Why is accessing private variable allowed here?

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

Upvotes: 3

Views: 140

Answers (2)

Suvarna Pattayil
Suvarna Pattayil

Reputation: 5239

int x, y; are private to the Class Point. It means all the members of Class Point can access,modify it.

The functions and constuctors of Class Point are public i.e Other functions(from some other class etc) [like main] can access them. The idea of OOPS is to keep your data safe, Anyone who wants to modify the values can do it via the public methods of that class. They can't access them directly. This allows you to keep any check for invalid modifications to data [setting invalid values like height = -4; etc]

If u keep int x,y as public [which is not correct OOPs] any function (main etc) can access it and modify the value to something undesirable.

Point p;

p.x = -5 (suppose you only wanted +ve values for x , you can't check if the functions accessing is setting some undesirable value)`

Not a relevant analogy , but still...You have a room in the house which only the family members can access. Any outsider who wishes to keep or remove or do anything to the things in the house has to ask your family members to do it. If u assume even family members are not allowed to access the room [assuming even member functions can't access variables ] then there is no use of keeping anythin in the room [the data cannot be used by anyone]

Upvotes: 0

maditya
maditya

Reputation: 8896

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;                                                      

}

Upvotes: 4

Related Questions