 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Jordan Taylor Guest
|
Posted: Sun Feb 26, 2006 5:06 am Post subject: why use "protected" instead of "private"? |
|
|
I am confused about protected member functions and objects. Is there
any particular advantage of declaring members protected? |
|
| Back to top |
|
 |
benben Guest
|
Posted: Sun Feb 26, 2006 6:06 am Post subject: Re: why use "protected" instead of "private"? |
|
|
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
|
Posted: Sun Feb 26, 2006 10:06 am Post subject: Re: why use "protected" instead of "private"? |
|
|
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
|
Posted: Sun Feb 26, 2006 10:06 am Post subject: Re: why use "protected" instead of "private"? |
|
|
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 |
|
 |
|
|
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
|
|