Re: [Dev-C++] protected and private in c++ classes
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Daniel G. <sp...@ho...> - 2004-03-09 13:03:35
|
Thanks Per, I think that example explains things better.
Daniel
:)
>From: Per Westermark <pw...@ia...>
>To: Daniel Glenfield <sp...@ho...>
>CC: stu...@li..., <Dev...@li...>
>Subject: Re: [Dev-C++] protected and private in c++ classes
>Date: Tue, 9 Mar 2004 12:59:44 +0100 (CET)
>
>I wrote terse, but was actually correct:
>"Private is only for methods of the class" as in: privately declared
>member fields and methods may only be accessed by members of that class :)
>
>I didn't say that only methods may use private. I meant that only methods
>of that class may acccess any private fields, methods, constructors, ...
>
>I don't think it's correct to say that methods are usually public. There
>are just as large reasons to keep methods protected or private as there
>are to keep variables protected or private.
>
>I think you missed a bit about inherritance of access rights to protected
>fields. The protected declarations will continue to be protected for
>multiple generations, unless one of the inherritances stops it, i.e.
>
>class A {
>protected:
> int a;
>};
>
>class B : public A {
> // a still protected
>};
>
>class C : public B {
> // a still protected
>};
>
>class D : private C {
> // a now private because parent C is private!
>};
>
>class E : public D {
> // No longer access to field a, since class D
> // declared it's parent relation as "private".
>};
>
>/Per W
>
>On Tue, 9 Mar 2004, Daniel Glenfield wrote:
>
> > I don't think that is quite correct.
> >
> > Without getting too nerdy, private specifies that the member variable or
> > method (methods are usually public, not private) can only be accessed by
>a
> > method of that class, and cannot be accessed directly.
> > So if I have a private member
> > int EmployeeID;
> > I cannot do
> >
> > MyObject.EmployeeID = 12345;
> > Because it is private. What I need is a public method like this
> >
> > void SetEmployeeID(int Num)
> > {
> > EmployeeID = Num;
> > }
> > Then I can do MyObject.SetEmployeeID(12345);
> >
> >
> > Protected means the members are public to that class, but if a class is
> > derived from it then they become private. e.g.
> >
> > class Person
> > {
> > protected:
> > double DNASequence;
> > }
> >
> > class Employee : public Person
> > {
> > // class Employee now has
> > // private : <<<< wrong
>
> // protected: <<<< still protected, since parent
> > // double DNASequence; <<< class was declared "public Person"
> >
> > }
>
> class Supervisor : private Employee
> {
> // class Supervisor also has PROTECTED
> // DNASequence, i.e.
>
> void clone(double newDNASequence) {
> // OK, since Supervisor is a descendant from Person, and
> // Empployee inherrited with public!
> // Supervisor Bhas access rights to DNASequence
> DNASequence = newDNASequence;
> }
>
> class MeanSupervisor : public Supervisor
> {
> void mutate(double amount) {
> // NOT ALLOWED, since Supervisor inherrited privately
> // from Employee.
> // MeanSupervisor does not have access rights to DNASequence
> DNASequence *= amount;
> }
> }
> >
> > Let me know if any of this sounds wrong.
> >
> > Daniel
> >
> >
> > >From: Per Westermark <pw...@ia...>
> > >To: studioartoni <stu...@li...>
> > >CC: Dev...@li...
> > >Subject: Re: [Dev-C++] protected and private in c++ classes
> > >Date: Tue, 9 Mar 2004 11:15:18 +0100 (CET)
> > >
> > >Private is only for methods of the class.
> > >
> > >Protected are also available to all inherriting classes.
> > >
> > >/Per W
> > >
> > >On Tue, 9 Mar 2004, studioartoni wrote:
> > >
> > > > Excuse the question,
> > > > but which is the difference between 'protected' and 'private' in c++
> > >classes?
> > > > Thank you.
> > > >
> > > >
> > >
> > >
> > >
> > >-------------------------------------------------------
> > >This SF.Net email is sponsored by: IBM Linux Tutorials
> > >Free Linux tutorial presented by Daniel Robbins, President and CEO of
> > >GenToo technologies. Learn everything from fundamentals to system
> > >administration.http://ads.osdn.com/?ad_id=1470&alloc_id=3638&op=click
> > >_______________________________________________
> > >Dev-cpp-users mailing list
> > >Dev...@li...
> > >TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
> > >https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
> >
> > _________________________________________________________________
> > Find a cheaper internet access deal - choose one to suit you.
> > http://www.msn.co.uk/internetaccess
> >
> >
>
>
>
>-------------------------------------------------------
>This SF.Net email is sponsored by: IBM Linux Tutorials
>Free Linux tutorial presented by Daniel Robbins, President and CEO of
>GenToo technologies. Learn everything from fundamentals to system
>administration.http://ads.osdn.com/?ad_id=1470&alloc_id=3638&op=click
>_______________________________________________
>Dev-cpp-users mailing list
>Dev...@li...
>TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
>https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
_________________________________________________________________
Stay in touch with absent friends - get MSN Messenger
http://www.msn.co.uk/messenger
|