Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
Having a constructor that sets the values of the data members of a class object but still admits the possibility of any part of a program being able to mess with what are essentially the guts of an object is almost a contradiction in terms. To draw an analogy, after you have arranged for a brilliant surgeon such as Dr. Kildare, whose skills were honed over years of training, to do things to your insides, letting the local plumber or bricklayer have a go hardly seems appropriate. You need some protection for your class data members.
You can get the security you need by using the keyword private when you define the class members. Class members that are private can, in general, be accessed only by member functions of a class. There's one exception, but we'll worry about that later. A normal function has no direct means of accessing the private members of a class. This is shown in Figure 7-6.