Solved

Private definition

Posted on 1997-12-09
5
204 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

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

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

830 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