for whom the header files are important?

Y

yogpjosh

Hi,

for whom the header files are important?

for the programmer or for the compiler..??
As the programmer very well sees all the private data of a class
through the header file ..but if he tries to access it then the
compiler throws the error..


Thanks and Regards,
Yogesh Joshi
 
G

Gianni Mariani

Hi,

for whom the header files are important?

for the programmer or for the compiler..??
As the programmer very well sees all the private data of a class
through the header file ..but if he tries to access it then the
compiler throws the error..


Header files allow you to create "interfaces" and reusability of code.

As for private data, you can, if you wish, hide all your private
elements in the cpp file.
 
K

Kai-Uwe Bux

for whom the header files are important?

for the programmer or for the compiler..??

The compiler.
As the programmer very well sees all the private data of a class
through the header file ..but if he tries to access it then the
compiler throws the error..

The programmer is supposed to read the documentation/specs. Stuff in the
headers are implementation details.


Best

Kai-Uwe Bux
 
S

Salt_Peter

Hi,

for whom the header files are important?

for the programmer or for the compiler..??
As the programmer very well sees all the private data of a class
through the header file ..but if he tries to access it then the
compiler throws the error..


Thanks and Regards,
Yogesh Joshi

As far as the compiler is concerned, header files don't exist.
That is, until you include them into your source(s).
And nothing stops you from accessing the private parts of a class,
either.
Thats what accessors and public member functions are for.
Compilers generate errors because the declaration of the class is a set
of rules the compiler must obey and enforce.
 
S

Signal9

Hi,

for whom the header files are important?

for the programmer or for the compiler..??
As the programmer very well sees all the private data of a class
through the header file ..but if he tries to access it then the
compiler throws the error..


Thanks and Regards,
Yogesh Joshi

You should read a C++ book please.
 
Y

yogpjosh

Salt_Peter said:
As far as the compiler is concerned, header files don't exist.
That is, until you include them into your source(s).
And nothing stops you from accessing the private parts of a class,
either.
Thats what accessors and public member functions are for.
Compilers generate errors because the declaration of the class is a set
of rules the compiler must obey and enforce.

Yes..got it..merely seeing the private variables doesn't mean you have
access to that..

Thanks and Regards,
Yogesh Joshi
 
S

Salt_Peter

Yes..got it..merely seeing the private variables doesn't mean you have
access to that..

Thanks and Regards,
Yogesh Joshi

Not neccessarily true.

class N
{
int n;
public:
N() : n(0) { }
void set(int x) { n = x; }
int get() const { return n; }
};

I can see, modify and access the private integer n.
 
Y

yogpjosh

Salt_Peter said:
Not neccessarily true.

class N
{
int n;
public:
N() : n(0) { }
void set(int x) { n = x; }
int get() const { return n; }
};

I can see, modify and access the private integer n.
sorry ..what I mean to say was..accessing the private variables
directly..i.e without using any member functions..
(I also come across this statement that " getter and setter functions
reveal the internal implementation details of the class and should be
avoided..")

cheers,
Yogesh Joshi
 
N

Noah Roberts

sorry ..what I mean to say was..accessing the private variables
directly..i.e without using any member functions..

You still can, but only if you abuse the system:

N n;
int * n_member = reinterpret_cast<int*>(&n);
n_member = 5;
 
G

Gianni Mariani

Noah Roberts wrote:
....
You still can, but only if you abuse the system:

N n;
int * n_member = reinterpret_cast<int*>(&n);
n_member = 5;

did you mean:

N n;
int & n_member = * reinterpret_cast<int*>(&n);
n_member = 5;

?
 
Y

yogpjosh

Gianni said:
Noah Roberts wrote:
...

did you mean:

N n;
int & n_member = * reinterpret_cast<int*>(&n);
n_member = 5;

?

but those are the intentional (illegal??) activities of the programmer
..But compiler will complain if the programmer writes

n.n //access denied..

so that means the header files are for compiler..

cheers,
Yogesh Joshi
 
S

Salt_Peter

but those are the intentional (illegal??) activities of the programmer
.But compiler will complain if the programmer writes

n.n //access denied..

so that means the header files are for compiler..

Again, as far as the compiler is concerned, header files don't exist.
Thats why you need to include them into source(s).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,264
Messages
2,571,065
Members
48,770
Latest member
ElysaD

Latest Threads

Top