473,653 Members | 3,000 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 6702
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(co nst filter&);

virtual void draw(void) = 0;

protected: // provided for inheritors
void draw_line(point from, point to);
void draw_curve(cons t std::vector<poi nt>& 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(col or::red); // ok, shape_drawer::s et_color is public
c.apply_filter( alpha_filter(0. 50)); // ok
c.draw();

vector<point> p;
p.push_back(poi nt(0, 0));
p.push_back(poi nt(3, 4));
p.push_back(poi nt(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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
6418
by: Jules Dubois | last post by:
I'm want to create a superclass with nothing but attributes and properties. Some of the subclasses will do nothing but provide values for the attributes. (I'd also like to make sure (1) that the subclass provides actual values for the attributes and (2) that no "client" module adds or removes attributes or properties, but I don't know how to do those.) I don't understand what I'm doing wrong, or maybe what I want to do is impossible. ...
6
6015
by: Jordi Vilar | last post by:
Hi All, Is there a way to dynamic_cast a pointer to a type defined by a type_info*? something like: type_info *info_of_type_to_cast = typeid(type_to_cast); type_to_cast *casted = really_dynamic_cast<info_of_type_to_cast>(my_data_ptr);
28
3408
by: Act | last post by:
Why is it suggested to not define data members as "protected"? Thanks for help!
19
4054
by: tthunder | last post by:
Hi @all, I've got an interesting problem. These are my classes: ---------------------- class fooBase {
4
7341
by: p988 | last post by:
using System; using System.Windows.Forms; using System.Drawing; class MyForm : Form { MyForm () { Text = "Windows Forms Demo"; }
175
8773
by: Ken Brady | last post by:
I'm on a team building some class libraries to be used by many other projects. Some members of our team insist that "All public methods should be virtual" just in case "anything needs to be changed". This is very much against my instincts. Can anyone offer some solid design guidelines for me? Thanks in advance....
4
1816
by: Tina | last post by:
This is an issue regarding what exactly is accomplished by using "Protected" when defining a variable. It seems it does much more than just applying Protected status to a variable. I have an ascx control named HeadingBar. I have dragged it onto an aspx page. If I use the following statement..... Dim HeadingBar1 as HeadingBar
7
3636
by: =?Utf-8?B?cmFtbzk5NDE=?= | last post by:
Hi, Is there any way disable using "protected override void OnPaint(..." method out of the assembly. I create usercontrol and i do not want user adds extra codes OnPaint method for securtity purpose. And i don't give a permission to call "CreateGraphics()". Thanks your responses.
0
8370
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8283
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8704
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8590
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7302
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5620
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4147
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4291
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1591
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.