Link to home
Start Free TrialLog in
Avatar of josey
josey

asked on

Reason for Private part os the class definition

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?
ASKER CERTIFIED SOLUTION
Avatar of nietod
nietod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of nietod
nietod

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.