473,898 Members | 2,888 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_me thod_with_publi c_method( void );

private:

priv_prt( void );

};


// file: access_defn.cpp
#include "access_dec l.h"

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

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

access::call_pr ivate_method_wi th_public_metho d( void )
{
cout << "Calling Private from public\n";

pub_prt();
}
// file: access_main.cpp
#include "access_dec l.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_pub lic_method();

return 0;
}
Jul 22 '05 #1
3 2332
"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_me thod_with_publi c_method( void );

private:

priv_prt( void );

};


// file: access_defn.cpp
#include "access_dec l.h"

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

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

access::call_pr ivate_method_wi th_public_metho d( 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_dec l.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_pub lic_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.c om...
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
2357
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
1560
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 define these such that the data in those node classes is public. As a specific example (a node they're going to use to build linked lists): template <typename T> class node { public:
10
3688
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' variable for the application. Any other code within the application can access it. public - can be shared across the application if instatiated. Does that sound about right? It seems these are more useful for methods rather than variables. Most of...
8
3960
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 example: class A { public: const vector<int>& getTable() { return m_table; }
11
5659
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
1926
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
2025
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: Public Class COM Private mMyProp As String Public Property MyProp() As String
86
4707
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 method can be defined by prefixing the declaration with the keywords: public, protected or private. Public declared items can be accessed everywhere."
2
1794
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 that stores objects of A and B. 2) A is at the lowest level and should "know about" only other As. B should know only about As and other Bs.
0
9993
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10854
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10946
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10480
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9658
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8034
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7187
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6076
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4295
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.