Embedded C Language – Object-Oriented Programming

Object-Oriented Programming vs. Procedural Programming

  • Procedural Programming (POP): Focuses on functions, achieving program functionality through function calls. Data and functions are separate. For example, C is a typical procedural language. When developing a student grade management system, data structures are defined to store student information, followed by writing functions to implement adding, deleting, modifying, and querying functionalities.

  • Object-Oriented Programming (OOP): Centers around objects, achieving program functionality through interactions between objects. An object encapsulates data and the functions that operate on that data. OOP features encapsulation, inheritance, and polymorphism, enhancing code maintainability and extensibility. For instance, in C++, when developing a student grade management system, a <span>Student</span> class is defined, encapsulating student information and methods for manipulating that information.

Device Drivers

A device driver is an interface between the operating system kernel and hardware devices, used to control and manage the operations of hardware devices. The relationships between devices, buses, and drivers are as follows:

  • Device: Hardware devices such as keyboards, mice, printers, USB, etc.

  • Bus: Interface specifications connecting devices to the computer motherboard, such as USB bus, PCI bus, etc. It defines the rules and electrical characteristics for data transmission between devices and the computer.

  • Driver: Programs written for devices that enable the operating system to recognize and operate hardware devices. It acts as a translator between the operating system and hardware devices, converting general device operation commands from the operating system into specific instructions executable by the hardware.

Device Classification:

  • Character Device: Transfers one character (one byte) at a time, usually not requiring complex file operations. For example, keyboard input generates one character of data with each key press, passed to the operating system via the character device driver.

  • Block Device: Transfers multiple bytes or sector data at a time, usually requiring file system support. For example, USB storage devices read and write data in blocks, with the driver responsible for organizing and transferring the data in blocks.

Encapsulation, Inheritance, and Polymorphism in Object-Oriented Programming

  • Encapsulation

    • Definition: Binding data (attributes) and functions (methods) that operate on the data together to form an independent object, hiding the internal implementation details and providing limited access interfaces externally.

    • Function: Enhances code security and maintainability, preventing arbitrary access and modification of internal data from outside.

    • Example: In a <span>BankAccount</span> class, the account balance is encapsulated as a private attribute, with methods like <span>deposit</span> (for deposits) and <span>withdraw</span> (for withdrawals) provided to manipulate the balance, while external access to modify the balance directly is restricted.

  • Inheritance

    • Definition: Allows a new class (subclass) to inherit attributes and methods from an existing class (parent class or base class), with the subclass able to extend or override the functionality of the parent class.

    • Function: Achieves code reuse, reduces code redundancy, and reflects the hierarchical relationships between entities.

    • Example: Define an <span>Animal</span> class with <span>eat</span> and <span>sleep</span> methods. Then define <span>Dog</span> and <span>Cat</span> classes that inherit from the <span>Animal</span> class, automatically acquiring the <span>eat</span> and <span>sleep</span> methods, while also adding their unique methods, such as the <span>bark</span> method for the <span>Dog</span> class and the <span>meow</span> method for the <span>Cat</span> class.

  • Polymorphism

    • Definition: Allows objects of different classes to respond to the same message, meaning the same operation can have different implementations across different objects. At runtime, the appropriate implementation is called based on the actual type of the object.

    • Function: Enhances code flexibility and extensibility, making programs easier to adapt to changes.

    • Example: Define an <span>Shape</span> abstract class containing a pure virtual function <span>area</span>. Then define <span>Circle</span> and <span>Rectangle</span> subclasses that inherit from the <span>Shape</span> class and implement the <span>area</span> function to calculate the area of circles and rectangles. In the main function, the <span>area</span> function can be called through a pointer or reference of type <span>Shape</span>, calculating the corresponding area based on the actual object type it points to.

Abstract Classes, Interfaces, and Detailed Explanation of Polymorphism

  • Abstract Class

    • Definition: A class that contains pure virtual functions, cannot be instantiated, and can only be inherited as a base class. Abstract classes are used to define a set of common interface specifications that subclasses must implement according to specific needs.

    • Function: Implements layered design, provides a unified operation interface, and hides the details of different implementations.

    • Example: In graphical user interface design, define a <span>Component</span> abstract class containing pure virtual functions like <span>paint</span> and <span>mouseClick</span>. Then define specific component classes like <span>Button</span> and <span>TextBox</span> that inherit from the <span>Component</span> class and implement these functions, specifying that all interface components must have drawing and mouse click handling capabilities, while the specific implementation varies by component type.

  • Interface

    • Definition: A special type of abstract class that contains only pure virtual functions (in C++, interfaces can be simulated using abstract classes; in languages like Java and C#, there are explicit interface keywords).

    • Function: Defines a set of specifications that dictate the methods and behaviors an object must provide, without concerning itself with the specific implementation details, allowing different objects to interact according to a unified specification.

    • Example: Define a <span>Drawable</span> interface containing a <span>draw</span> method. Various shape classes (like <span>Circle</span>, <span>Square</span>, etc.) implement the <span>Drawable</span> interface, each implementing the <span>draw</span> method in their own way to render their shapes.

  • Polymorphism Implementation Mechanism

    • There exists an inheritance hierarchy.

    • There are virtual functions in the base class.

    • Virtual functions are called through base class pointers or references.

    • Compile-time Polymorphism (Static Polymorphism): Achieved through function overloading and operator overloading. The appropriate function version is selected at compile time based on the parameter types and counts of the function call.

    • Run-time Polymorphism (Dynamic Polymorphism): Achieved through virtual functions and inheritance. The appropriate function is called at runtime based on the actual type of the object. The following conditions must be met:

    • Example: The example of the <span>Shape</span> abstract class and its subclasses <span>Circle</span> and <span>Rectangle</span> mentioned above demonstrates polymorphism by calling the <span>area</span> function through a base class pointer.

Leave a Comment