470,815 Members | 1,278 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,815 developers. It's quick & easy.

public vs private access

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;
}
Jul 22 '05 #1
3 2161
"quo" <st@us.net> wrote...
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, non-standard header. Use <iostream>

//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
Jul 22 '05 #2
On Thu, 08 Jan 2004 03:32:24 +0000, Victor Bazarov wrote:
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.


As can friends of the class.

HTH,
M4

Jul 22 '05 #3

"quo" <st@us.net> wrote in message
news:pk********************************@4ax.com...
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
Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by darrel | last post: by
8 posts views Thread by Gregory | last post: by
11 posts views Thread by prefersgolfing | last post: by
8 posts views Thread by Al | last post: by
86 posts views Thread by jopperdepopper | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.