    <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  :: Questions and Answers</title>
        <link>https://members.accu.org/index.php/articles/1093</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 + CVu Journal Vol 13, #1 - Feb 2001</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/c77/">CVu</a>

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

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

                    -                        <a href="https://members.accu.org/index.php/articles/c65+122/">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;Questions and Answers</h1>
<p><strong>Author:</strong>&nbsp;</p>
<p>
<strong>Date:</strong> 04 February 2001 13:15:43 +00:00 or Sun, 04 February 2001 13:15:43 +00: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>
</div>
<p>Unless I have lost something, we are very short of answers in
this issue. I have added a few more questions just to keep the pot
simmering. Of course some of you just read C Vu for answers, but I
know many of you read it to stimulate your grey matter. A lifetime
of experience has taught me that I only understand something when I
can write it up for someone else. I have lost count of the number
of times when I thought I understood something only to hit a fatal
flaw when forced to write about it.</p>
<p>Even when I have been correct in my understanding, it is the
process of distilling it to simple words that allows me to take
possession of the idea. Please do not short change yourselves by
only reading this column. One reason that I do not give the answers
myself is that that would destroy at least half of what this is
about.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e24" id="d0e24"></a>Answers</h2>
</div>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e27" id="d0e27"></a>Answer to Q1 from
C Vu 12.6</h3>
</div>
<div class="sect3" lang="en">
<div class="titlepage">
<h3><a name="d0e30" id="d0e30"></a>from: James
Holland <tt class="email">&lt;<a href=
"mailto:jamie_holland@lineone.net">jamie_holland@lineone.net</a>&gt;</tt></h4>
</div>
<p>The problem occurs when assigning a floating-point number to an
integer. The C standard states that the fractional part is
truncated. A floating-point value, say 11.99, being assigned to an
integer variable would result in the variable containing 11.
Presumably, what is required is for the integer variable to be set
to the nearest integer representing the floating-point valve. I
suggest the following expression.</p>
<pre class="programlisting">
int k;
float p;
...
k = floor(p + 0.5);
</pre>
<p>where <tt class="function">floor()</tt> is declared in
<tt class="filename">math.h</tt>.</p>
<p>This method works with negative numbers as well as positive
ones. Incidentally, Borland 5.5.1 gives <tt class="literal">k =
12</tt> as the result of the program in the original question.</p>
</div>
</div>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e52" id="d0e52"></a>Answer to Q3 from
C Vu 12.6</h3>
</div>
<div class="sect3" lang="en">
<div class="titlepage">
<h3><a name="d0e55" id="d0e55"></a>from The
Harpist</h4>
</div>
<p>The problem was about the use of a namespace directive when none
of the included files declared that namespace (incidentally, do you
declare or define a namespace?). The specific problem related to
<tt class="literal">namespace std</tt> but is not confined to that.
Before you can use a namespace directive, that namespace must be
visible to the compiler. Some standard header files (such as
<tt class="filename">cassert</tt> and <tt class=
"filename">climits</tt>) do not introduce names to <tt class=
"literal">std::</tt> and so do not use it. So the question comes
down to whether there is a way of declaring a namespace name in a
way that is analogous to declaring a class name.</p>
<p>The solution once you see it is very simple. Namespaces can be
re-opened (unlike classes) so all you need to do is:</p>
<pre class="programlisting">
namespace std {}
</pre>
<p>Now you can safely write:</p>
<pre class="programlisting">
using namespace std;
</pre>
<p>as the next line of your source code.</p>
</div>
</div>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e82" id="d0e82"></a>Answer to Q4 from
C Vu 12.6</h3>
</div>
<div class="sect3" lang="en">
<div class="titlepage">
<h3><a name="d0e85" id="d0e85"></a>from Jim Hyslop
<tt class="email">&lt;<a href=
"mailto:Jim.Hyslop@Leitch.com">Jim.Hyslop@Leitch.com</a>&gt;</tt></h4>
</div>
<p>Silas Brown asked, given the expression:</p>
<pre class="programlisting">
f(g(a()), g(b()), g(c()))
</pre>
<div class="blockquote">
<blockquote class="blockquote">
<p>&quot;... is each call of <tt class="function">a</tt>, <tt class=
"function">b</tt> and <tt class="function">c</tt> guaranteed to be
followed by a call to <tt class="function">g</tt>, or is the
compiler free to call <tt class="function">a</tt>, <tt class=
"function">b</tt> and <tt class="function">c</tt> first and then
<tt class="function">g</tt> three times...&quot;</p>
</blockquote>
</div>
<p>It is the latter, I'm afraid. Each of <tt class=
"function">a()</tt>, <tt class="function">b()</tt>, <tt class=
"function">c()</tt> and each call to <tt class="function">g</tt> is
considered a separate sub expression. The compiler can evaluate
each of these sub-expressions in any order (except, of course, that
it cannot call <tt class="function">g</tt> before it has called the
corresponding one of <tt class="function">a</tt>, <tt class=
"function">b</tt> or <tt class="function">c</tt>).</p>
<p>If you need the arguments evaluated in a particular order, then
it is up to you to re-write the code to force that order, as
in:</p>
<p>If you want the order <tt class="function">a</tt>, <tt class=
"function">g</tt>, <tt class="function">b</tt>, <tt class=
"function">g</tt>, <tt class="function">c</tt>, <tt class=
"function">g</tt></p>
<pre class="programlisting">
arg1 = g(a());
arg2 = g(b());
arg3 = g(c());
f(arg1, arg2, arg3);
</pre>
<p>or, if you do not care which order the '<tt class=
"function">g</tt>'s are called, but you do want <tt class=
"function">a</tt>, then <tt class="function">b</tt>, then
<tt class="function">c</tt>:</p>
<pre class="programlisting">
argx = a();
argy = b();
argz = c();
f( g(argx), g(argy), g(argz));
</pre>
<p><i><span class="remark">Thanks Jim, welcome to these
pages</span></i>.</p>
</div>
</div>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e190" id=
"d0e190"></a>Questions</h2>
</div>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e193" id="d0e193"></a>Q1. (from Silas
Brown) <a href="http://www.flatline.org.uk/~silas" target=
"_top">www.flatline.org.uk/~silas</a></h3>
</div>
<div class="sect3" lang="en">
<div class="titlepage">
<h3><a name="d0e198" id="d0e198"></a>A common
overloading problem</h4>
</div>
<p>Should a base class's methods still be visible when they are
overloaded by a subclass? My compiler seems to think not.</p>
<pre class="programlisting">
class Base {
public:
  void method() {};
};
class Sub : public Base {
public:
  void method(int p) {
    Base::method(); // Compiles OK
    method(); // Error
  }
};
</pre>
<p class="c2"><span class="remark">Variations on this question are
among the most common questions about C++. What we are looking for
is a clear explanation of why the language rules are as they are.
And in case you too are in doubt, the compiler is right.
FG</span></p>
</div>
</div>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e208" id="d0e208"></a>Q2. (from
comp.lang.c++.moderated)</h3>
</div>
<div class="sect3" lang="en">
<div class="titlepage">
<h3><a name="d0e211" id="d0e211"></a>Missing Header
File?</h4>
</div>
<p>I'm using CodeWarrior. I cannot find the <tt class=
"filename">bool.h</tt> file, so I can't run any program that uses a
boolean. It must be an installation problem. Can anyone send me a
copy of the <tt class="filename">bool.h</tt> file?</p>
<p class="c2"><span class="remark">Note the source of the question.
Now to answer you will need to know something about the latest C
Standard. FG</span></p>
</div>
</div>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e225" id="d0e225"></a>Q3. (from a
student)</h3>
</div>
<div class="sect3" lang="en">
<div class="titlepage">
<h3><a name="d0e228" id="d0e228"></a>Conversion
functions</h4>
</div>
<p>How does this code work? In other words why does the output
print out a value when operator&lt;&lt; does not exist for Point
objects</p>
<pre class="programlisting">
#include &lt;iostream&gt;
using namespace std;
class Point {
protected:
  double crd[3];
public:
  Point(double,double,double);
  operator double* ();
  operator double const* () const;
};
Point::Point(double x, double y, double z) {
  crd[0]=x; crd[1]=y; crd[2]=z;
}
Point::operator double* () {return crd;}
Point::operator double const* () const {
  return crd;
}
int main() {
  Point p(1,2,3);
  cout&lt;&lt; &quot;y-component of p is &quot; &lt;&lt;p[1] &lt;&lt; endl;
  return 0;
}
</pre>
<p class="c2"><span class="remark">Note that this questions was
motivated by trying to understand some published code. You might
feel the need to comment on the technique as well as why it works.
FG</span></p>
</div>
</div>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e238" id="d0e238"></a>Q4. Contributed
by Alan Lenton <tt class="email">&lt;<a href=
"mailto:alan@ibgames.net">alan@ibgames.net</a>&gt;</tt></h3>
</div>
<div class="sect3" lang="en">
<div class="titlepage">
<h3><a name="d0e243" id="d0e243"></a>Formatting
intgers with commas</h4>
</div>
<p>Hi Mr. Lenton (if you would like me to use some other name, let
me know, and I will).</p>
<p>I'm trying to do my programming homework, and I'm not good at
number theory, but I've been trying. This is the assignment. We
take a <span class="type">long int</span> (up to 10 digits) and
have the number displayed with commas. For instance, 123456789
should be displayed as 123,456,789.</p>
<p>I got it to work with a number up to 9 digits, but then my code
goes haywire when it gets to 10 digits. If you're not too busy, I'd
appreciate any help you can give me. If you have suggestions as to
make the code cleaner, I'd appreciate them also. Like I said, I
tried my best with this, so it might look haphazard. Also, this is
in a function - I'm not including main.</p>
<p>Here goes:</p>
<pre class="programlisting">
#include &lt;iostream.h&gt;
void displaywithcommas(long int num){
  int set, set2, set3;
  if (num &lt; 1000)  
// no commas if number is less than 1000
    cout &lt;&lt; num &lt;&lt; endl;
  else if (num &gt;= 1000 &amp;&amp; num &lt; 1000000){
    set = num%1000;
    num-=set;
    num/=1000;
    cout &lt;&lt; num &lt;&lt;&quot;,&quot;&lt;&lt; set &lt;&lt; endl;
  }
  else if (num &gt;= 1000000 &amp;&amp; 
                  num &lt; 1000000000){
    set = num%1000;
    set2 = ((num%1000000)-set)/1000;
    num-=set;
    num/=1000000;
    cout&lt;&lt;num&lt;&lt; &quot;,&quot; &lt;&lt;set2&lt;&lt; &quot;,&quot; &lt;&lt;set&lt;&lt;endl;
  }
  else if (num &gt;=1000000000){
    set  = num%1000;
    set2 = ((num%1000000)-set)/1000;
    set3 = ((num%1000000000)-set2)/1000;
    num-=set;
    num/=1000000000;
    cout &lt;&lt; num &lt;&lt; &quot;,&quot; &lt;&lt; set3 &lt;&lt; &quot;,&quot; &lt;&lt; set2
         &lt;&lt; &quot;,&quot; &lt;&lt; set &lt;&lt; endl;
  }
}
</pre>
<p>Once again, I'd appreciate any help you can give me.</p>
<p class="c2"><span class="remark">Well Alan answered the original
enquiry (like authors, game designers get asked for help), but what
would you have suggested? FG</span></p>
</div>
</div>
</div>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
