2

I am learning OOP in C++ and I pretty much know most of the basics of it. But here is my query. I have learnt that we cannot access private data members from other objects. But I have a code snippet that seems to be doing so. It's working perfectly, but I want to know, why and how is this code working? Isn't it a violation of the OOP rules?

Here is the code:

#include "iostream"

using namespace std;

class Dist {
int feet;          //private by default
float inches;      //private by default
public:
void getdata()
{
    cout<<"Enter the feets: ";
    cin>>feet;
    cout<<"Enter the inches: ";
    cin>>inches;
}
void putdata()
{
    cout<<"Data is "<<feet<<"\' "<<inches<<"\" "<<endl;
}
void add(Dist d)
{
    this->feet = this->feet + d.feet;// accessing private data members!
    this->inches = this->inches + d.inches;
    if (this->inches >= 12) {
        this->feet++;
        this->inches = this->inches - 12;
    }
}
};

int main()
{
Dist d1,d2;
d1.getdata();
d2.getdata();
d1.add(d2);
d1.putdata();
return 0;
}

How is this possible?

iksemyonov
  • 4,106
  • 1
  • 22
  • 42
Panda
  • 2,400
  • 3
  • 25
  • 35

3 Answers3

3

In case you mean this part of the code:

void add(Dist d){
    this->feet = this->feet + d.feet;// accessing private data members!
    this->inches = this->inches + d.inches;
    /* ... */
}

Then it's perfectly fine for the reason that both this and d are objects of class Dist. What matters is that they're the same class, not the same object.

See Why do objects of the same class have access to each other's private data? for a detailed explanation.

Community
  • 1
  • 1
iksemyonov
  • 4,106
  • 1
  • 22
  • 42
  • So, that means, if they are of same class, then Ill be able to access them! and not if they are objects of another class ? – Panda Feb 20 '16 at 17:07
  • 1
    "then Ill be able to access them" access from where exactly? In your code, you're accessing `Dist::feet` from `void Dist::add(Dist)`, which belongs to the same class. If you tried to access `Dist::feet` from another class, then that would be an error since private data members aren't accessible by both other non-derived classes and derived classes of `Dist`. The only exception would be to declare the other class a `friend` class of `Dist`. – iksemyonov Feb 20 '16 at 17:11
1

No it's fine. Access to the private members is restricted to functions in the class; irrespective of whether the function itself is private, protected, or public.

Because your functions are public, they can be accessed through instances of your class.

Note that functions that have instances of the class as parameters can access the private members of those too - if that was not the case then it would be difficult to write code like copy constructors and assignment operators.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • 1
    But, d is a different object. – Panda Feb 20 '16 at 17:03
  • That's a very good point but it doesn't matter. It would be difficult to write things like copy constructors if this was disallowed! – Bathsheba Feb 20 '16 at 17:04
  • In theory, the language could require a class to `friend` itself in order to allow access to `private` members of other objects. I have never thought seriously about this, the thought just crossed my mind... – Christian Hackl Feb 20 '16 at 17:07
  • I'm not convinced the language could implement that at compile time. Need time to think over this too. – Bathsheba Feb 20 '16 at 17:24
  • So, @ChristianHackl, Did I just discover a problem with the language ? – Panda Feb 22 '16 at 08:19
  • @Milind: not really, because I don't think it has ever caused bugs, made development times longer or decreased general software quality. – Christian Hackl Feb 22 '16 at 16:59
1

Private means you cannot access the data from outside the class.

Other members of the class can still access them, even if they are public themself.

Something like

d1.feet;

would be invalid though.

Anedar
  • 4,235
  • 1
  • 23
  • 41