Solved

10. Why we need public and private keywords?

Posted on 2015-01-23
6
63 Views
Last Modified: 2015-01-24
Why we need public and private keywords?
Comprehensive explanation and examples are welcome.
0
Comment
Question by:Nusrat Nuriyev
6 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 40567601
'public', 'protected' and 'private' both control access to date members and member functions in classes. Variables and functions that are declared 'public' are accessible from outside the class, 'protected' ones only from within the class and derived classes, 'private' members can only be accessed from within the class they are used.

This site has ery good examples, (and I don't want to just copy them and paste them here): http://www.learncpp.com/cpp-tutorial/83-public-vs-private-access-specifiers/

Then, there is also 'public', 'protected' and 'private' inheritance, but that's a different story covered here: http://www.programiz.com/cpp-programming/public-protected-private-inheritance
0
 
LVL 76

Expert Comment

by:arnold
ID: 40567605
is this a homework assignment?

Post what you think and have individuals here confirm or correct your thinking.
It seems you have posted quite a flurry of questions similarly worded.
0
 

Author Comment

by:Nusrat Nuriyev
ID: 40567630
>>>  is this a homework assignment?
Yes, it's the homework assignment. For my students.

I will try to skip overly simple or similar-like questions.

Ok, here is other subquestion: how to get the zen of determining when I need protected or private modifiers?
With public access modifier it's more or less clear. May you recommend something in this area?
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 86

Accepted Solution

by:
jkr earned 250 total points
ID: 40567640
In most cases, 'protected' is fine. I'd use 'private' only in a case where I provide a base class for others to work with, but want some data to not be visible to them. And honestly, these cases are rare...
0
 
LVL 32

Assisted Solution

by:sarabande
sarabande earned 250 total points
ID: 40568035
in my opinion 'protected' keyword for data members violates the principles of 'information hiding' and 'encapsulation' as it provides unlimited access to members of the class for derived classes. 'information hiding' principle requires to have private data members only and provide access to private members only by the interface as far as it is necessary. encapsulation means that the class should encapsulate all required functionality regarding of its (private) members such that a derived class doesn't need to access the member variable directly but only via the existing interface.

as a sample you may consider a piece of text stored in a private member of a base class as utf-8 encoded string. if you allow direct access to the internal string variable, all these derived classes must be aware of the utf-8 encoding and do appropriate conversion. moreover, if  the text was modified by the derived class it also must ensure that the encoding and probably other restrictions of size limits or visibility or existing locks were still guaranteed. this task rapidly may grow to a nightmare for software maintenance.

Sara
0
 

Author Comment

by:Nusrat Nuriyev
ID: 40568166
Sara, thank you for your example.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now