OOP programming intro.
Dive into the world of OOP programming with a focus on encapsulation. Explore concepts like inheritance, polymorphism, and data abstraction through practical examples in C++. Understand how to implement encapsulation effectively for clean and maintainable code.
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
Button.cpp Implementation #include "Button.hpp" Button::Button(int buttonPin, long int debounce) { pin = buttonPin; prevState = HIGH; prevTime = 0; counter = 0; debounceDelay = debounce; pinMode(pin, INPUT_PULLUP); } Button.hpp #ifndef BUTTON_HPP #define BUTTON_HPP #include <Arduino.h> void Button::update(long int currentTime) { bool currentState = digitalRead(pin); class Button { private: if (currentState != prevState && currentTime - prevTime > debounceDelay) { if (currentState == LOW) { counter++; } prevTime = currentTime; } int pin; bool prevState; long int prevTime; int counter; long int debounceDelay; prevState = currentState; } public: Button(int buttonPin, long int debounce = 50); bool Button::isPressed() { return prevState == LOW; } void update(long int currentTime); bool isPressed(); int Button::getCounter() { return counter; } int getCounter(); void resetCounter(); long int timeSinceLastPress(long int currentTime); void Button::resetCounter() { counter = 0; } }; #endif long int Button::timeSinceLastPress(long int currentTime) { return currentTime - prevTime; }
INO #include "Button.hpp" Button button(2); void setup() { Serial.begin(9600); } void loop() { long int currentTime = millis(); button.update(currentTime); if (button.isPressed()) { Serial.print("Button pressed, count: "); Serial.println(button.getCounter()); } // Do other non-blocking tasks here }
Polymorphism Interface** implementation (Abstract class) **Interfaces are not a concept in the OOP of C++. They can be simulated by abstract classes
How to refer to a Class/Object method ., -> , :: Symbol . . -> pointer Foo *foo = new Foo(); foo->member_var = 10; foo->member_func(); . instance Foo foo; foo.member_var = 10; foo.member_func(); :: namspace int some_val = Foo::static_var; Foo::static_method(); int max_int = std::numeric_limits<int>::max();
this The keyword this identifies a special type of pointer. Suppose that you create an object named x of class A, and class A has a non-static member function f(). If you call the function x.f(), the keyword this in the body of f() stores the address of x. Advanced links *this vs this