470,849 Members | 1,383 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,849 developers. It's quick & easy.

why use "protected" instead of "private"?

I am confused about protected member functions and objects. Is there
any particular advantage of declaring members protected?

Feb 26 '06 #1
3 6419
Jordan Taylor wrote:
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
Feb 26 '06 #2

Jordan Taylor wrote:
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.

Feb 26 '06 #3
Protected keywords are only used in the inheritance context. The base
class protected variables can be accessed in the derived class also.

Feb 26 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Jules Dubois | last post: by
6 posts views Thread by Jordi Vilar | last post: by
28 posts views Thread by Act | last post: by
4 posts views Thread by p988 | last post: by
4 posts views Thread by Tina | last post: by
7 posts views Thread by =?Utf-8?B?cmFtbzk5NDE=?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.