OOP programming intro.

OOP programming intro.
Slide Note
Embed
Share

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.

  • OOP Programming
  • Encapsulation
  • Inheritance
  • Polymorphism
  • Data Abstraction

Uploaded on Feb 23, 2025 | 0 Views


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


  1. OOP programming intro.

  2. OOP

  3. Button Class

  4. UML diagram

  5. 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; }

  6. 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 }

  7. Encapsulation ()

  8. Data Abstraction

  9. Parent/Child, Base/Derived

  10. Inheritance Override

  11. Polymorphism Override of Methods

  12. Polymorphism Interface** implementation (Abstract class) **Interfaces are not a concept in the OOP of C++. They can be simulated by abstract classes

  13. Polymorphism - Overloading

  14. 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();

  15. 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

More Related Content