Solved

Private definition

Posted on 1997-12-09
5
200 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
Comment Utility
Edited text of question
0
 
LVL 23

Accepted Solution

by:
chensu earned 50 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks for your answer!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 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.

763 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

7 Experts available now in Live!

Get 1:1 Help Now