Solved

Private definition

Posted on 1997-12-09
5
201 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
  • 3
  • 2
5 Comments
 

Author Comment

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

Accepted Solution

by:
chensu earned 50 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

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

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…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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.

863 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

24 Experts available now in Live!

Get 1:1 Help Now