Solved

Reason for Private part os the class definition

Posted on 1997-12-09
2
183 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
MSVCR80.dll crash 2 126
QAction use more than one time. 3 20
Handling string inputs in C/Linux 23 168
best sources to up-to-date in C++? 8 56
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

747 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

12 Experts available now in Live!

Get 1:1 Help Now