This is the first example in a serie of cheap (absolutely free and cheap) articles to get some basic and practical C++ knowledge. It will references the great explanations provided at www.cplusplus.com C++ tutorial. Using Arduino Duemilanove (well, really it’s a Funduino).
Here you can download The file with the code, and you can find links to a lot of free tutorials, courses and books to learn C++ here.
// C++ crash tutorial with Arduino Duemilanove. // First example: http://softwaresouls.com/softwaresouls/2013/06/23/c-crash-tutorial-using-robotis-cm-900-board-and-ide-i/ /* Hello World shows messages on construction and destruction Also it lets you to salute. Showing always its assigned identifiction number MyId */ class HelloWorld { private: int myId; //Object identification num ber public: // class constructor http://www.cplusplus.com/doc/tutorial/classes/ HelloWorld(char *message, byte id) { myId=id; Serial.print(message); printId(); } // class destructor http://www.cplusplus.com/doc/tutorial/classes/ ~HelloWorld() { Serial.print ("Destructing object: "); printId(); } void printId() { Serial.print(" ID:"); Serial.println(myId); Serial.println(" "); } void Salute(char *name) { Serial.print("Hi!, "); Serial.println(name); Serial.print("Regards from object: "); printId(); } }; /* void setup() function it's only executed one time at the start of the execution. It is called from a hidden main() function in the Ronbotis CM-900 IDE core. \ROBOTIS-v0.9.9\hardware\robotis\cores\robotis\main.cpp Note: "while (1)" is a forevr loop ( http://www.cplusplus.com/doc/tutorial/control ): (Basic structure http://www.cplusplus.com/doc/tutorial/program_structure/) int main(void) { setup(); while (1) { loop(); } return 0; } */ void setup() { Serial.begin(57600); //initialize serial USB connection delay(3000); //We will wait 3 seconds, let the user open (Control+Shift+M) the Monitor serial console } //We will not see neither the construction nor the destruction of this global object because serial port it's not still initiated HelloWorld hw0("construction object", 0); //Object construction http://www.cplusplus.com/doc/tutorial/classes/ // A counter to see progress and launch events int iterationCounter=0; //An integer variable to count iterations http://www.cplusplus.com/doc/tutorial/variables // void loop() will be executing the sentences it contains while CM-900 is on. void loop() { // Lets's show only the first 5 iterations http://www.cplusplus.com/doc/tutorial/control/ if (iterationCounter<5) { Serial.print("starting iteration #"); Serial.println(++iterationCounter); // firts, iterationCounter is incremented and then printed // We will see the consttructiona and destruction messages from this local (inside the loop function) object. Object construction http://www.cplusplus.com/doc/tutorial/classes/ HelloWorld hw1("constructing object", 1); hw1.Salute("Joe"); if (iterationCounter==3) { // We will see the consttruction and destruction messages from this local (inside the "if" block inside the "loop" function) object. Objet construction http://www.cplusplus.com/doc/tutorial/classes/ HelloWorld hw2("constructing object", 2); hw2.Salute("Jones"); } // Objet hw2 destruction http://www.cplusplus.com/doc/tutorial/classes/ //Let's show that object hw0 is alive hw0.Salute("Pepe"); Serial.print("ending iteration #"); Serial.println(iterationCounter++); // first cpunter is printed, then incremented. } // Objet hw1 destruction http://www.cplusplus.com/doc/tutorial/classes/ } // Program end. Objet hw0 destruction http://www.cplusplus.com/doc/tutorial/classes/