    <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:admin="http://webns.net/mvcb/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:content="http://purl.org/rss/1.0/modules/content/">
     <channel>
        <title>ACCU  :: Putting Jack in the Box</title>
        <link>https://members.accu.org/index.php/articles/606</link>
        <description>Professionalism in Programming</description>
        <dc:language>en-us</dc:language> 
        <dc:creator>Administrator</dc:creator> 
        <admin:generatorAgent rdf:resource="http://www.xaraya.org" /> 
        <admin:errorReportsTo rdf:resource="mailto:webeditor@accu.org" />
       <sy:updatePeriod>hourly</sy:updatePeriod>
       <sy:updateFrequency>1</sy:updateFrequency>
       <docs>http://backend.userland.com/rss</docs>




<div class="xar-mod-head"><span class="xar-mod-title">Programming Topics + Overload Journal #6 - Mar 1995</span></div>

<table border="0" cellpadding="1" cellspacing="0">
    <tbody>
    <tr>
        <td valign="top">
            Browse in :
       </td>
       <td valign="top">

                                            <a href="https://members.accu.org/index.php/articles/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c13/">Topics</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c65/">Programming</a>
<br />

                                            <a href="https://members.accu.org/index.php/articles/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c76/">Journals</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c78/">Overload</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c177/">06</a>
<br />

                                            <a href="https://members.accu.org/index.php/articles/c65-177/">Any of these categories</a>

                    -                        <a href="https://members.accu.org/index.php/articles/c65+177/">All of these categories</a>
<br />
</td>
   </tr>
   </tbody>
</table>




<div class="xar-error">
   <p>
 <strong>Note:</strong> when you create a new publication type,
the articles module will automatically use the templates
<em>user-display-[publicationtype].xt</em>
and <em>user-summary-[publicationtype].xt</em>.
If those templates do not exist when you try to preview or display a new article,
you'll get this warning :-)  Please place your own templates in themes/<em>yourtheme</em>/modules/articles . The templates will get the extension .xt there. </p>
</div>
<div class="xar-norm xar-standard-box-padding">
   <h1><strong>Title:</strong>&nbsp;Putting Jack in the Box</h1>
<p><strong>Author:</strong>&nbsp;</p>
<p>
<strong>Date:</strong> 27 March 1995 18:22:18 +01:00 or Mon, 27 March 1995 18:22:18 +01:00</p>
<p><strong>Summary:</strong>&nbsp;</p>
<p><strong>Body:</strong>&nbsp;<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e18" id="d0e18"></a></h2>
<h3>A question</h3>
</div>
<p>One day I will get around to providing the answers to some
problems. I feel a bit guilty at the moment as all I seem to do is
ask for the advice of those more experienced than I, in the hope I
can learn something.</p>
<p>This will be the third article I have written for C Vu/Overload
and I will take my usual approach of posing a problem (or two) and
hoping that somebody will answer it (them).</p>
<p>This particular problem is fairly easy to define and, I suspect,
is a problem that a lot of people new to C++ have. Like me, I
suspect they &quot;program round it&quot; and as a result get into an
horrendous mess as the project develops.</p>
<p>But, I suspect, that the problem I am about to describe is
fundamental to OO programming and unless you have an understanding
of the concepts then you are likely to get into the mess that I am
currently in.</p>
<p>Let us say we have an object which we'll call a box. The aim of
this object is to allow you to draw a box on the screen and
manipulate it in various ways. For example you can change the
colour of the border, you can change the background colour, you can
print text in it, you can enquire upon its size (width and depth)
etc. etc.</p>
<p>The attributes of the box (such as co-ordinates, colour, text,
text font, text size etc.) are all held within data members that
are part of the class.</p>
<p>Member functions are used to retrieve this information and other
member functions provide the facilities to draw the box, display
text etc. etc.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e34" id="d0e34"></a>So far so
good</h2>
</div>
<p>Now let us assume we have another class. This class, for the
benefit of this example, draws a picture of a man on the screen
(we'll assume we have created an object of man called Jack).</p>
<p>The data members of the &quot;man&quot; class define attributes like his
size, his colour etc. etc.</p>
<p>Member functions would take care of drawing Jack, setting the
colour and would also provide the class with the ability to tell
the outside world about its attributes e.g. {return colour} .</p>
<p>Still looking good (at least I hope it is).</p>
<p>Now to the problem. What do you do if you want to draw Jack
inside the &quot;box&quot;?</p>
<p>In essence how do you join the two classes together.</p>
<p>I see a couple of options.</p>
<p>You could do something like this.</p>
<pre class="programlisting">
Jack.drawman(box);
</pre>
<p>and in the drawman function prototype you would have</p>
<pre class="programlisting">
int man::drawman(Box&amp;);
</pre>
<p>This passes by value the address of the box object to the
drawman member function. The drawman function could now do this</p>
<pre class="programlisting">
x=box.returnx;
y=box.returny;
</pre>
<p>It can now use the x and y co-ordinates to position the man on
the screen.</p>
<p>In effect, by passing the man class the address of the box
class, the man class is able to call the public member functions of
the box class. This, at first sight, might seem like a good idea
but it seems to link the man and box class together. This means the
man and box object have to &quot;know&quot; about each other, even though
they might not &quot;need&quot; each other.</p>
<p>What do you do for example, if you just wanted to draw Jack
without putting him in the box. I suppose you could overload the
drawman function so that &quot;normal&quot; x and y co-ordinates could be
passed but it all seems a bit messy. Also, when compiling the man
class, you would still need to have access to the box class which
seems a bit wasteful.</p>
<p>Another option could be to do this</p>
<pre class="programlisting">
x=box.returnx;
y=box.returny;
Jack.drawman(x,y);
</pre>
<p>or (more simply?)</p>
<pre class="programlisting">
Jack.drawman(box.returnx,box.returny);
</pre>
<p>The above code calls the drawman member function and tells it
the co-ordinates of the box. These co-ordinates could be used to
tell the man where to draw himself.</p>
<p>This seems better but it still seems a bit messy. You are
manipulating two objects in the &quot;main&quot; (or controlling code) when,
if I understand OO correctly, you should be able to say to the man
object &quot;Go away and draw yourself and don't forget that you need to
draw yourself within the confines of that box we talked about
earlier.&quot;</p>
<p>Can you say something like this in C++? In essence allowing the
man class to know about the box class when it needs to but, if you
are just drawing the man on the screen directly, the man has no
knowledge of a box class.</p>
<p>As I said at the beginning on this article you can &quot;program
round&quot; this problem, and I have shown two possible ways, but I
suspect there are hundreds of others but what is the proper C++/OO
method?</p>
<p>Bear in mind that I have only described two classes being
&quot;linked.&quot; The project I was working on has nine boxes on the screen
and I ended up passing all nine box addresses to most functions.
You can't tell me that is good programming?</p>
<p>I suspect inheritance will come into it somewhere but how? Do
you derive a class from man and box and somehow link the
co-ordinates of both classes? And how would you use inheritance if
you have many classes involved?</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e89" id="d0e89"></a>An answer from
The Harpist</h2>
</div>
<p>When Francis passed Graham's article to me, my first instinct
was to write a short article for C Vu. The longer I thought about
it the more it seemed to me that a fully satisfying answer is
rather beyond the scope of the defined targets of C Vu. Overload
seemed to be the right place so here is my first submission to C
Vu's sibling publication.</p>
<p>The first problem we must tackle is at the design stage. Both
'jack' and the 'box' are objects but what kind of objects are they?
The novice just sees them as simple objects and programs them in a
static form. Note that in this context static means an object that
is fully defined and unambiguously identified at compile time
whereas I use dynamic to refer to objects whose full properties
will only be determined at run time. I hope that by the end of this
article you will be comfortable with this pair of concepts and will
be better able to relate them to object-based (OBP) versus
object-oriented programming (OOP).</p>
<p>Almost all programmers pass through a stage of confusing OBP
with OOP. Each has its place, its advantages and its problems. To
further confuse matters OOP objects can often be used as OBP ones
though the reverse process always contains defects. To put it
another way, your compiler can often determine that a specific
access to a polymorphic object is unambiguously defined at compile
time and so can use more efficient static access methods.</p>
<p>Look at 'Jack'. He is an instance of the class man. What sort of
class is 'man'? Surely it isn't an isolated class? Is there a class
'woman'? Surely there must be, at least implicitly. And in a modern
non-sexist you better not use 'man' as a base class for 'woman'
however much biblical support you quote for such a view. Clearly
there must be a parent (base) class 'person' which is used to
derive two equal (but different) status classes 'man' and 'woman'
(and if you don't think they are different you have a far more
serious problem than distinguishing between OOP and OBP) Now if you
design your classes so that you must always either know someone's
gender at compile time or the action is gender neutral and generic
'person' behaviour will suffice you will be writing in an OBP
style. If you design your classes so that the specific response to
certain actions will be determined in at run time in the context of
the gender of a person then you are likely to be considering your
design from an OOP view.</p>
<p>Let me give you a small example of part of a class interface for
the three classes 'person', 'man' and 'woman'.</p>
<pre class="programlisting">
class Person
{
  // data
  PersonList * children; // null pointer
                         // terminated list
  Person * mother;
  Person * father;
  // protected interface
public:  bool has_children()
  { return children ? true : false; }
  void is_parent()
  {
    if (has_children)
    {
      cout &lt;&lt; &quot;I am a parent&quot;;
    }
    else
    {
      cout &lt;&lt; &quot;I am not a parent&quot;;
    }
  }
  // other member functions
};

class Man: public Person
{
  // extended private interface
  // extensions to protected interface
public: void is_parent()
  {
    if (has_children)
    {
      cout &lt;&lt; &quot;I am a father&quot;;
    }
    else
    {
      cout &lt;&lt; &quot;I am not a parent&quot;;
    }
  }
  // other member functions
};

class Woman: public Person
{
  // extended private interface
  // extensions to protected interface
public: void is_parent()
  {
    if (has_children)
    {
      cout &lt;&lt; &quot;I am a mother&quot;;
    }
    else
    {
      cout &lt;&lt; &quot;I am not a parent&quot;;
    }
  }
  // other member functions
};
</pre>
<p>Please note that has_children() is a kind of access function and
its existence allows the new versions of is_parent() to work
without the Person data (which is a pure implementation detail
being made directly available - provide protected access functions
where needed but do not provide direct access to data in a base
class, otherwise you can never have second thoughts. Keep a clear
view, the private interface encapsulates those things that are
strictly to do with implementing a class, the protected interface
is for implementation details that concern a class and its derived
versions and a public interface is to enable others to use your
class.)</p>
<p>The above hierarchy is an OBP one. For example consider the
following:</p>
<pre class="programlisting">
int main(){
  Man jack;
  Woman jill;
  Person * someone = &amp;jack;
  jack.is_parent();
  jill.is_parent();
  someone-&gt;is_parent();
  someone= &amp;jill;
  someone-&gt;is_parent();
}
</pre>
<p>I know that I have left out scads of code. Without any
constructors specified the exact output of this program is
impossible to determine but you should be able to see that the
compiler will not determine the true gender of someone (in this
example it could, but we might select someone to point to jack or
jill in response to some external run time event and then the
compiler simply could not know). Make sure you understand this
because unless you do you do not understand static versus dynamic
binding.</p>
<p>Now if we want to upgrade this program to OO rather than OB we
must declare the member function is_parent() virtual in the Person
class. The effect of that small extra is to move the Person class
into being polymorphic. The compiler can no longer determine what
you wish to do at compile time (actually, a really good optimising
compiler will recognise that it can in the code as written) and so
must refer to a hidden pointer in the jack/jill objects that points
to a lookup table of polymorphic (virtual) functions to find the
address of the (in this case) first function for the appropriate
derived class.</p>
<p>Note (and Microsoft might note this as well when they next
revise MFC) that a polymorphic class should have a virtual
destructor to ensure that resources are correctly de-allocated if a
derived dynamic instance is being handled through a base class
pointer.</p>
<p>Now let us get back to Graham's problem. A property of jack is
that he can be in something. The problem is that we forgot to allow
for this when we designed the original. If jack is simply an
instance of a simple class Person we can quickly fix the problem by
deriving a new version of Person that includes a pointer to a class
Container of which box is an instance. But this is where the OBP
view begins to creak. The pointer will be to a specific type, and
unless it is a polymorphic type we will only be able to put jack in
one kind of container.</p>
<p>If we change our viewpoint to the 'box' we will realise that it
is a property of that kind of object that it can contain things
(one or more). Again, in an OBP environment we have to decide
precisely what sort of object box may contain.</p>
<p>As you see, we can fix the problem on a piecemeal basis, and we
can even do so without touching the original code. That much
inheritance will provide even in a purely OBP environment. It isn't
really enough.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e122" id="d0e122"></a>Two new
classes</h2>
</div>
<p>Consider the following:</p>
<pre class="programlisting">
class Contents {
  Container * wrap;
// the rest of the class, polymorphic for
// preference
};
class Container {
  List_of_Contents * contents;
// yes, you now need some mechanism to hold a
// list of Contents
// rest of class
};
</pre>
<p>Note that you cannot avoid pre-declaring some of the classes
needed for this scheme because you have a chicken and egg type
problem. Also note that these are really stub type classes
encapsulating a single property that may be shared by many
different types of object.</p>
<p>A box has the property that it can both contain containable
objects and be contained by container type objects so how should we
define it. Let me take a first cut at the problem.</p>
<pre class="programlisting">
class Box {
  Contents c;
  Container w;
// other details
};
</pre>
<p>The problem with this aggregation approach is that it fails
because this does not turn a Box into an object of type Container.
To see the problem, let us revisit Person.</p>
<pre class="programlisting">
class Person_with_Container: public Person {
  Container * cp;
// rest of class
} jack;
</pre>
<p>But cp must point to a Container type of object and Box is not
such a type. We must change Box so that it is a derived type of
Container:</p>
<pre class="programlisting">
class Box: public Container {
// other details
};
</pre>
<p>Now jack.cp can point to a Box type object and as long as we
have made the Container type polymorphic the actual container (a
Box in this case) will behave correctly.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e145" id="d0e145"></a>Trouble</h2>
</div>
<p>The problem with this approach is that we have taken a very
one-sided view of a Box, that of being a container. What happens
when we want to put it in something else? Well to achieve that we
must make sure that it is a Content type object so that it can
belong to a List_of_Contents type object. That means it must be
derived from the Contents class. So now we must write:</p>
<pre class="programlisting">
class Box: public Container, public Contents {
  // class details
};
</pre></div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e152" id="d0e152"></a>Multiple
inheritance</h2>
</div>
<p>It is for this kind of programming style that multiple
inheritance was designed. I know that you can manage without but
once you start encapsulating properties to provide building blocks
for less abstract objects, multiple inheritance becomes
increasingly attractive. In a polymorphic environment aggregation
(or layering as it is sometimes called) is not enough. If an object
needs to be viewed as many different types of thing simultaneously
then it must be derived from many different types of things.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e157" id="d0e157"></a>Fix-ups</h2>
</div>
<p>One problem of OOP is that you do need to get the design right
to start with. Look back at our Person hierarchy. To insert the
property that a person can be a content we have to provide the
modification at that level, it is too late to do so at the 'Man' or
'Woman' stage. There is no mechanism for splicing properties into
an inheritance graph, for that you really do need the original
source code and a recompilation of the class library. It is no good
coming along to Francis with bright ideas as how to extend C++ to
incorporate such a feature, it is too late. Having said that, there
are, of course, programming mechanisms developed by experts which
allow modification to base classes but not quite to the extent of
adding in another polymorphic type. If this is the sort of thing
that challenges you, go back and read Francis' article on the
Cheshire Cat. Now imagine that you have a pointer to a Suitcase
object that has the property of being available for packing with a
thousand and one objects -- back to our container and contents.</p>
<p>I think this is far enough -- it is already further than I meant
to go.</p>
</div>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
