Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
en:iot-open:introductiontoembeddedprogramming2:cppfundamentals:structuresandclasses [2023/11/17 16:48] pczekalskien:iot-open:introductiontoembeddedprogramming2:cppfundamentals:structuresandclasses [2023/11/23 10:21] (current) pczekalski
Line 1: Line 1:
 ====== Structures and Classes ====== ====== Structures and Classes ======
 +{{:en:iot-open:czapka_b.png?50| General audience classification icon }}{{:en:iot-open:czapka_e.png?50| General audience classification icon }}\\
 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 37: Line 38:
 adr1.city = "Gliwice"; adr1.city = "Gliwice";
 adr2.city = "Oslo"; adr2.city = "Oslo";
-adr3.street = "Rodney";+adr3.street = "Lime Street";
 </code> </code>
 The structure's data can be initialised member by a member or at once using the simplified syntax. Order is meaningful, and types need to fit the definition (C++ only): The structure's data can be initialised member by a member or at once using the simplified syntax. Order is meaningful, and types need to fit the definition (C++ only):
 <code c> <code c>
-adr3 = {"Liverpool", "L1 9EX", "27 Rodney", -2.973083901947872, 53.401615049766406 };+adr3 = {"Gliwice", "44-100", "1 Lime", -2.973083901947872, 53.401615049766406 };
 </code> </code>
 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 74: Line 75:
 </code> </code>
  
-=== 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.\\
 <note>Classes are an important part of IoT programming as they bring an idea of the digital twin to low-level programming: a class used to represent a single piece of hardware, e.g. a sensor and provide its current state, access to the data it grabs and gives access to the operations on the hardware. Thus, in most IoT projects, each device external to the MCU that composes an IoT device is represented by one or more classes on the software side.</note> <note>Classes are an important part of IoT programming as they bring an idea of the digital twin to low-level programming: a class used to represent a single piece of hardware, e.g. a sensor and provide its current state, access to the data it grabs and gives access to the operations on the hardware. Thus, in most IoT projects, each device external to the MCU that composes an IoT device is represented by one or more classes on the software side.</note>
Line 149: 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,6); //BetterCalculator->Calculator->x=10, y=6+  BetterCalculator calc2=BetterCalculator(10,6);  
 +                   //BetterCalculator->Calculator->x=10, y=6
   ...   ...
   z = calc2.Sub(); //z=4   z = calc2.Sub(); //z=4
Line 164: Line 166:
 #define h_MYCLASS #define h_MYCLASS
 class Calculator{ class Calculator{
-  public: //you can access this part+  public:                       //you can access this part
   int x,y;   int x,y;
-    Calculator();  //Default constructor+    Calculator();               //Default constructor
     Calculator(int px, int py); //Another constructor     Calculator(int px, int py); //Another constructor
-    ~Calculator(); //This is dummy destructor+    ~Calculator();              //This is dummy destructor
     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:                      //that part is private,  
 +                                //and you cannot access it      
     void clear();     void clear();
 }; };
en/iot-open/introductiontoembeddedprogramming2/cppfundamentals/structuresandclasses.1700239735.txt.gz · Last modified: 2023/11/17 16:48 by pczekalski
CC Attribution-Share Alike 4.0 International
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0