11 C++ and Libraries Arduinos are simple microcontrollers. Most of the time, Arduino sketches are quite
small, so using the C programming language works just fine. However, the programming
language for Arduino is actually C++ rather than C. C++ is an extension to the C
programming language that adds something called
object orientation. Object Orientation This is only a short book, so an in-depth explanation of the C++ programming language is
beyond its scope. The book can, however, cover the basics of C++ and object orientation.
But the main goal is to increase the
encapsulation of your programs. Encapsulation keeps
relevant things together, something that makes C++ very suitable for writing libraries such
as those that you have used for the Ethernet and LCD sketches in earlier chapters.
There are many good books on the topics of C++ and object-oriented programming.
Look for the higher-rated books on the topic in your favorite online bookstore.
Classes and Methods Object orientation uses a concept called
classes to aid encapsulation. Generally, a class is
like a section of a program that includes both variables—called
member variables —and
methods , which are like functions but apply to the class. These functions can either be
public , in which case the methods and functions may be used by other classes, or
private ,
in which case the methods can be called only by other methods within the same class.
Whereas an Arduino sketch is contained in a single file, when you are working in C++,
you tend to use more than one file. In fact, there are generally two files for every class: A
header file , which has the extension .h, and the
implementation file , which has the
extension .cpp.