ABSTRACT DATA TYPES
We mentioned the abstract data type at the beginning of this chapter and again briefly midway through, and it is time to describe it a little more completely. An abstract data type is a group of data, each of which can store a range of values, and a set of methods or functions that can operate on that data. Since the data are protected from any outside influence, it is protected and said to be encapsulated. Also, since the data is somehow related, it is a very coherent group of data that may be highly interactive with each other, but with little interaction of its class outside the scope.
The methods, on the other hand, are coupled to the outside world through the interface, but there are a limited number of contacts with the outside world and therefore a weak coupling with the outside. The object is therefore said to be loosely coupled to the outside world. Because of the tight coherency and the loose coupling, ease of maintenance of the software is greatly enhanced. The ease of maintenance may be the greatest benefit of object oriented programming.
It may bother you that even though the programmer may not use the private variables directly outside of the class, they are in plain sight and he can see what they are and can probably make a good guess at exactly how the class is implemented. The variables could have been hidden completely out of sight in another file, but because the designers of C++ wished to make the execution of the completed application as efficient as possible, the variables were left in the class definition where they can be seen but not used.
A function outside of a class can be defined to be a friend function by the class which gives the friend free access to the private members of the class. This in effect, opens a small hole in the protective shield of the class, so it should be used very carefully and sparingly. There are cases where it helps to make a program much more understandable and allows controlled access to the data. Friend functions will be illustrated in some of the example programs later in this tutorial. It is mentioned here for completeness of this section. A single isolated function can be declared as a friend, as well as members of other classes, and even
entire classes can be given friend status if needed in a program. Neither a constructor nor a destructor can be a friend function.
THE struct IN C++
The struct is still useable in C++ and operates just like it does in ANSI-C with one addition. You can include methods in a structure that operate on data in the same manner as in a class, but all methods and data are automatically defaulted to be public in a structure. Of course you can make any of the data or methods private by defining a private section within the structure. The structure should be used only for constructs that are truly structures. If you are building even the simplest objects, you are advised to use classes to define them.
A VERY PRACTICAL CLASS
The examples of encapsulation used in this chapter have all been extremely simple in order to illustrate the mechanics of encapsulation. Since it would be expedient to study a larger example the date class is given below for your instruction. The date class is a complete nontrivial class which can be used in any program to get the current date and print it as an ASCII string in any of four predefined formats. It can also be used to store any desired date and format it for display.
Examine the file named DATE.H which is the header file for the date class. This file is so well commented that we don’t have much else to say about it. If you understand the principles covered in this chapter you should have no problem understanding this class. The first thing that is new to you is the reserved word protected which is used in line 12. We will define this word in a couple of chapters. Until then, pretend that it means the same thing as private and you will be close enough for this present example. The code in lines 8 and 9 along with line 55 will be explained shortly. For the present time, simply pretend those lines of code are not there. Also the keyword static as used in lines 16 and 17 will be explained later.
You should spend the time necessary to completely understand this class header, with the exception of the new things added, before going on to the implementation for this class.
The file named DATE.CPP is the implementation for the date class and once again, there is nothing unusual or difficult about this code. It uses very simple logic to store and format the date in a usable manner. You should study
this code until you understand it completely before going on to the next example which will use the date class in a main program.
The very simple program named USEDATE.CPP is a main program that uses the date class to list the current date and another date on the monitor. Once again, you should have no problem understanding this program so nothing more will be said about it.
You should spend the time necessary to understand these three files because they are the starting point for a practical track in the next few chapters. This class will be used in conjunction with others to illustrate single and multiple inheritance. Even though you do not understand all of the details of these files, spend enough time that you are comfortable with the structure and the major points of them.