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

8.7 Object-oriented Programming in C++                              237


            A class's function members are written rather like C procedures with the return type
        and class name in front of two colons and the function member name.

        void Cstack::push{int i){if(Ptr==Top){Error=l; return?} *(++Ptr)=i; }

        int Cstack::pull{){if(Ptr==Bottom){ Error=l; return 0;} return *(Ptr—);}

        char Cstack::error{){ char i; i = Error; Error = 0; return i; }

            Any data member, such as Top, may be accessed inside any function member of
        class Cstack, such as push(). Inside a function member, when a name appears in an
        expression, the variable's name is first searched against local variables and function
        formal parameters. If the name matches, the variable is local or an argument. Then the
        variable is matched against the object data members and finally against the global
        variables. In a sense, object data members are global among the function members,
        because each of them can get to these same variables. However, it is possible that a data
        member and a local variable or argument have the same name such as Error. The data
        member can be identified as this->Error, using key word this to point to the object
        that called the function member, while the local variable or argument is just Error.
            C++ uses constructors, allocators, destructors, and deallocators. An allocator
        allocates data member storage. A constructor initializes these variables; it has the same
        function name as the class name. Declaring or blessing an object automatically calls the
        allocator and constructor, as we will see shortly. A destructor terminates the use of an
        object. A destructor has the same function name as the class name but has a tilde (~) in
        front of the function member name. A deallocator recovers storage for data members for
        later allocation. We do not use a deallocator in our experiments; it is easier to reset the
        6812 to deallocate storage. Here's Cstack's constructor:
        Cstack::Cstack(int i){Top=(Ptr=Bottom=(char*)allocate(i))+i;Qlen=
        Error=0;}

            Throughout this section, a conventional C procedure allocate provides buffer
        storage for an object's data members and for an object's additional storage such as its
        stacks. The contents of global variable free are initialized to the address just above the
        last global; storage between free and the stack pointer is subdivided into buffers for
        each object by the allocate routine. The stack used for return addresses and local
        variables builds from one end and the allocator builds from the other end of a common
        RAM buffer area, allocate's return type void * means a pointer to anything.

        char *free=0xb80;
        void *allocate(int i) { void *p=free; free += i; return p; }

             A global object of a class is declared and then used as shown below:
                                Cstack S(10);
                                void main() { int i;
                                   S.push(l); i = S.pull();
                                }
   255   256   257   258   259   260   261   262   263   264   265