$USD
  • EUR€
  • £GBP
  • $USD
TUTORIALS ESP32ESP8266

ESP32 / ESP8266 Tutorial: Class constructors

DFRobot Apr 20 2018 713

The objective if this post is to give an introduction on class constructors. The tests on the ESP32 were performed using a DFRobot’s ESP32 module device integrated in a ESP32 development board. The tests on the ESP8266 were performed on a DFRobot’s ESP8266 development board.

Introduction

The objective if this post is to give an introduction on class constructors. Even tough this was tested on the Arduino core running on the ESP32 and on the ESP8266, this tutorial should work on other boards compatible with the Arduino environment.

Note also that the classes and constructors features we are using are from C++ (the language on top of which Arduino is built), which means that the scope of these tutorials extend beyond the scope of microcontrollers programming.

In this previous post, we created a very simple class with two  data members initialized with some default values. Nonetheless that class was not that much useful. So We will enhance it a little bit by analyzing the concept of constructors.

Constructors are functions of a class that are called when an object of that class is instantiated [1]. Thus, we can use them for initializing class data members when the class is instantiated. In order for this to be possible, we can specify input arguments for the constructor when we declare it, pretty much like we do when creating a regular function.

Furthermore, we can declare more than one constructor, as long as the input parameters are different. This is called constructor overloading [2]. Nonetheless, we are not going to cover this subject on this tutorial.

In terms of syntax, a constructor has the exact same name of the class and it doesn’t return any type. This means that it doesn’t even return void [1].

An important thing to mention is that even when we don’t specify any constructor in the code, the compiler will generate a default one for us, which expects no parameters and has an empty body [2]. So, in the previous tutorial, this was what happened, since we did not specify any constructor for the declared class.

The tests on the ESP32 were performed using a DFRobot’s ESP32 module device integrated in a ESP32 development board. The tests on the ESP8266 were performed on a DFRobot’s ESP8266 development board.

The code

For simplicity, we will declare our class in the main code file. To do it, we will use the class keyword followed by the class name and then enclose the class definition in brackets.

class TestClass{
   //Class declaration
};

As we did before, we will specify two  data members for our class but this time we won’t initialize them. We will use the public access modifier to allow them to be externally accessed.

class TestClass{
 
  public:
    int x;
    int y;
 
};

Now we will declare a constructor that receives two input parameters and initializes the previous data members. We will also declare it under the public modifier, so it is accessible in our main code.

Inside the constructor we can directly access the class data members and assign their values from the input arguments.

class TestClass{
 
  public:
    int x;
    int y;
 
    TestClass(int arg1, int arg2){
      x = arg1;
      y = arg2;
    }
};

So after finishing our class declaration we can now move to the Arduino setup function. The first thing we will do is opening a serial connection, so we can later output the results of our program.

Serial.begin(115200);

Next, we will instantiate an object of our class using the constructor to pass the initialization values for the data members. Remember that our constructor receives two arguments, which initialize both the x and y data members of the class.

TestClass myObject(10, 20);

To finalize the code, we will print the value of the data members to confirm they were correctly initialized. Remember that we can access the data members of the class object using the dot operator, since they are public.

Serial.println(myObject.x);
Serial.println(myObject.y);

You can check below the full source code.

class TestClass{
 
  public:
    int x;
    int y;
 
    TestClass(int arg1, int arg2){
      x = arg1;
      y = arg2;
    }
};
 
void setup() {
 
  Serial.begin(115200);
 
  TestClass myObject(10, 20);
 
  Serial.print("Data member x:");
  Serial.println(myObject.x);
 
  Serial.print("Data member y:");
  Serial.println(myObject.y);
 
}
 
void loop() {}

Testing the code

To test the code, simply compile it and upload it to your device. Then, open the Arduino IDE serial monitor. As shown in figure 1, you should get as output the values of both x and y data members printed to the serial console.

Figure 1 – Output of the program.


DFRobot supply lots of esp32 arduino tutorials and esp32 projects for makers to learn.


REVIEW