Thread: 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 11:03:22
|
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 :
// double DNASequence;
}
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
|
|
From: Daniel G. <sp...@ho...> - 2004-03-09 11:24:20
|
As I understand it, when a class is derived PUBLICALLY from another class,
it inherits the parent's public members as public.
So
class Person
{
public:
String* HairColour;
}
class EmployeeID : public Person
{
// We implicitly have a public member String* HairColour. If the code
was written
// instead of being hidden it would read
// public:
// String* HairColour;
}
If EmpoyeeID was derived as protected, we would have
// private:
// String* HairColour
By public to that class, I mean an object of the class can access the member
by doing MyObj.PublicMember = 25; or whatever.
>From: Siva Chandra <siv...@ya...>
>To: Daniel Glenfield <sp...@ho...>
>CC: Dev...@li...
>Subject: Re: [Dev-C++] protected and private in c++ classes
>Date: Tue, 9 Mar 2004 02:57:43 -0800 (PST)
>
>hi Daniel,
>
>I agree on your explaination for private but I am kind
>of confused with your explaination on protected. what
>do you mean by "//class employee now has private:
>..."?
>
>...and what do you mean by "...public to that class"?
>
>Siva Chandra
>
>--- Daniel Glenfield <sp...@ho...> 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 :
> > // double DNASequence;
> >
> > }
> >
> > 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
>
>
>__________________________________
>Do you Yahoo!?
>Yahoo! Search - Find what youre looking for faster
>http://search.yahoo.com
_________________________________________________________________
Stay in touch with absent friends - get MSN Messenger
http://www.msn.co.uk/messenger
|
|
From: Daniel G. <sp...@ho...> - 2004-03-09 11:35:27
|
That sounds about right :) >From: Oladipo Odumosu <gen...@ya...> >To: Daniel Glenfield <sp...@ho...> >Subject: Re: [Dev-C++] protected and private in c++ classes >Date: Tue, 9 Mar 2004 11:06:28 +0000 (GMT) > >private members cannot be accessed except by member methods/functions of >the class they belong to. they also cannot be inherited. protected members >are almost exactly like private, but they can be inherited. inheriting a >protected member are private makes it private in the derived class. >inheriting a protected member as public leaves it as protected. > >plz lemme know if i'm wrong, too > > >--------------------------------- >Too much spam in your inbox? Yahoo! Mail gives you the best spam protection >for FREE! Get Yahoo!Mail _________________________________________________________________ Express yourself with cool new emoticons http://www.msn.co.uk/specials/myemo |
|
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
|
|
From: Siva C. <siv...@ya...> - 2004-03-09 11:14:45
|
hi Daniel,
I agree on your explaination for private but I am kind
of confused with your explaination on protected. what
do you mean by "//class employee now has private:
..."?
...and what do you mean by "...public to that class"?
Siva Chandra
--- Daniel Glenfield <sp...@ho...> 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 :
> // double DNASequence;
>
> }
>
> 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
__________________________________
Do you Yahoo!?
Yahoo! Search - Find what youre looking for faster
http://search.yahoo.com
|
|
From: Per W. <pw...@ia...> - 2004-03-09 12:16:53
|
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
>
>
|