On 05.04.1998 Mike Walker wrote about PRIVATE / PROTECTED...
> Something that I am not quite clear on is the difference between
> private and protected specifiers within a class. Can someone explain
> the difference between these? Also when would each be used?
//I'll take a go at this one. Please note that I just program as
//a hobby and anything I say is probably wrong, dangerous, illegal,
//and prohibited in Florida.
//
//public: is seen by all classes
//
//protected: is seen by derived classes and members of the same class
//
//private: is not seen by derived classes, it is only seen by members of the
// same class
// A base class
class foo
{
public:
void foofunction();
int public_int;
protected:
int protected_int;
private:
int private_int;
};
void foo::foofunction()
{
public_int = 1; // legal
protected_int = 1; // legal
private_int = 1; // legal
}
// A derived class
class bar : public foo
{
public:
void barfunction();
};
void bar::barfunction()
{
public_int = 1; // legal
protected_int = 1; // legal
private_int = 1; // ILLEGAL
// noclass.cc:60: member 'private_int' is private
}
// A global function
int main(void)
{
bar fly;
fly.public_int = 1; // legal
fly.protected_int = 1; // ILLEGAL
// noclass.cc:71: member 'protected_int' is protected
fly.private_int = 1; // ILLEGAL
// noclass.cc:73: member 'private_int' is private
return 0;
}
--
* jmr 0.7.17 [Linux] * benjamin.l.mcgee@purgatoire.org
--- FLAME v1.1
---------------
* Origin: Purgatoire BBS, 719-846-0140, Trinidad, CO, V.34 (1:15/7)
|