Solved

10. Why we need public and private keywords?

Posted on 2015-01-23
6
77 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 77

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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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 33

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
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 be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

830 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