English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

If you declare a class member as "private" in C++ or Object Pascal (Delphi), derived classes can't refer to or use it. What's the point? How could the person writing the base class possibly know that no derived classes in the future will ever need to use the private members? Why does the language even allow that?

2007-10-26 09:25:03 · 2 answers · asked by rainfingers 4 in Computers & Internet Programming & Design

2 answers

Maybe the private methods are used by other public methods within the base class which are more useful - but the private methods are more flexible but without a defined interface.

For example, a multi-programmer project. Fred knows how to communicate with USB devices, but since talking to some devices without his expertise may result in them exploding or losing data, he prevents the other programmers from doing anything dangerous, so only makes the safe methods available.

Jim knows how to talk to a camera using Fred's base class, so he writes a new derived class, but can only access the safe methods since he might cause the camera to discharge its battery into the PC if he doesn't know what he's doing on the USB interface.

A few years later, USB3 protocol is invented. Fred then re-writes his private methods to cope with the change in technology, and Jim just needs to re-link his code without modifying it. The public methods stay the same, but the internal ones are modified in the base class to cope.

There are probably more reasons why a base class may not wish to share all of its functions with derived classes - such as security or encryption where certain internal workings should not be used by derived classes.

2007-10-26 11:27:08 · answer #1 · answered by nibblesthemouse 4 · 0 0

All variables could desire to pick to desire to via skill of a few skill be initialized someplace. as quickly as you have elementary information types in a class (like an int or a double) they could desire to be initialized on your constructor. as quickly as you have an merchandise as a class member then that merchandise will in all possibility be initialized in *its* constructor, so as which you in all possibility do now now no longer could desire to pick to desire to be in touch approximately it.

2016-12-30 06:53:29 · answer #2 · answered by ? 3 · 0 0

fedest.com, questions and answers