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

236                                   Chapter 8 Programming in C and C++


         Nevertheless, though less than perfect, one can use a fixed code that is based on other
         statistics if the statistics are reasonably similar. Finally, although the code is almost
         unbreakable without the decoding tree, if any bit in the bit string is erroneous, your
        decoding routine can get completely lost. This may be a risk you decide to avoid because
        the code has to be sent through a communications link that must be as error free as
        possible.


        8.7 Object-Oriented Programming in C++

        The concept of object-oriented programming was developed to program symbolic
        processes, database storage and retrieval systems, and user-friendly graphic interfaces.
        However, it provides a programming and design methodology that simplifies the
        programming of microcontrollers and systems that center on them.
            Object-oriented programming began with the language SMALLTALK.
        Programmers using C wanted to use object-oriented techniques. Standard C cannot be
        used, but a derivative of C, called C++, has been developed to utilize objects with a
        syntax similar to that of C. Although a 6812 C++ compiler was not available to the
        author when this book was written, the Metrowerks C++ compiler was used to generate
        code for 68332 and 68340-based microcontrollers to check out the ideas described below.
            C++ has a few differences from C. C++ permits declarations inside expressions, as
        infer (int i = 0; i < 10; i++). Parameters can be passed by name using a
        PASCAL-like convention; & in front of a formal parameter is like VAR. See the actual
        parameter a and corresponding formal parameter b below:
                void main(){ char a;                 void f(char &b) {
                    f(a);                                b = '!';
                >                                    }
            An object's data are data members, and its procedures are function members; data
        and function members are encapsulated together in an object. Combining them is a
        good idea because the programmer becomes aware of both together and logically separates
        them from other objects. As you get the data, you automatically get the function
        members used on them. In the class for a character stack shown below, observe that data
        members error, Bottom, Top, and Ptr are declared much as in a C struct, and
        function members push, pull, and error are declared like prototypes are declared in C.
        Protection terms, protected, public, and virtual, will be soon explained.
        class Cstack {                       // definition of a class
             protected:                      // members below are not available outside
                  char Error;                // a data member to record errors
                  int *Bottom, *Top, *Ptr;   // data members to point to the stack
             public:                         IS members below are available outside
                  Cstack (char);             // constructor, used to initialize data members
                  virtual void push (int);   // function member to push onto stack
                  virtual int pull (void);   // function member to pull from stack
                  virtual char error (void); // function member to check on errors
        ;.•
   254   255   256   257   258   259   260   261   262   263   264