Learn from: Tai Chi Maker.
Practice is the only criterion for testing the truth. Practicing C++ via Arduino is a good option.
Overview of the previous section
The first part is mainly aware of the packaging practices of this concept.
hypothesis
There are two main parts in which each object differs from other objects:
One,external diff: object name.
two,internal difference: The value of the attribute of the object itself, that is, the value of the data member.
The process of creating an object:
During the program execution process, when an Object statement is encountered, the program will apply to a specific memory space of the operating system to store the new object. (Similar to the previous variable creation process, but the variable is different: variables will record the initial value of the variable when allocating memory space, and the class object is too complex to implement, and the constructor needs to be built on this time.)
Building
Summary of relevant knowledge
the influence: When an object is created, the object is used to initialize the object to a state of a certain state.
notice:
① also builderclass member function。
② The name of the constructor functionSame class nameAnd there is no going back.
③ The constructor is usually declaredpublic function。
④ the constructor willconnect automatically。
⑤ Constructor that should not provide parameters to callsdefault constructor。
⑥do not writeconstructor willAutomatically create a hidden default constructorThe lists of parameters and functions are empty. (If you type, you will not be updated ·)
⑦ The constructor can directly access all data members of the class, but the inner union can fetch parameters or reload.
⑧Builder version: its parameters are links to this class. The role is: use an existing object (defined by the replication constructor parameter) to initialize a new object of the same type.
hardware design
Help program
class Led {
public:
Led();
Led(int userLedPin);
void on();
void off();
void setLedPin(int userLedPin);
int getLedPin();
private:
int ledPin = 3;
};
Led::Led(){
Serial.println("Led Object Created.");
pinMode(ledPin,OUTPUT);
}
Led::Led(int userLedPin) {
Serial.println("Led Object Created.");
ledPin = userLedPin;
pinMode(ledPin, OUTPUT);
}
inline int Led::getLedPin() {
return ledPin;
}
void Led::setLedPin(int userLedPin ) {
ledPin =userLedPin;
pinMode(ledPin,OUTPUT);
}
void Led::on() {
digitalWrite(ledPin, HIGH);
}
void Led::off() {
digitalWrite(ledPin, LOW);
}
void setup() {
Serial.begin(9600);
Led myLed;
Led myLed2(5);
for (int i = 0; i < 5; i++) {
myLed.on();
myLed2.on();
delay(500);
myLed.off();
myLed2.off();
delay(500);
}
Serial.println(myLed.getLedPin());
Serial.println(myLed2.getLedPin());
}
void loop() {
}
experimental phenomena
Output in serial monitor:Two LEDs flash five times.
Detailed explanation of the code
Led();
、Led(int userLedPin);
All constructor.
That constructor can be foundThis is consistent with the class nameand don’t write the data type (No return value) , At what timeWhen the object is createdThen you will startyou realizeConstructor (the initialized number of pins and the serial port output display “LED object created”), and there are two types of constructors to find. This is the use of C++ in C++overloadThe concept, according to the format generated by the object, define different constructors.
destructive function
The famous constructor was called when the object was first created. What about the struct function? This is called when an object disappears. In fact, this is why the object we created is written heresetup()
On a mission.
Because if a local object is defined in a function, the object in the function goes away when the function calls the end of the function.
the influence:
Used to complete some cleaning work before deleting the object.
② Automatically called at the end of the thread stay.
③ Usually a member of a public office.
④ The name of the development function consists of “~” in front of the class name.
⑤ There is no return value.
⑥ It does not take any parameters, but it can be a virtual function.
⑦ If no description is displayed, it automatically creates an implicit destructor with the function body empty.
note: A destructor that runs empty may do nothing.
We can add an analytic function to the above program.
hardware design
Same as above.
Help program
Добавьте к классу светодиода в вышеуказанной справочной программе: ~Led();,придется:
class Led {
public:
Led();
Led(int userLedPin);
~Led();
void on();
void off();
void setLedPin(int userLedPin);
int getLedPin();
private:
int ledPin = 3;
};
И добавить оператор определения функции анализа:
Led::~Led(){
Serial.println("Led Object Deleted.");
}
experimental phenomena
Serial screen outputTwo LEDs flash five times.
Detailed explanation of the code
It was added to the destructor and since the object was created heresetup()
In this function, it is a local object. When the function returns the call, the object disappears, destruct is called, and “object deleted” is printed in the serial monitor.