Navarr's Tech Side The Technical Side of my Life

2Oct/092

C++, I learned it

Practically overnight, too.  Of course, that thanks to my knowledge of Java and PHP, as well as tutorials by About.com.  Basically, C++ is like this:

   1: #include <stdio.h>

   2: #include <iostream>

   3: int main(void)

   4: {

   5:     // Program statements here.

   6:     std::cout << "Hello World" << std::endl;

   7:     return 0;

   8: }

It all starts with a single function, but you can have such things as classes, overload, etc.  Variables have to be created specifically (int, double, string, float, etc.) before being used, like Java (unlike PHP) and you have classes that are similar to both Java and PHP, for example:

   1: class Polygon

   2: {

   3: protected:

   4:     int sideAmount;

   5:     double sides[MAX_POLYGON_SIDES];

   6: public:

   7:     bool regular;

   8:     Polygon(void)

   9:     {

  10:         regular = false;

  11:     }

  12:     void setSides(int newSides)

  13:     {

  14:         sideAmount = newSides;

  15:     }

  16:     int getSides(void)

  17:     {

  18:         return sideAmount;

  19:     }

  20:     int addSide(void)

  21:     {

  22:         sideAmount += 1;

  23:         return getSides();

  24:     }

  25:     int addSide(int sideValue)

  26:     {

  27:         sideAmount += 1;

  28:         setSide(getSides(),sideValue);

  29:         return getSides();

  30:     }

  31:     int addSides(int addAmount)

  32:     {

  33:         sideAmount += addAmount;

  34:         return getSides();

  35:     }

  36:     void setSide(int side,double value)

  37:     {

  38:         side -= 1;

  39:         sides[side] = value;

  40:     }

  41:     double getSide(int side)

  42:     {

  43:         side -= 1;

  44:         return sides[side];

  45:     }

  46:     double getPerimeter()

  47:     {

  48:         if(getSides() < 3) { throw 1; }

  49:         double total = 0;

  50:         for(int i = 1;i <= getSides();i++)

  51:         {

  52:             total += getSide(i);

  53:         }

  54:         return total;

  55:     }

  56: };

See?  Does it look that hard?  I didn’t think so.  And, to help those who haven’t used Java:

   1: #include <stdio.h>

   2: #include <iostream>

   3: #include "Polygon.h"

   4:  

   5: int main(void)

   6: {

   7:     Polygon poly;

   8:     poly.setSides(3);

   9:     poly.setSide(1,1);

  10:     poly.setSide(2,1);

  11:     poly.setSide(3,1);

  12:     std::cout << "Perimeter: " << poly.getPerimeter() << std::endl;

  13:     return 0;

  14: }

(Will, of course, return 3).

  • SM

    I have the implementation of polygon area somewhere

  • http://tech.gtaero.net/ Navarr Barnier

    I didn't want to code a function for area because i left it open for irregular polygons. I'm not mathematically inclined to know if the area for a regular polygon and an irregular polygone is the same if all the sides are the same length.