public vs private access

Q

quo

two questions:
1) Does this program demonstrate the basic difference between public
and private access?

It appears correct to say that instances of a class cannot directly
call a private method, but a public method can be called by the
instance to invoke the private method.

2) So is it true that only public methods of a class can invoke a
private method of that same class?

#include <iostream.h>

//file: access_decl.h
class access
{
public:

pub_prt( void );
call_private_method_with_public_method( void );

private:

priv_prt( void );

};




// file: access_defn.cpp
#include "access_decl.h"

access::pub_prt( void ){ cout << "Public\n"; }

access::priv_prt( void ){ cout << "Private\n"; }

access::call_private_method_with_public_method( void )
{
cout << "Calling Private from public\n";

pub_prt();
}


// file: access_main.cpp
#include "access_decl.h"

int main( void )
{
access a;

a.pub_prt();

// can't call private method w/instance of class
//a.priv_prt();

a.call_private_method_with_public_method();

return 0;
}
 
V

Victor Bazarov

quo said:
two questions:
1) Does this program demonstrate the basic difference between public
and private access?
No.

It appears correct to say that instances of a class cannot directly
call a private method, but a public method can be called by the
instance to invoke the private method.

The statement above is, what's the word?, murky. Instances of a class
do not call anything. It's always somebody else calling member functions
of a class _for_ a particular instance of it. However, if an instance
while in a member function wants to call a private member function, it
may.
2) So is it true that only public methods of a class can invoke a
private method of that same class?

No. Protected member functions and private member functions can also
call private member functions.
#include <iostream.h>

Old said:
//file: access_decl.h
class access
{
public:

pub_prt( void );
call_private_method_with_public_method( void );

private:

priv_prt( void );

};




// file: access_defn.cpp
#include "access_decl.h"

access::pub_prt( void ){ cout << "Public\n"; }

access::priv_prt( void ){ cout << "Private\n"; }

access::call_private_method_with_public_method( void )
{
cout << "Calling Private from public\n";

pub_prt();

You're not calling a _private_ member here. 'pub_prt' is PUBLIC.
}


// file: access_main.cpp
#include "access_decl.h"

int main( void )
{
access a;

a.pub_prt();

// can't call private method w/instance of class
//a.priv_prt();

a.call_private_method_with_public_method();

return 0;
}

Victor
 
J

jeffc

quo said:
two questions:
1) Does this program demonstrate the basic difference between public
and private access?

It appears correct to say that instances of a class cannot directly
call a private method, but a public method can be called by the
instance to invoke the private method.

Your terminology is suspect. It is not the instance of the class you're
interested in - it's *where* the call is made. From within the class code,
private methods can be called. Externally they cannot.
2) So is it true that only public methods of a class can invoke a
private method of that same class?

No, all methods of a class can invoke private methods of that class.

class A
{
private:
void f();
void g();
public:
void i();
};

void A::f()
{
g(); // this is fine - private method calling private method (internal)
}
void A::i()
{
g(); // this is fine - public method calling private method (internal)
}
A a;
a.g(); // this is not fine - external call to private method
a.i(); // this is fine - external call to public method
 

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,159
Messages
2,570,879
Members
47,414
Latest member
GayleWedel

Latest Threads

Top