Friday, October 2, 2009

C++, I learned it

Practically overnight, too.  Of course, that thanks to my knowledge
of Java and PHP, as well as href="http://cplus.about.com/od/learning1/Learn_about_C_and_how_to_write_Programs_in_It.htm">tutorials by
About.com.  Basically, C++ is like this:

style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom:
4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%;
padding-right: 4px; font-family: 'Courier New', courier, monospace; direction: ltr;
max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right:
silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
 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;



style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt;
border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%;
padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr;
border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible;
padding-top: 0px"> 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:



style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom:
4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%;
padding-right: 4px; font-family: 'Courier New', courier, monospace; direction: ltr;
max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right:
silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt;
border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px;
font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none;
color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">

 1: class Polygon



style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt;
border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%;
padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr;
border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible;
padding-top: 0px"> 2: {



 3: protected:



style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt;
border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%;
padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr;
border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible;
padding-top: 0px"> 4: int sideAmount;


style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt;
border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%;
padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr;
border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible;
padding-top: 0px"> 5: double sides[MAX_POLYGON_SIDES];



 6: public:



style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt;
border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%;
padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr;
border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible;
padding-top: 0px"> 7: bool regular;


style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt;
border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%;
padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr;
border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible;
padding-top: 0px"> 8: Polygon( style="color: #0000ff">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);



style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt;
border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%;
padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr;
border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible;
padding-top: 0px"> 29: return getSides();



 30: }




 31: int addSides(int addAmount)




 32: {




 33: sideAmount +=
addAmount;



 34:
return getSides();




 35: }




 36: void setSide(int side,            style="color: #0000ff">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++)



style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt;
border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%;
padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr;
border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible;
padding-top: 0px"> 51: {



 52: total += getSide(i);




 53: }




 54: return total;



style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt;
border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%;
padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr;
border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible;
padding-top: 0px"> 55: }



 56: };






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



style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom:
4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%;
padding-right: 4px; font-family: 'Courier New', courier, monospace; direction: ltr;
max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right:
silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt;
border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px;
font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none;
color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">

 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;



style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt;
border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%;
padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr;
border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible;
padding-top: 0px"> 14: }




(Will,
of course, return 3).

2 comments:

  1. I have the implementation of polygon area somewhere

    ReplyDelete
  2. 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.

    ReplyDelete