Arduino C++ examples tutorial using Duemilanove (I)

This entry is part 5 of 5 in the series Cheapest robot (Arduino)

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 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:

Hello World shows messages on construction and destruction
Also it lets you to salute.
Showing always its assigned identifiction number MyId
class HelloWorld
int myId; //Object identification num ber


// class constructor
HelloWorld(char *message, byte id)

// class destructor
Serial.print ("Destructing object: ");

void printId()
Serial.print(" ID:");
Serial.println(" ");

void Salute(char *name)
Serial.print("Hi!, ");
Serial.print("Regards from object: ");

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.

Note: "while (1)" is a forevr loop ( ):

(Basic structure

int main(void) {

while (1) {
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

// A counter to see progress and launch events
int iterationCounter=0; //An integer variable to count iterations

// void loop() will be executing the sentences it contains while CM-900 is on.
void loop()
// Lets's show only the first 5 iterations
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
HelloWorld hw1("constructing object", 1);

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
HelloWorld hw2("constructing object", 2);
} // Objet hw2 destruction

//Let's show that object hw0 is alive

Serial.print("ending iteration #");
Serial.println(iterationCounter++); // first cpunter is printed, then incremented.
} // Objet hw1 destruction
} // Program end. Objet hw0 destruction
Series Navigation<< Arduino LCD 1602 (16×2) display

Comments are closed.

%d bloggers like this: