Solved

Reason for Private part os the class definition

Posted on 1997-12-09
2
201 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone 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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
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.

630 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