Object-Oriented Programming Lecture Notes

Jul 14, 2024

Object-Oriented Programming (OOP)

Introduction

  • Presenter: Saldina, software engineer
  • Channel: CodeBeauty on YouTube
  • Topic: Object-Oriented Programming (OOP)

Course Structure

  • Beginner to Advanced: Starts with basic concepts and moves to advanced concepts
  • Implementation: Explanation and code implementation of concepts
  • Resources: Full content available in the video description
  • Additional Learning: More C++ videos on the CodeBeauty channel

What is OOP?

  • Definition: OOP is a programming paradigm consisting of rules, ideas, and standards used to solve specific types of problems.
  • Purpose: To represent real-life objects and their attributes/behaviors in programs.
  • Example: Car as an entity with attributes (manufacturer, color, price) and behaviors (drive, stop, fuel).

Classes and Objects

  • Class: Building block of OOP; a user-defined data type.
  • Attributes and Behaviors: Represent real-life entities (e.g., car, game, student) in programs.
  • Example Implementation: Creating an Employee class with name, company, and age attributes.

Creating a Class in C++

  1. Syntax:
    class Employee {
        public:
            std::string name;
            std::string company;
            int age;
    };
    
  2. Important Notes:
    • Use ; after class declaration
    • Attributes can be added incrementally

Constructors

  • Definition: Special type of method invoked when an object of a class is created.
  • Creating a Constructor:
    class Employee {
        public:
            Employee(std::string name, std::string company, int age) {
                this->name = name;
                this->company = company;
                this->age = age;
            }
    };
    
  • Passing Values: Directly pass and initialize values using the constructor.

Access Modifiers

  • Private: Hidden from outside the class.
  • Public: Accessible outside the class.
  • Protected: Accessible in derived classes but not outside the class hierarchy.
  • Example: Setting name, company, and age as private and using getters and setters to access them.

Getters and Setters

  1. Setter:
    void setName(std::string name) { this->name = name; }
    
  2. Getter:
    std::string getName() { return name; }
    
  3. Validation: Add rules (e.g., age above 18) within setters.

Four Pillars of OOP

  1. Encapsulation: Binding data and methods that operate on data within a class; prevents direct access but allows controlled access via methods (getters/setters).
  2. Abstraction: Hiding complex logic, exposing only necessary parts; achieved using abstract classes/interfaces.
  3. Inheritance: Mechanism of basing a new class on an existing class, obtaining its attributes and methods.
  4. Polymorphism: Ability of objects/methods to take on many forms, often via base class references referring to child class objects.

Encapsulation

  • Implementation: Use private attributes with public getters/setters.
  • Purpose: Control and protect access to data.

Abstraction

  • Concept: Hide complex logic behind simple interfaces (e.g., smartphone features like taking a picture).
  • Implementation: Use abstract classes with pure virtual functions.

Inheritance

  • Base and Derived Classes: New class inherits attributes and methods from an existing class.
  • Example: Developer and Teacher classes inheriting from Employee class
  • Syntax:
    class Developer : public Employee {
        public:
            std::string favoriteProgrammingLanguage;
    };
    

Polymorphism

  • Definition: Use base class references to refer to derived class objects.
  • Example:
    Employee* e = &developer;
    e->work();
    
  • Key Implementation: Mark methods in base class as virtual to enable derived class overrides.

Summary and Conclusion

  • Key Concepts: Encapsulation, Abstraction, Inheritance, Polymorphism
  • Practical Use: Creating versatile and maintainable code structures
  • Further Learning: Explore more videos on CodeBeauty channel for in-depth understanding and examples.