Solved

Reason for Private part os the class definition

Posted on 1997-12-09
2
196 Views
Last Modified: 2010-04-10
Since the the private part of the class is not accessible by the clients, why must its parts be listed within the class definition. 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 a short explanation and not code examples? I know the definition must be there, what I need to know is WHY?
0
Comment
Question by:josey
  • 2
2 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 50 total points
ID: 1175619
answer comming.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1175620
The simple answer:
To make life easy for the programmers who implement (write) the c++ compilers.

The longer answer:
When an object is created the compiler must create the entire class, that is, it must allocate and initialize all of the class (all data members, not just public ones).  Although client code cannot fool with a class's private data it does need to initialize it and it does need to know that it is there so that it knows how big a class is.  Now it would be possible to get around this.  A language could produce two versions of a class definition.  One for client's to "know" about.  And one for the compiler and class designer to use.  But that's more work for the language writters.  C++ was written by one overworked man who didn't do anything he didn't have to do.

I hope this helps.  I'm partial to using code examples.
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

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…
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.

815 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

11 Experts available now in Live!

Get 1:1 Help Now