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:
ub_prt( void ){ cout << "Public\n"; }
access:
riv_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;
}
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:
access:
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;
}