    <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  :: Class Struggle</title>
        <link>https://members.accu.org/index.php/articles/1336</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 #1 - Apr 1993</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/c220/">01</a>
<br />

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

                    -                        <a href="https://members.accu.org/index.php/articles/c65+220/">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;Class Struggle</h1>
<p><strong>Author:</strong>&nbsp;</p>
<p>
<strong>Date:</strong> 30 April 1993 11:55:00 +01:00 or Fri, 30 April 1993 11:55:00 +01:00</p>
<p><strong>Summary:</strong>&nbsp;A basic introduction to the uses of C++</p>
<p><strong>Body:</strong>&nbsp;<p>This is a basic introduction to C++ programming. The code example we
are going to use is a counter value that appears at a fixed location on
the screen, for example the score on a game, a count of the
numbers of records processed etc. I know its a trivial example, but it
serves well to illustrate the difference in paradigms between C and C++.</p>
<h2>The scenario in C.</h2>
<p>There are several ways this can be achieved. The first option is to
have a general function e.g.</p>
<pre class="programlisting">  printat(xpos, ypos, value);</pre>
<p>This has the disadvantage of having to remember the X and Y
coordinates of where the score is to be printed each time. This can be
cured by the second option which is to have the positions of the
counter hard wired into the program.</p>
<pre class="programlisting">  print_counter(value);</pre>
<p>This however, has the obvious disadvantage that when two or more
counters are needed, each will have to have its own function, each
differing only with the hard wired xpos and ypos values. This can be
overcome by setting up a struct containing the xpos, ypos and value and
passing the struct to the function. This means that the xpos and ypos
values would only need setting once. The whole of the struct can then
be passed to the function. This is a better method as we can now have a
general function that has all its information tied into one block.</p>
<pre class="programlisting">  typedef struct <br>  {<br>    int xpos;<br>    int ypos;<br>    int value; <br>  } count;<br>  count myscore, computer_score; <br>  myscore.xpos = 10; <br>  myscore.ypos = 10; <br>  myscore.value = 30; <br>  computer_score.xpos = 50; <br>  computer_score.ypos = 10; <br>  computer_score.value = 33; <br>  printat(&amp;myscore); <br>  printat(&amp;compute r_score); <br>  getch();<br>  while (myscore.value++ &lt; 1000) <br>  {<br>    compute r_score.value++; <br>    printat(&amp;myscore); <br>    printat(&amp;computer_score); <br>  }</pre>
<p>This has solved the problem of accidentally putting the wrong
information at the wrong coordinates etc.</p>
<h2>The scenario in C++</h2>
<p>In C++, a class would be set up that would contain all the
coordinates and the current value, and are manipulated as a whole.
Operators can be defined to manipulate the class as if it was a
variable in its own right.</p>
<p>Some of this code may not be obvious as to its function, but
examine it carefully and then read the annotated code below it.</p>
<p>The header file (counter.h):</p>
<pre class="programlisting">  class Counter<br>  {<br>  protected:<br>    int X_Pos;<br>    int Y_Pos;<br>    int CounterValue;<br>    void Display(void); <br>  public:<br>    Counter (int X, int Y, int val = 0);<br>    int operator += (int Val);<br>    int operator ++ (void);<br>    int operator = (int);<br>    friend int&amp; operator = (Counters);<br>  };</pre>
<p>The C++ file (counter.cpp):</p>
<p>(Note: those with versions prior to 3.0 will need to use gotoxy and
cout in conio.h &amp; iostream.h respectively)<br>
</p>
<pre class="programlisting">  #include &lt;constream.h&gt;<br>  #include &lt;iomainip.h&gt;<br>  #include &quot;counter.h&quot;<br><br>  void Counter::Display () <br>  {<br>    constream console;<br>    console &lt;&lt; setxy(X_Pos, Y_Pos)<br>            &lt;&lt; setw(5) &lt;&lt; CounterValue; <br>  }<br><br>  Counter::Counter (int X, int Y, int val) <br>  {<br>    X_Pos = X;<br>    Y_Pos = Y;<br>    CounterValue = val;<br>    Display (); <br>  }<br><br>  int Counter::operator += (int Val) <br>  {<br>    CounterValue += Val;<br>    Display() ;<br>    return CounterValue; <br>  }<br><br>  int Counter::operator ++ (void) <br>  {<br>    CounterValue++;<br>    Display();<br>    return CounterValue; <br>  }<br><br>  int Counter::operator = (int Val) <br>  {<br>    CounterValue = Val;<br>    Display();<br>    return CounterValue; <br>  }<br><br>  int&amp; operator = (Counter&amp; K) {<br>    return K.CounterValue;<br>  }</pre>
<p>The program testcoun.cpp</p>
<pre class="programlisting">  #include &quot;counter.h&quot; <br><br>  void main(void) {<br>    Counter myscore(10,10),<br>    computer_score(30,10,3); <br>    int&nbsp; i;<br>    while ((i=myscore)&lt;1000) {<br>      myscore++; <br>      computer_score++; <br>    }<br>  }</pre>
<p>Though the definition of a counter class is slightly more
long-winded than setting up a struct, I think you will agree that its
usage in the main function is much more natural to the C programmer,
and it is a lot easier to use in larger programs.</p>
<p>Let's take a look at it in more detail and explain each of the
lines. I apologise if this proves to be a little tedious, but it is
intended for those without any C programming as well as C programmers.</p>
<p>The class structure of a C++ program is not that dissimilar to
a struct in C, but has the addition has the keywords protected, private
and public to limit the access to the member items of the class. A
class, simply put, is a collection of data items and functions to
manipulate those data items.</p>
<p>The general format of a simple class is as follows:</p>
<pre class="programlisting">  class &lt;class_name&gt;<br>  {<br> &nbsp;private:   <br>    &lt;private data &amp; functions) <br>  protected: <br>    &lt;protected data &amp; functions) <br>  public:<br>   &nbsp;&lt;public data &amp; Junctions)<br>  };</pre>
<p>The best way to start designing a class is firstly to decide on
the
data to be held and then to decide on the member functions that are
needed, both internal to the class and the member functions that are
visible to functions outside of the class.</p>
<p>We now need to think about several things. Firstly what information
do
we need to hold? This has already been decided, we need to hold the X
and Y positions and the current value that is on the screen. We then
need to decide on the level of protection for these items. As none of
these items are to be manipulated by anything outside of the class,
they can all become either private or protected. Which one selected
depends upon if we intend to use this class for inheritance
or not.
For now we will ignore this possibility and set them to protected.</p>
<p>At this stage our class would look as follows:</p>
<pre class="programlisting">  class Counter&nbsp; // name of the class<br>  {<br>  protected:&nbsp;&nbsp;&nbsp;&nbsp; // the following items are protected <br>    int X_Pos;&nbsp; // the xposition of the counter <br>    int Y_Pos;&nbsp; // the yposition of the counter <br>    int CounterValue; // the value of the counter<br>  };</pre>
<p>Secondly we need to specify the public interface (i.e. methods) of
the class. This applies to operators, functions and data.</p>
<pre class="programlisting">  class Counter<br>  {<br>  protected:<br>    int X_Pos;<br>    int Y_Pos;<br>    int CounterValue; <br>  public:&nbsp; // The following items are public<br>    // Same name therefore a constructor<br>    Counter (int X, int Y, int val = 0);<br>    int operator += (int Val); // &lt;Counter&gt; += &lt;int&gt;<br>    int operator ++ (void);&nbsp;&nbsp; // &lt;Counter&gt;++<br>    int operator = (int);&nbsp;&nbsp;&nbsp; // &lt;Counter&gt; = &lt;int&gt;<br>    // for &lt;int&gt; = &lt;Counter&gt;<br>    friend int&amp; operator = (Counter&amp;);<br>  };</pre>
<p>Thirdly, we need to decide on the functions and data that are needed
by the class for its internal usage.</p>
<pre class="programlisting">  class Counter<br>  {<br>  protected:<br>    int X_Pos;<br>    int Y_Pos;<br>    int CounterValue;<br>    void Display(void);&nbsp; // to display the counter public:<br>    Counter (int X, int Y, int val =0);<br>    int operator += (int Val);<br>    int operator ++ (void);<br>    int operator = (int&amp;);<br>    friend int operator = (Counter&amp;);<br>  };</pre>
<p>All the member functions of the Counter class have been written
'out-of-line', the other alternative is that they could have been
written 'in-line' and included in the class definition.</p>
<p>If the display function was to be written 'in-line' it would be
within the class definition and look like this:</p>
<pre class="programlisting">  void Display (void) <br>  { <br>    constream console;<br>    console &lt;&lt; setxy(X_Pos, Y_Pos) &lt;&lt; CounterValue;<br>  }</pre>
<p>Written 'out-of-line' the display function will look as follows:</p>
<pre class="programlisting">  void Counter::Display()<br>  {<br>    constream console;<br>    console &lt;&lt; setxy(X_Pos, Y_Pos)<br>            &lt;&lt; setw(5) &lt;&lt; CounterValue;<br>  }</pre>
<p>The structure of the 'out-of-line' member function is as follows:</p>
<pre class="programlisting">  &lt;return-type&gt;&nbsp; &lt;Class-name&gt;::&lt;member function name&gt;<br>                               ( &lt;params&gt;..) <br>  {<br>    &lt;function statements);<br>  }</pre>
<p>Lets look at some of the other functions. Any member function that
has the same name as the class is known as a constructor. If the name
is preceded by a tilde (~) and is the same as the class name it is
known as a destructor.</p>
<p>The constructor and destructor member functions are never called by
the programmer, they are called by the compiler at the creation and
destruction of the object (i.e. when it comes into scope and it leaves
scope).</p>
<p>The Counter member function takes 2 or 3 parameters, yes C
programmers (2||3 parameters). The third parameter, if not supplied is
defaulted to the number specified in the class declaration.</p>
<pre class="programlisting">  Counter::Counter (int X, int Y, int val) <br>  {<br>    // copy the parameters into the class<br>    X_Pos = X;<br>    Y_Pos = Y;<br>    CounterValue = val;<br>    // and display it on the screen at the desired<br>    // position<br>    Display ();<br>  }</pre>
<p>With C++ classes, it is not only functions that can be used,
operators
can also be custom built to suit the class. The general structure of
the operator is as follows:</p>
<pre class="programlisting">  &lt;return-type&gt;&nbsp; &lt;Class-name&gt;::operator &lt;operator&gt;&nbsp;&nbsp; <br>                              (&lt;params&gt;..)<br>  {<br>    &lt;function statements);<br>  }<br></pre>
<p>The first needed operator is the equal sign<br>
</p>
<pre class="programlisting">  int Counter::operator = (int Val) <br>  {<br>    CounterValue = Val;<br>    Display();<br>    return CounterValue; <br>  }</pre>
<p>This operator is used to assign an integer value to acounter. The +=
and ++ operators follow a very similar pattern.</p>
<p>This allows the programmer to use += to increase the value on the
screen, e.g.</p>
<pre class="programlisting">  Counter score(20,24,0);
  if (space_invader-&gt;hit())
    score+=space_invader-&gt;value();

  int Counter::operator ++ (void) {
    CounterValue++;
    Display() ;
    return CounterValue;
  }</pre>
<p>The ++ operator allows a screen counter to be incremented easily, e.g.</p>
<pre class="programlisting">
  Counter num_recs(10,10,0);
  if (get_record()==0K)
  {
    process_record();
    num_recs++;
  }
</pre>
<p>The situation can also occur when you want to assign the value of
the
class to an integer. This is a situation in which the function produced
cannot be a member function as it is not an action on the object of the
type Counter, but an action on type int. For this to happen the
function must be declared within the class as a friend of the class.
This has the effect of allowing the function access to the data members
of Counter.</p>
<p>This function looks a little strange to C programmers as it uses
references to values, not pointers to values. A reference is like an
automatically de-referenced pointer.</p>
<pre class="programlisting">  int&amp; operator = (Counter&amp; K)
  {
    return K.CounterValue;
  }</pre>
<p>The main program is very simple to follow and requires no
annotation:</p>
<pre class="programlisting">  void main (void)
  {
    Counter myscore(10,10), // declaration of score
    computer_score(30,10,3); // positions &amp; values while
    ((myscore++)&lt;1000) {
      compute r_score++;
    }
}</pre>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
