-1
#include<iostream>
using namespace std;
class complex
{
    int a,b;

    public:
    void set_data(int a,int b)
    {
        this->a=a;
        this->b=b;
    }
    void show_data()
    {
        cout<<a<<"+"<<b<<"i";
    }
    complex add(complex c)
    {
        complex temp;
        temp.a=a+c.a;
        temp.b=b+c.b;
        return temp;
    }
};
int main()
{
    complex c1,c2;
    c1.set_data(5,6);`
    c2.set_data(4,5);
    complex c3=c1.add(c2);
    c3.show_data();
}

here temp and c are accessing a and b by . operator

why temp and c can access a and b private variables using dot operator???

but private variables are not accessible outside of class.

Jignasha Royala
  • 1,032
  • 10
  • 27
  • `temp` is a `complex` itself and is therefore trusted. It would be really hard to write C++ code if that wouldn't be the case. – Ted Lyngmo Mar 13 '21 at 09:47
  • 4
    The `private` and `protected` keywords work per _class_ and not per _instance_. Method `add` is a member of `complex`, and therefore has access to `private` members, even for other instances of `complex` than `this`. – prapin Mar 13 '21 at 09:50
  • but if i do the same thing in main function there will be no access to variables a and b! – Anshul Pareek Mar 13 '21 at 09:51
  • `main` is not a `complex` member function - it's a free function. – Ted Lyngmo Mar 13 '21 at 09:51
  • 1
    _"but private variables are not accessible outside of class."_ - where do you use `temp`? Inside `complex` or outside? – Lukas-T Mar 13 '21 at 09:52
  • so the scope of private keyword is what??? – Anshul Pareek Mar 13 '21 at 09:52
  • It's for everything _not_ being a `complex` – Ted Lyngmo Mar 13 '21 at 09:53
  • i used temp inside class. inside the class made a new object of that class, and then I am able to access its member variables. but the scope of a and b is just available for member functions. so according to me they should haven't been able to be accessed by dot operator anywhere. – Anshul Pareek Mar 13 '21 at 09:54
  • 2
    As prapin said, the scope of `private` is the class. Specifically any member functions of the class or `friend`s of the class (which can only be declared within the class definition) can access `private` members of any instance of that class, or any `static` members that are also `private`. – Peter Mar 13 '21 at 09:55
  • so ted if i make a class and inside that I make member functions and in the member functions if I make any object of the same class then member variables are accessible using dot operator inside that function even if the variables are private?? – Anshul Pareek Mar 13 '21 at 09:57
  • 1
    The `add()` function is a member of `complex`. It can therefore access `private` members of `temp` (which is an instance, of automatic storage duration, of `complex` created with the `add()` member function). The `add()` member function can also access members of `c`, that is a `complex` passed to it by value. – Peter Mar 13 '21 at 09:58

1 Answers1

4

Taken from cppreference:

A class defined with the keyword class has private access for its members and its base classes by default. A class defined with the keyword struct has public access for its members and its base classes by default. A union has public access for its members by default.

And

A private member of a class is only accessible to the members and friends of that class, regardless of whether the members are on the same or different instances

Meaning that you can use a or b (by default private members of your class complex) inside the class, but every other class or function outside of the class can't have access to them. For that to happen you will need accessors (setters/getters) inside your class.

Those accessors come under the OOP rule of Encapsulation

ex.

int Get_a()
{
    return this->a;
}

void Set_a(int other)
{
    this->a = other;
}

Also, as already said in the comments, temp and c are themselves of type complex and have access to private members.

Segmentation
  • 403
  • 7
  • 20
  • what is the friends of a class?? – Anshul Pareek Mar 13 '21 at 10:02
  • 1
    @AnshulPareek given that you are new to c++ there is no need to pay attention to a friend class. What matters to you now is that a class that has private members can only access them from inside the class. Every other function or class or instance of the same class can't access them. – Segmentation Mar 13 '21 at 10:10
  • but in my code temp is an object made of class complex, so why this have access to a and b?? and also inside the class only member functions can access private variables?? – Anshul Pareek Mar 13 '21 at 10:14
  • 1
    `temp` has access because `private`'s scope is the class and `temp` is declared and used inside the class. As for the second question, every member of the class can access private variables inside the class. Most of what I am answering is already answered in the comments above. Inside class `complex` you can use all `private` members even from instances of class `complex`. But only inside the class. – Segmentation Mar 13 '21 at 10:34