Solved

10. Why we need public and private keywords?

Posted on 2015-01-23
6
74 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
ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
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.

831 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