Removing Implementation Details from C++ Class Declarations

Removing Implementation Details from C++ Class Declarations

Removing Implementation Details from C++ Class Declarations Mark R. Headington Computer Science Department University of Wisconsin-La Crosse La Crosse, WI 54601 [email protected] Abstract that defines, for both the user and the implementor, the services provided by the ADT without reference to imple- Data abstraction= concept introduced at varying places in mentation details. The implementation encapsulates the the CS l/CS2/CS7 sequen~separates the properties of a concrete data representation and the program code for the data type (its values and operations) fmm the implementa- ADT operations. Client code cannot access encapsulated tion of that type. This separation of spxification fmm data except through the operations provided in the interface. implementation is achieved by encapsulating the implemen- This separation of specifkation tlom implementation yields tation so that users of the type can neither access nor be three important beneti~ influenced by the implementation details. Ideally, therefore, the specifkation should be implementation-independent, ● The user does not have to know how the abstraction The C++ class mechanism compromises information works in order to use it correctly, hiding by requiring the interface to include information-the ● The implementor is guaranteed that client code cannot private part of the class &claratio*that is needed only for compromise the correctness of the implementation. implementation pqoses. This paper describes two tech- ● Changes to the implementation do not require clients to niques for removing details of implementation structure be reprogrammed and recompiled. from the C+ class declaration and discusses the advantages and disadvantages of each, In C++, the private part of a class declaration can-and usually does-include the declarations of the 1. Introduction concrete data representation of an ADT At some point in the CS1/CS2/CS7 sequence, data abstrac- #include “bool .h” / / Declarations for tion is introduced as a tool for managing complexity in // Boolean type software systems. Data abstraction, the separation of the class Int Stack { properties of a data type from its implementation, depends public: heavily on encapsulation-the technique of minimizing Boolean IsEmpty ( ) Const; interdependencies among separately-mitten modules by Boolean IsFull ( ) const; defining strict external interfaces Nica88, Snyd86]. void Push ( int ) ; In object-oriented programming languages, data int Top ( ) const; void Popo; abstraction and encapsulation are provided by the class IntStack ( ) ; // Constructor mechanism. The class, a program representation of an private: abstract data type (ADT), encapsulates both structure (the lnt data [MAX_DEPTH] ; concrete duta representatwn of the abstract data) and int top; behavior (algorithms that implement operations on the }; abstract data). Encapsulation follows because an ADT The disadvantage is that the interface represented by such defines only the abstract properties of the type, not the a class declaration is not implementation-independent. The concrete data representation or the implementations of the declaration of Int Stack clearly discloses the data repre- associated operations, sentation- int array and a variable to keep track of the To maximize the benefits of encapsulation, the ADT top of the stack. Although the reserved word private (hen% class) is composed of two parts: the specifkxtion prevents client code from accessing the data representation and the implementation, The specification is the interface directly, encapsulation and information hiding are violated Permission to copy without fee all or part of this material is in ways this paper will discuss. Sections 2 and 3 describe granted provided that the copies are not made or distributed for the problem as it affects encapsulation and information direet commercial advantage, the ACM copyright notice and the title of the publication and Its date appear, and notice is given hiding, and Section 4 presents two programming techniques that COPYI is by permlseion of the Association of Computing for stnmgthening the separation of specitkation from Machinery.Y o COPYotherwise, or to republish, requkas a fee implementation opaque types and abstract classes. %#E??W?W#%lle TN”SA @ 1995 ACM O-89791-8!k3-x195/0003....50.5O 24 2. Encapdabn facilities to concentrate on the essentials. Infor- mation hiding makes this possible by separating In C++ classes, encapsulation is enforced, in one sense, by function tiom implementation, and should be strict access controls in the form of the private and viewed by client programmers as help rather than protect ed reserved words. Components of the class that hindrance. are private and protected are inaccessible to client code, Yet encapsulation is weakened by requiring that the At best, the visibility of an ADT’s data representation private part be located in the interface. If the implementor is a distraction whereby the user may spend time speculat- changes the ADT’s data representation, all client code must ing about further implementation details. At worst, the user be recompiled as well as relinked. This dependency is may misinterpret the data representation and make decisions inconsistent with the very notion of encapsulation. based on the misinterpretation, Here is a case in point. Stroustrup addresses this issue in Z%e C++ Program- In my CS2 course, the textbook llhmd94] proceeds ming Lunguage [Stro91]: slowly from abstraction to implementation. Students are introduced to the properties of common data struc- Why, you may ask was C++ designed in such a tures-lists, stacks, queues, sets-long before they learn way that recompilation of users of a class is how to implement them. For an assignment using queues necessary after a change to the private part? as abstractions, I gave the students the specification of a And why indeed need the private part be present character queue class (as a . h file) and the compiled in the class declaration at all? In other words, form-but not the source code-of the implementai~on since users of a class are not allowed to access (. CPP) file. The assignment involved falling a queue and the private members, why must their declarations processing the characters in certain ways. This is the be present in the header files the user is sup- header file, abridged by deleting comments: posed to read? The answer is run-time ejicien- cy. On many systems, both the compilation // Header file cqueue .h process and the sequence of operations imple- #include “bool .h” menting a function call are simpler when the size of automatic objects (objects on the stack) is const int MAX_LENG = 101; known at compile time. class CharQueue { C++ is often used for applications nx@ring efficienc public: Boolean IsEmpty ) const; compact code. Particularly in system programming, size Boolean IsFull (; const; and speed of programs can be critical. For many applica- void Enqueue char ) ; tions, however, run-time efficiency is secondary to the char Front ( ) const; modukity and pliability afforded by strong encapsulation void Dequeue ); of implementation details, Section 4 of this paper describes CharQueue ( ) ; private: how to remove details of the data representation tlom the int front; class declaration. int rear; char data [MA2_LENG ] ; 3. Information Hiding ); Related to encapsulation is the concept of information The constant MALLENG is purely au implementation detail. hiding-the suppression of implementation details that do It is needed only to tell the compiler the size of the array not affect the use of an abstraction. Even though the representing the queue. private members of a C++ class are inaccessible to client Most students used the I SFU 11 ( ) operation to code, they are visibly exposed to the user as a human. Here determine when the queue was full. But some students is Bertrand Meyer’s perspective on information hiding bypassed this operation, assuming (incorrectly) that the meye88]: maximum queue length was 101. These students ran into trouble because the implementation algorithms use a circular It is worth recalling ... that the purpose of infor- buffer in which one array element must always be left mation hiding is abstraction, not protection. We empty. The maximum queue length is therefore 100, not do not necessarily wish to prevent client pro- 101. Had the private data representation and MAX.LENG grammers from accessing secret class elements, been absent from the header file, the students would h:we but rather to relieve them from having to do so. used ISFU11 ( ) as intended. In a software project, programmers are faced with too much information, and need abstraction 25 4. Decoupling Teehniquee struct PrivateRec { int front; Given the desirability of completely decoupling an ADT’s int rear; char data [MAX_LENG ] ; specification from its implementation, there are two tech- }; niques for removing details of the concrete data representa- tion flom a C++ class declaration, In this discussion it is CharQueue :: CharQueue ( ) / / Constructor assumed that the class declaration appears in a specifkation { (. h) fde and the class implementation appears in an p = new PrivateRec; p-> front = O; implementation (. cpp) file, p->rear = O; } 4.1 Opaque Types CharQueue: : -CharQueue ( ) // Destructor An opaque type is a record (st ruct) type whose forward { delete p; declaration appears in the specification fde and whose } complete declaration is hidden away in the implementation ffle. ‘Ile private data in the class declaration consists of

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    5 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us