I have a doubt. I need to use constructor and class in the coding. I don't know how to explain in words. So here is my coding
class Cuadrado {
public:
Cuadrado(float x); // constructor is called
float area (float x); // method defined inside the class
float perimetro (float x);
float longitud_diagonal(float x);
} ;
// Member functions definitions
Cuadrado :: Cuadrado (float x) {
Serial.println(" Longitud del lado: ");
while (Serial.available() == 0) {
}
}
float Cuadrado :: area(float x) {
return x * x;
}
float Cuadrado :: perimetro(float x) {
return 4 * x;
}
float Cuadrado :: longitud_diagonal(float x) {
return sqrt(2) * x;
}
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
float lado = Serial.parseFloat();
//float lado = ladoString.toFloat()
Cuadrado cuadrado1(lado);
Serial.println(lado);
Serial.print(" Area del cuadrado: ");
Serial.println(cuadrado1.area(lado));
Serial.print(" Perimetro del cuadrado: ");
Serial.println(cuadrado1.perimetro(lado));
Serial.print(" Longitud diagonal del cuadrado: ");
Serial.println(cuadrado1.longitud_diagonal(lado));
Serial.println(" ");
}
what come out in the serial monitor when the serial input is 2 is:
Longitud del lado:
0.00
Area del cuadrado: 0.00
Perimetro del cuadrado: 0.00
Longitud diagonal del cuadrado: 0.00
Longitud del lado:
2.00
Area del cuadrado: 4.00
Perimetro del cuadrado: 8.00
Longitud diagonal del cuadrado: 2.83
and the question is why it also calculate for longitud 0 ? And everytime i put the input in the serial monitor, it always came out the ' zero longitud copy' . i don't know if i explained it well.
I changed your code a tiny bit, too lazy to explain, right at the beginning of loop().
Also set the line ending in the serial monitor to "No line ending", it's next to the baud setting at the bottom, you have to read up on Serial.parseFloat and using the serial monitor. So do I for that matter, I never use it.
class Cuadrado {
public:
Cuadrado(float x); // constructor is called
float area (float x); // method defined inside the class
float perimetro (float x);
float longitud_diagonal(float x);
} ;
// Member functions definitions
Cuadrado :: Cuadrado (float x) {
Serial.println(" Longitud del lado: ");
while (Serial.available() == 0) {
}
}
float Cuadrado :: area(float x) {
return x * x;
}
float Cuadrado :: perimetro(float x) {
return 4 * x;
}
float Cuadrado :: longitud_diagonal(float x) {
return sqrt(2) * x;
}
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial.println("hello world!");
}
void loop() {
// put your main code here, to run repeatedly:
float lado;
Cuadrado cuadrado1(lado);
lado = Serial.parseFloat();
Serial.println(lado);
Serial.print(" Area del cuadrado: ");
Serial.println(cuadrado1.area(lado));
Serial.print(" Perimetro del cuadrado: ");
Serial.println(cuadrado1.perimetro(lado));
Serial.print(" Longitud diagonal del cuadrado: ");
Serial.println(cuadrado1.longitud_diagonal(lado));
Serial.println(" ");
}
Where is the constructor called? I am not able to learn C++ but it appears that a new CuradorElDorado is created at every loop() iteration as a local object.*
By which point Serial is fully up to speed.
*Which object, I assume, gets destroyed when loop returns.
In this application it works, but used as a harmless global variable, it halts the sketch.
My objects don't have such hidden features, if I can avoid it.