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

8,7 Object-oriented Programming in C++                               24!

        object; the function member names and arguments do not have to be changed. You don't
        have to generate obscure names for functions to keep them separate from each other.
        Moreover, in C++, the number and types of operands, called the function's signature, are
        part of the name when determining if two functions have the same name. For instance,
        push {char a) is a different function than push(int a).
            A function member that is declared virtual will be called indirectly instead of by a
        BSR or equivalent instruction. The program jumps indirectly through a list of function
        member entry addresses, called a viable. The blessing of the object links the calls to the
        object's function members through this viable. If an object is blessed for a different
        class, another class's vtable is linked to the object, so that calling the object's function
        members will go to the class's function members for which it is blessed.
            When object pointers are blessed at run time and have virtual function members, if a
        virtual function member appears for a class and is overridden by function members with
        the same name in its derived classes, the sizes and types of all the arguments should be
        the same, because the compiler does not know how an object will be blessed at run time.
        If they were not, the compiler would not know how to pass arguments to the function
        members. For this reason, we defined the arguments of cstack's push and pull
        function members to be int rather than char, so that the same function member name
        can be used for the int version or a char version. This run-time selection of which
        class to assign to an object isn't needed with declarations of objects, but only with
        blessing of object pointers, because the run-time program can't select at compile time
        which of several declarations might be used. Also the pointer to the object must be
        declared an object of a common base class if it is to be used for several class.
            Information hiding limits access to data or function members. A member can be
        declared public, making it available every where, protected, making it available only to
        function members of the same class or a derived class of it, or private, making it
        available only to the same class's function members and hiding it from other functions.
        These words appearing in a class declaration apply to all members listed after them until
        another such word appears; the default if no such words appear is private. The data
        member Error in the class Cstack cannot be accessed by a pointer in main as in i
        =S. Error or i =Sptr->Error because it is not public, but only through the public
        function member error () . This way, the procedure main can read (and automatically
        clear) the Error variable but cannot accidentally or maliciously set Error, nor can it
        read it, forgetting to clear it. You should protect your data members to make your
        program much more bug-proof. Declare all data and function members as private if
        they are to be used only by the class's own function members, declare them protected
        if they might be used by derived classes, and declare them public if they are used
        outside the class and its derived classes.
            Templates generalize object-oriented programming. A template class is a class that
        is defined for an arbitrary data type, which is selected when the object is blessed or
        declared. We will define a templated class Stack. The class declaration and the function
        members have a prefix like template <class T> to allow the user to bless or declare
        the object for a specific class having a particular data type, as in s = new
        stack<char> (10). The generalized class definition is given below; you can substitute
        the word char for the letter T every where in declarations or class function members.
   259   260   261   262   263   264   265   266   267   268   269