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

8.7 Object-oriented Programming in C++                               239


        Wherever the symbolic name s appears, the compiler substitutes (*sptr) in its place.
        Note that *ptr.member is the same as ptr->member. So this makes the syntax of
        the use of pointers to objects match the syntax of the use of objects most of the time.
        However, the blessing of the object explicitly uses the pointer name.
            A hierarchy of derived and base classes, inheritance, overriding, and factoring are all
         related ideas. These are described below, in that order.
            A class can be a derived class (also called subclass) of another class, and a
        hierarchy of classes can be built up. We create derived classes to use some of the data or
        function members of the base class, but we can add members to, or replace some of the
        members of, the base class in the derived class. For instance the aforementioned class
        Cstack can have a derived class istack for int variables; it declares a potentially
         modifiable constructor and different function members pull and push for its stack.
         When defining the class Istack the base class (also called superclass) of Istack is
        written after its name and a colon as : public Cstack. A class such as Cstack, with no
        base class, is called a root class; it has no colon and base class shown in its declaration,
        class Istack:public Cstack
          {public:Istack(char)?virtual void push(int);virtual int pull(void);};

         Istack::Istack(char i) : Cstack(i & -1) {}

        void Istack:: push (int i)
          {if(Ptr==Top) {Error=l; return;} *(++Ptr) = i»8; *(++Ptr)=i; }

         int Istack:: pull ( )
          {int i;if(Ptr==Top){Error=l;return 0;} return *(Ptr—)|(*(Ptr-)«8);}
            The notion of inheritance is that an object will have data and function members
        defined in the base class(es) of its class as well as those defined in its own class. The
        derived class inherits the data members or function members of the parent that are not
        redefined in the derived class. If we execute Istack::error then the function member
        Cstack: :error is executed, because Istack does not declare a different error
        function member. If a function member cannot be found in the class that the object was
        declared or blessed for, then its base class is examined to find the function member to be
        executed. In a hierarchy of derived classes, if the search fails in the class's base class, the
        base class's base class is searched, and so on, up to the root class. Overriding is the
        opposite of inheritance. If we execute Sptr->push( 1) ;, function member
         istack: :push is executed rather than Cstack: :push,because the class defines an
        overriding function member. Although we did not need additional variables in the derived
        class, the same rules of inheritance and overriding would apply to data members as to
        function members.
            Most programmers face the frustration of several times rewriting a procedure, such
        as one that outputs characters to a terminal, wishing they had saved a copy of it and used
        the earlier copy in later programs. Commonly reused procedures can be kept in a library.
        However, when we collect such common routines, we will notice some common parts in
        different routines. Common parts of these library procedures can be put in one place by
        factoring. Factoring is common to many disciplines—or instance, to algebra. If you
   257   258   259   260   261   262   263   264   265   266   267