Look at c-plus-plus.cpp to see examples of how to use C++ in microcontroller code.
Since our toolchain is based on GCC, and the SDK is written in C, we get C++ support for free due to backwards compatibility. That means you can use many of the features of C++ for embedded programming on a Pico.
Object-oriented programming in C++ is useful for embedded development, since we can create C++ objects that represent the various components and peripherals connected to our Pico.
Exercises
Exercise
Create a C++ class to represent an LED.
Class template:
class LED {public: // Setup the pin as an output. LED(uint gpio); // Turn the LED on or off. void set_state(bool state);};
Exercise
Create a C++ class to represent a button input.
Class template:
class Button {public: // Set pin as input and configure pullup/pulldown Button(uint gpio); // Return whether the button is pressed or not. bool is_pressed();};