Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Private definition

Posted on 1997-12-09
5
Medium Priority
?
210 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 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
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 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 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.
Suggested Courses

926 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