title: C/C++: Perimeter and area of rects. Volume of cuboids
body: <p>I want to calculate area and perimeter of rects using the following code:</p>
  
  <pre><code>    rect a;
      a = ( -----
            !   !
            -----a );
  std::cout &lt;&lt; a.area() &lt;&lt; std::endl;
  std::cout &lt;&lt; a.perimeter() &lt;&lt; std::endl;
  </code></pre>
  
  <p>For this purpose I crafted the following class:</p>
  
  <pre><code>class rect
  {
  public:
      rect():w(0), h(2) {}
      rect&amp; operator - () { w += 0.5f; return *this; }
      rect&amp; operator - (rect&amp;) { w += 0.5f; return *this; }
      rect&amp; operator -- (int a) { w += a; return *this; }
      rect&amp; operator -- () { w += 1; return *this; }
      rect&amp; operator ! () { h += 0.5f; return *this; }
      void clear() { w = 0; h = 2; }
      int area() { return w * h; }
      int perimeter() { return 2 * w + 2 * h; }
      int width() { return w; }
      int height() { return h; }
  private:
      float w;
      float h;
  };
  </code></pre>
  
  <p>Here are some usage examples:</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  int main()
  {
      rect a;
  
      a = ( -----
            !   !
            -----a );
  
      std::cout &lt;&lt; a.area() &lt;&lt; std::endl;
      std::cout &lt;&lt; a.perimeter() &lt;&lt; std::endl;
      std::cout &lt;&lt; a.width()  &lt;&lt; std::endl;
      std::cout &lt;&lt; a.height() &lt;&lt; std::endl;
  
      std::cout &lt;&lt; std::endl;
  
      a.clear();
  
      a = ( ----------
            !        !
            !        !
            !        !
            !        !
            ---------a );
  
      std::cout &lt;&lt; a.area() &lt;&lt; std::endl;
      std::cout &lt;&lt; a.perimeter() &lt;&lt; std::endl;
      std::cout &lt;&lt; a.width()  &lt;&lt; std::endl;
      std::cout &lt;&lt; a.height() &lt;&lt; std::endl;
  
      return 0;
  }
  </code></pre>
  
  <p><strong>Here are my questions:</strong></p>
  
  <ol>
  <li><strong>Can it be done without involving any floating point arithmetic?</strong>
  (indeed, it is an integer grid)</li>
  <li><p><strong>Can it be generalized on a 3D case?</strong> I.e:</p>
  
  <pre><code>cuboid b;
  b = (  ---------
        /        /!
       ! -------! !
       !        ! !
       !        ! !
       !        !/
       ---------b );
  
  std::cout &lt;&lt; b.volume() &lt;&lt; std::endl;
  </code></pre></li>
  </ol>