Home Segments Index Top Previous Next

257: Mainline

The need-to-know principle: Generally, when you design classes to be used by other programmers, your classes will contain many more member variables and functions than you expect to be accessed by the functions written by those other programmers. By restricting access to your classes to the member variables and functions in public interface, you can revise and improve the other member variables and functions without worrying about whether other programmers have come to depend on member variables that disappear or functions whose behavior changes.

When you define a cylinder class, for example, you might choose to place a radius member variable in the private part, along with read_radius and write_radius functions in the public part. Your rationale would be that you could change later to a diameter-based definition without fear that anyone would have come to depend on direct access to the radius member variable. Instead, all cylinder users would have to use the read_radius and write_radius functions in the public interface, which you easily could redefine to work with a diameter member variable.