Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Private definition

Posted on 1997-12-09
5
Medium Priority
?
209 Views
Last Modified: 2010-05-18
Given:

class c
{
     private:
          ... // Attributes
     public:
          ... // Methods
};

Since the private part, in my example the "Atributes", of the class are not accessible to clients, why must they be listed in the class definition? Obviously the program needs it there, but why?
0
Comment
Question by:josey
[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
  • 3
  • 2
5 Comments
 

Author Comment

by:josey
ID: 1175614
Edited text of question
0
 
LVL 23

Accepted Solution

by:
chensu earned 150 total points
ID: 1175615
That is for the clients to access the public members correctly. The memory allocation follows the class declaration. Otherwise, the memory pointers are different.

Given:
class c
{
    private:
        short m;  // 2 bytes
    public:
        short n;  // 2 bytes
} test;

If you use the following for the clients
class c
{
    //private:
        //short m;
    public:
        short n;  // 2 bytes
} test;

test.n will access the private member m actually.
0
 

Author Comment

by:josey
ID: 1175616
The answer is right, and I know how to declare the class parts. Still I need to answer to my students :WHY MUST I LIST THE PRIVATE PARTS OF THE CLASS SINCE THESE ARE NOT ACCESSIBLE BY THE CLIENTS? I OTHER WORDS, WHY NOT JUST FORGET ABOUT LISTING THEM? THE CLIENT DOES NOT NEED THEM ANYWAY. WHY MUST THESE DEFINITIONS MUST APPEAR WITHIN THE CLASS DEFINITION? I am looking for an explanation and not code examples?
0
 
LVL 23

Expert Comment

by:chensu
ID: 1175617
You seem not to read my answer carefully. I did explain it. The C++ compiler memory allocation follows the class declaration. If you don't list the private parts, the object memory will be totally different from what it is really.
0
 

Author Comment

by:josey
ID: 1175618
Thanks for your answer!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
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 difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

730 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