954,087 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Explain why and when do we use protected instead of private.

can anyone explain whether this is correct or else help me to write a proper answer plsss


answer.................


Private data members cannot be accessed outside the class. When a class inherits a base class, all the data members except the private get inherited into it. So if we want data members to be accessible to only derived classes and not privately or publicly accessible, then we can use protected.

precila
Newbie Poster
11 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

Hi Precila. Your question makes absolutely no sense at all.:) When you say protected and private, what do you mean by that?

AndreRet
Senior Poster
3,918 posts since Jan 2008
Reputation Points: 334
Solved Threads: 350
 

this is one of the questions i got 4 my assignment

so thougth of answering this by explaining what is protected and what is private mean

dont know whether my idea is correct cauz i hav no clue about this

precila
Newbie Poster
11 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

>Private data members cannot be accessed outside the class.
Yes.

>When a class inherits a base class, all the data members except the private get inherited into it.
Private members are inherited, but not accessible from the child class.

>So if we want data members to be accessible to only derived classes
>and not privately or publicly accessible, then we can use protected.

While not incorrect, this answer is the kind of cookie cutter thing you'd get right out of a textbook. It shows knowledge, but not necessarily understanding.

A better answer would give a practical example for using protected, explain why it's preferred, then derive a more general guideline from the explanation. This covers all bases in that it shows you can think of a good situation where protected makes sense (understanding of the concept), defend the decision (understanding the pros and cons versus alternatives), and draw a logical conclusion (show creativity and independent thought).

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

tanx AndreRet and Narue you guy's were really helpfull tanx a lot 4 helping me to complete this

tanx a lot agian friends

precila
Newbie Poster
11 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

It was a pleasure.:)

AndreRet
Senior Poster
3,918 posts since Jan 2008
Reputation Points: 334
Solved Threads: 350
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: