C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

why use "protected" instead of "private"?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Jordan Taylor
Guest





PostPosted: Sun Feb 26, 2006 5:06 am    Post subject: why use "protected" instead of "private"? Reply with quote



I am confused about protected member functions and objects. Is there
any particular advantage of declaring members protected?
Back to top
benben
Guest





PostPosted: Sun Feb 26, 2006 6:06 am    Post subject: Re: why use "protected" instead of "private"? Reply with quote



Jordan Taylor wrote:
Quote:
I am confused about protected member functions and objects. Is there
any particular advantage of declaring members protected?


If you want a particular member accessible from a class inheriting you
class but not the direct user then you should make the member protected.

For example, consider the classic shape hierarchy with drawing
capabilities. Naturally, the code dealing with drawing is encapsulated
in a separate class shape_drawer:

class shape_drawer
{
public: // provided for users
void set_color(const color&);
void apply_filter(const filter&);

virtual void draw(void) = 0;

protected: // provided for inheritors
void draw_line(point from, point to);
void draw_curve(const std::vector<point>& pts);

~shape_drawer();
};

Then the shape hierarchy can be augmented by deriving all concrete
shapes from shape_drawer

class circle: public shape, public shape_drawer
{
// ...
public:

// protected members in base are used to implement the following
// function for example:
void draw(void)
{
using namespace std;
vector<point> pts = get_coordinates();
draw_curve(pts);
}
//...
};


// protected members are kept away from user, as follows:
int main()
{
circle c;
c.set_color(color::red); // ok, shape_drawer::set_color is public
c.apply_filter(alpha_filter(0.50)); // ok
c.draw();

vector<point> p;
p.push_back(point(0, 0));
p.push_back(point(3, 4));
p.push_back(point(4, 5));

c.draw_line(p[0], p[1]); // error: calling protected member
c.draw_curve(p); // error: calling protected member
}


Regards,
Ben
Back to top
al pacino
Guest





PostPosted: Sun Feb 26, 2006 10:06 am    Post subject: Re: why use "protected" instead of "private"? Reply with quote



Jordan Taylor wrote:
Quote:
I am confused about protected member functions and objects. Is there
any particular advantage of declaring members protected?

hi jordan,

well protected members can be manipulated by the functions of the
derived
class thats it.

however according to principle of least privelage(C++ how to program by
Deitel)
always use private data members if derived class wants to use those
values
provide function/s in the base class that acts as an interface for the
private data.
Back to top
Backer
Guest





PostPosted: Sun Feb 26, 2006 10:06 am    Post subject: Re: why use "protected" instead of "private"? Reply with quote

Protected keywords are only used in the inheritance context. The base
class protected variables can be accessed in the derived class also.
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.