Archived

This topic is now archived and is closed to further replies.

Subotron

Class: difference between private and protected?

Recommended Posts

Subotron    182
When making a class, it looks like this:
class CClass
{
private:
    // Stuff
protected:
    // Stuff
public:
    // Stuff
};
 
Now what are the differences between these? All I know about it is that I cannot access a private function/variabele outside the class, but I can do this with public ones. Are there any other differences? And what does protect do? | Panorama 3D Engine | Contact me | | MSDN | Google | SourceForge |

Share this post


Link to post
Share on other sites
granite811    122
You are correct about public and private. Protected has to do with inheritance. A protected member variable or function acts as a private one for instances of this class. But say you have a class CChild that is an inherited class of CClass, i.e.

class CChild : public CClass{
// yadda
};

CChild will NOT be able to access private members of CClass, but will be able to access protected members of CClass. Think of protected as private to the world, but public to child classes.

That''s pretty much the only differences between the three. As far as function, you could make everything public and it would work fine, but it would be bad programming practice is all.

Share this post


Link to post
Share on other sites
RizMan    146
members of a class A which have been declared as protected can be accessed from a class B which inherits from A. However, other classes can''t. Whereas private means that only class A can access these members.
I put together following table:

Type Class itself inherating class foreign class
private yes no no
protected yes yes no
public yes yes yes


"yes" means it can access the member
"no"... well guess

Hope that helps

Share this post


Link to post
Share on other sites
CraZeE    217
get a good book on C++ and u''ll have all ur answers. DO a search for Bruce Eckels free PDF version of his "Thinking in C++" vol 1 and 2.

But for the immediate moment, u got the basic concept of ''public'' and ''private'' sections of a class declaration. Their other prominent differences together with ''protected'' only come in during class inheritance.

I will not get into WHAT inheritance is (as i said, get a book ) but lets just say its a way for classes to have ''children''; and like real kids, the ''child'' classes inherit ''genes'' from the parent. How a gene is inherited is where those keywords come in.

private: child classes inherit these members/functions BUT it cannot access them directly. its like the wires in ur house, u know they''re there, but u can only use em thru sockets

public: go figure!

protected: tricky fellow. these bunch get inherited to the child AND are accessible to the child class. but they behave like private members, meaning OTHER classes cant access them directly.
Also, these keywords are also used in conjunction with the class specifier during inheritance. eg: class Child : public Dad {...};

Now, the above explanation of the 3 keywords only apply under the assumption of the above example; public inheritance. If instead u use the private specifier, ALL inherited data will become private. So the child class cannot access the parent-class'' private members, but all else will become native private members to the child.

I''m not very sure bout ''protected'' at this point. Private members will definitely remain private, and public members get promoted to protected. BUT, whether protected data remain protected or become private... need clarification.

thats a mouthful but hope it helps. and i sure hope i got em rite

Share this post


Link to post
Share on other sites