Solved

10. Why we need public and private keywords?

Posted on 2015-01-23
6
69 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
c++ mixing int and strings in multi array 8 71
returning a dereferenced pts in C++ 10 141
Writing a parser for java language 4 71
FMX enumerated colours 2 84
When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

943 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

1 Experts available now in Live!

Get 1:1 Help Now