Page 267 - Introduction to Microcontrollers Architecture, Programming, and Interfacing of The Motorola 68HC12
P. 267

244                                    Chapter 8 Programming in C and C++


            C++ object-oriented programming offers many useful features. Encapsulation
        associates variables with procedures that use them in classes, inheritance permits
        factoring out of procedures that are common to several classes, overriding permits the
        redefinition of procedures, polymorphism allows common names to be used for
        procedures, virtual functions permit different procedures to be selected at run time,
        information hiding protects data, template classes generalize classes to use different data
        types, and operator overloading permits a program to be written in the format of
        algebraic expressions. If the programmer doesn't have C++ but has a minimal C
        compiler, many of the features of object-oriented programming can be simulated by
        adhering to a set of conventions. For instance, in place of a C++ call Cstack. push () ,
        one can write instead StackPush() . Information hiding can be enforced by only
        accessing variables like QptrError in procedures like stackPush(). C++ gives us a
        good model for useful C conventions.
            Object-oriented programming has very useful features for designing state-of-the-art
        microcomputer's I/O device software, as proposed by Grady Booch in his tutorial
        Object-Oriented Computing. Encapsulation is extended to include not only instance
        variables and methods, but also the I/O device, digital, analog, and mechanical systems
        used for this I/O. An object is these parts considered as a single unit. For instance,
        suppose you are designing an automobile controller. An object (call it PLUGS) might be
        the spark plugs, their control hardware, and procedures. Having defined PLUGS, you call
        function members (for instance, SetRate( 10) to PLUGS), rather like connecting wires
        between the hardware parts of these objects. The system takes shape in a clear intuitive
        way as the function members are defined. In top-down design, you can specify the
        arguments and the semantics of the methods that will be executed before you write them.
        In bottom-up design, the object PLUGS can be tested by a driver as a unit before it is
        connected to other objects.
            An object can be replaced by another object, if the function calls are written the
        same way (polymorphism). If you replace your spark plug firing system with another,
        the whole old PLUGS object can be removed and a whole new PLUGS 1 object inserted,
        You can maintain a library of classes to construct new products by building on large
        pretested modules. Having several objects with different costs and performances, you can
        insert a customer-specified one in each unit.
            In this context, protection has clear advantages. If interchangeable objects avoid
        mismatch problems, then all public function and data members have to be defined exactly
        the same. Private and protected function and data members, by contrast, do not need to be
        defined exactly the same in each of the objects because they cannot be accessed outside
        the object. Make a function or data member public only if you will maintain the
        member's appearance and meaning in all interchangeable classes. Because there is no need
        to make private and protected members the same in all classes, you have more
        flexibility, so you make function and data members public only if you have to.
            Virtual functions have further advantages, if an object can be blessed to be one of a
        number of polymorphic classes at run time. For instance, output from a microcontroller
        can be sent to a liquid crystal display or a serial printer. The user can cause the output to
        be sent to either device without reloading, or in any way modifying, the
        microcontroller's program.
   262   263   264   265   266   267   268   269   270   271   272