Solved

10. Why we need public and private keywords?

Posted on 2015-01-23
6
81 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 79

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
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
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 34

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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying 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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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…

615 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