Fundamentals of Object Oriented Programming

Aug 18, 2024

Object Oriented Programming Lecture Notes

Introduction

  • Today's topic: Object Oriented Programming (OOP)
  • Importance of OOP in C++ at the industrial level
  • Fundamental OOP topics relevant for interviews and placements

Agenda

  1. Why OOP?
  2. What is OOP?
  3. Fundamentals of OOP:
    • Class
    • Object
    • Constructor
    • Destructor
    • Encapsulation
    • Abstraction
    • Inheritance
    • Access Specifiers
    • Polymorphism
  4. Code implementation alongside theoretical explanations

Why Object Oriented Programming?

  • Transition from Procedure Oriented Programming (POP)
  • POP: Conventional programming paradigm using high-level languages (e.g., C, COBOL, FORTRAN)
    • Programs divided into tasks grouped into functions
    • Heavy reliance on global data
    • Problems with global data:
      • Difficult to track changes
      • Changes in data structure require extensive updates
  • OOP focuses on binding data with functions using objects, improving data protection and accessibility.

What is OOP?

  • OOP is a programming paradigm based on objects that have data and methods associated with them.
  • Objects can only be accessed through their respective functions, enhancing data security.

Key Concepts in OOP

Class

  • User-defined data type that provides a blueprint for creating objects.
  • Example: Defining a Fruit class with properties (e.g., color, taste).

Object

  • Instance of a class; variables of a class type.
  • Example: Creating objects like mango, apple, grapes from the Fruit class.

Constructor

  • Special function called when an object is created to initialize its properties.
  • Types of constructors:
    • Default Constructor: No parameters, initializes with default values.
    • Parameterized Constructor: Accepts parameters to initialize properties.
    • Copy Constructor: Initializes an object using another existing object.

Destructor

  • Function called when an object is deleted, cleans up resources.
  • Defined using the tilde operator (~) followed by the class name.

Encapsulation

  • Bundling of data (attributes) and methods (functions) into a single unit (class).
  • Restricts direct access to some object components, which can be achieved using access specifiers.

Abstraction

  • Hiding unnecessary details from the user and showing only essential features.
  • Example: Using a function without needing to understand its internal workings.

Inheritance

  • Mechanism where a new class derives properties and behaviors (methods) from an existing class.
  • Types of inheritance:
    • Single Inheritance: One class inherits from another.
    • Multi-level Inheritance: A class inherits from a derived class.
    • Multiple Inheritance: A class inherits from multiple parent classes.
    • Hierarchical Inheritance: Multiple classes inherit from a single parent class.
    • Hybrid Inheritance: Combination of multiple inheritance types.

Access Specifiers

  • Define how members of a class can be accessed:
    • Public: Accessible from anywhere.
    • Protected: Accessible in the class itself and derived classes.
    • Private: Accessible only within the class.

Polymorphism

  • Ability for different classes to be treated as instances of the same class through inheritance.
  • Types of polymorphism:
    • Compile-time Polymorphism: Resolved during compilation (e.g., function overloading, operator overloading).
    • Runtime Polymorphism: Resolved during program execution (e.g., function overriding with virtual functions).

Friend Function

  • A non-member function that can access private members of a class.
  • Declared using the friend keyword within the class definition.

Conclusion

  • Comprehensive coverage of fundamental OOP concepts in C++ for future applications in data structures and algorithms.
  • Next lecture will build on these concepts using linked lists.
  • Recommended: Review the video, practice coding examples, and implement function/operator overloading.