Hi I'm very new to c++. I just write a c++ code which is following :
#include<iostream>
using namespace std;
class complex
{
float x,y;
};
int main ()
{
complex a;
a.x=500;
a.y=600;
cout<<" the value of a.x = "<<a.x<<"\n" ;
cout<<" the value of a.y = "<<a.y<<"\n" ;
return 0;
}
when i compile the program it give me following error:
try.cpp: In function ‘int main()’:
try.cpp:5: error: ‘float complex::x’ is private
try.cpp:10: error: within this context
try.cpp:5: error: ‘float complex::y’ is private
try.cpp:11: error: within this context
try.cpp:5: error: ‘float complex::x’ is private
try.cpp:12: error: within this context
try.cpp:5: error: ‘float complex::y’ is private
try.cpp:13: error: within this context
i resolve the error by just declaring data member public ;
Now what should i do to make this thing work with private member? Why can't i access the private member with the object of class? How can i directly access the private data members or why i cant use the data member of class directly with class object ? What's the reason behind it?
How class is implemented in memory? How class prevent us or stop us to use its private data or implements it's security mechanism ? what is compiler do when is saw a class? How compiler implement the class and its security mechanism ?
Please explain this to me