473,394 Members | 1,916 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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 2288
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: qazmlp | last post by:
class base { // other members public: virtual ~base() { } virtual void virtualMethod1()=0 ; virtual void virtualMethod2()=0 ; virtual void virtualMethod3()=0 ;
3
by: DaveLessnau | last post by:
In a book on Data Structures that I'm reading, the authors are describing various linked lists and trees. In these, they start with some form of node class. What's driving me crazy is that they...
10
by: darrel | last post by:
I'm still trying to sort out in my head the differences between public and shared when referring to declaring properties or variables. This is my understanding: shared - akin to a 'global'...
8
by: Gregory | last post by:
I have a question about using STL containers in C++ class public interface. Lets say that I want to return some container from class method or accept class method parameter as some container. For...
11
by: prefersgolfing | last post by:
I'm trying to find on MSDN, or someplace, that speaks to variables being public or private by default. Anyone know where? Thanks.
7
by: tron.thomas | last post by:
Please consider the following code: class Abstract { public: virtual ~Abstract() {} virtual void Method() = 0; }; class Concrete : public virtual Abstract
8
by: Al | last post by:
I'd like to create Class Library in VB 2005, which has a property accessible by external programs. I decided to include 1 Class with 1 property in this project. I placed this code in Class:...
86
by: jopperdepopper | last post by:
Hi, finally giving php 5 a go, and going over the new approach to classes. Can someone clarify the public, private and protected to me? I quote the php manual: "The visibility of a property or...
2
by: fgh.vbn.rty | last post by:
Hi, I'm not sure if i'm asking the question correctly but anyway here it is. Say I have 3 classes - class A, class B, class R. 1) A and B are the building blocks and R is like a repository...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.