This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
en:iot-open:introductiontoembeddedprogramming2:cppfundamentals:structuresandclasses [2023/11/13 18:15] – ekontoturbo | en:iot-open:introductiontoembeddedprogramming2:cppfundamentals:structuresandclasses [2023/11/23 10:21] (current) – pczekalski | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ==== Structures and Classes ==== | + | ====== Structures and Classes ====== |
+ | {{: | ||
Structures and classes present complex data types, definable by the developer. Not all C/C++ programming environments provide support for classes (e.g., STM32 in HAL framework mode does not), but luckily, the Arduino framework supports it. Structures, conversely, are part of the C language definition and are present in almost every implementation of software frameworks for IoT microcontrollers. | Structures and classes present complex data types, definable by the developer. Not all C/C++ programming environments provide support for classes (e.g., STM32 in HAL framework mode does not), but luckily, the Arduino framework supports it. Structures, conversely, are part of the C language definition and are present in almost every implementation of software frameworks for IoT microcontrollers. | ||
- | === Structures === | + | ==== Structures |
In C and C++, a structure is a user-defined data type that allows you to combine different types of variables under a single name. | In C and C++, a structure is a user-defined data type that allows you to combine different types of variables under a single name. | ||
Line 30: | Line 31: | ||
</ | </ | ||
- | Structures with type definitions are common when authoring libraries to let library users be able to declare new variables on their own, simply using a type. | + | Structures with type definitions are common when authoring libraries to let library users be able to declare new variables on their own, simply using a type.\\ |
- | == Manipulating Structure' | + | ** Manipulating Structure' |
Access to the fields of the structure' | Access to the fields of the structure' | ||
<code c> | <code c> | ||
adr1.city = " | adr1.city = " | ||
adr2.city = " | adr2.city = " | ||
- | adr3.street = "Rodney"; | + | adr3.street = "Lime Street"; |
</ | </ | ||
The structure' | The structure' | ||
<code c> | <code c> | ||
- | adr3 = {"Liverpool", "L1 9EX", "27 Rodney", -2.973083901947872, | + | adr3 = {"Gliwice", "44-100", "1 Lime", -2.973083901947872, |
</ | </ | ||
In C++, structures can also have member functions that manipulate the data (in C, they cannot). That is not so far from the Classes idea described in the following chapter. | In C++, structures can also have member functions that manipulate the data (in C, they cannot). That is not so far from the Classes idea described in the following chapter. | ||
Line 73: | Line 74: | ||
int z = fCalcDataAdd(& | int z = fCalcDataAdd(& | ||
</ | </ | ||
- | === Classes === | + | |
+ | ==== Classes | ||
Classes were introduced in C++ to extend structures encapsulating data and methods (functions) to process this data. A method presented above in the structure context brings an overhead with a need to pass a pointer to the structure for each call. Moreover, it makes access levels tricky, e.g. when you do not want to expose some functions but use them for internal data processing. Thus, classes can be considered as an extension of the structures.\\ | Classes were introduced in C++ to extend structures encapsulating data and methods (functions) to process this data. A method presented above in the structure context brings an overhead with a need to pass a pointer to the structure for each call. Moreover, it makes access levels tricky, e.g. when you do not want to expose some functions but use them for internal data processing. Thus, classes can be considered as an extension of the structures.\\ | ||
< | < | ||
Line 102: | Line 104: | ||
</ | </ | ||
The code above declares a new type, '' | The code above declares a new type, '' | ||
+ | |||
**Constructors**\\ | **Constructors**\\ | ||
There are " | There are " | ||
Line 109: | Line 112: | ||
The above code instantiates an object '' | The above code instantiates an object '' | ||
There can be multiple constructors, | There can be multiple constructors, | ||
+ | |||
**Destructor**\\ | **Destructor**\\ | ||
A destructor is called automatically when an object' | A destructor is called automatically when an object' | ||
Line 115: | Line 119: | ||
</ | </ | ||
<note tip>In the embedded world, explicitly implemented destructors releasing allocated memory are rare as for the safety of the software, dynamic allocation of the memory is rather to be avoided; thus, destructors are eventually related to the network connections more than memory management.</ | <note tip>In the embedded world, explicitly implemented destructors releasing allocated memory are rare as for the safety of the software, dynamic allocation of the memory is rather to be avoided; thus, destructors are eventually related to the network connections more than memory management.</ | ||
+ | |||
**Members**\\ | **Members**\\ | ||
Member fields can be of any type. When marked as '' | Member fields can be of any type. When marked as '' | ||
+ | |||
**Methods**\\ | **Methods**\\ | ||
A method can have any name other than reserved (e.g. for constructors and destructor). Methods marked as '' | A method can have any name other than reserved (e.g. for constructors and destructor). Methods marked as '' | ||
Line 144: | Line 150: | ||
Instantiation and use are similar to the presented ones in the previous examples: | Instantiation and use are similar to the presented ones in the previous examples: | ||
<code c> | <code c> | ||
- | BetterCalculator calc2=BetterCalculator(10, | + | BetterCalculator calc2=BetterCalculator(10, |
+ | // | ||
... | ... | ||
z = calc2.Sub(); | z = calc2.Sub(); | ||
Line 159: | Line 166: | ||
#define h_MYCLASS | #define h_MYCLASS | ||
class Calculator{ | class Calculator{ | ||
- | public: //you can access this part | + | public: |
int x,y; | int x,y; | ||
- | Calculator(); | + | Calculator(); |
Calculator(int px, int py); //Another constructor | Calculator(int px, int py); //Another constructor | ||
- | ~Calculator(); | + | ~Calculator(); |
int Add(); | int Add(); | ||
int Mul(); | int Mul(); | ||
void setX(int px); | void setX(int px); | ||
void setY(int py); | void setY(int py); | ||
- | private: //that part is private, and you cannot access it | + | private: |
+ | //and you cannot access it | ||
void clear(); | void clear(); | ||
}; | }; |