ЁЯУШ

OOPS from Zero to Advanced - One Shot Lecture

Jul 9, 2024

Object-Oriented Programming (OOPS) Lecture Notes

Introduction

  • Purpose: Cover OOPS from scratch to advanced level for interview preparation, internships, and college exams
  • Importance: Major companies ask OOP questions in tech interviews; strong OOP understanding is crucial
  • Content: Theory concepts, definitions, practical examples, code implementation, and 30 MCQs for practice

Object-Oriented Programming (OOP)

  • Definition: A programming paradigm that uses objects and classes
  • Practical Implementation: Makes real-life scenarios easier to represent in code
  • Examples: Using libraries (vector, string, stack) in C++ STL implemented via OOP concepts

Key Terms

  • Class: Blueprint of objects; tells how objects should look like (Attributes and Methods)
  • Object: Instance of a class; real-world entities converted into code objects
  • Attributes (Properties): Data stored in objects (e.g., name, department, salary)
  • Methods: Functions inside a class

Example: Teacher Class

  • Attributes: name, department, subject, salary
  • Methods: changeDepartment, calculateTax
  • Implementation: C++ code snippets to define Teacher class and use it to create objects

Access Modifiers

  • Private: Accessible only within the class
  • Public: Accessible from outside the class
  • Protected: Accessible within the class and derived classes
  • Practical Use: Use access modifiers to implement data hiding and encapsulation

Example: Teacher Class with Access Modifiers

  • Attributes: name (public), department (public), subject (public), salary (private)
  • Setter Method: To modify private attributes
  • Getter Method: To access private attributes

Encapsulation

  • Definition: Wrapping data and methods in a single unit (class)
  • Purpose: Data hiding and protecting sensitive information, providing necessary access
  • Example: Teacher class with private salary, public setter and getter methods

Inheritance

  • Definition: Mechanism where a class derives properties and behavior from another class
  • Types of Inheritance:
    • Single: One parent class to one child class
    • Multi-level: Chain of inheritance (e.g., Person -> Student -> GraduateStudent)
    • Multiple: One child class from multiple parent classes
    • Hierarchical: Multiple child classes from one parent class
    • Hybrid: Combination of multiple and hierarchical inheritance
  • Access Specifiers with Inheritance:
    • Public Inheritance
    • Protected Inheritance
    • Private Inheritance

Example: Classes Demonstrating Inheritance

  • Person -> Student: Public inheritance from Person class (name, age) to Student class (roll number)
  • Person -> Teacher: Public inheritance from Person class (name, age) to Teacher class (subject, salary)

Polymorphism

  • Definition: Ability for a function or object to take on multiple forms
  • Types:
    • Compile-time (e.g., Function overloading, Operator overloading)
    • Run-time (e.g., Function overriding, Virtual functions)
  • Function Overloading: Multiple functions with the same name but different parameters
  • Function Overriding: Child class re-implements a function from the parent class

Example: Polymorphism

  • Function Overloading: Different show() methods for int and char
  • Function Overriding: Child class's getInfo() method overloading the parent class's method
  • Virtual Functions: Functions expected to be overridden in derived classes

Abstract Classes

  • Definition: Blueprint for other classes; cannot be instantiated
  • Pure Virtual Functions: Functions with no definition, must be overridden in derived classes
  • Usage: Provide a base class for other classes to derive from

Example: Shape Class

  • Abstract Class: Shape with a pure virtual function draw()
  • Derived Classes: Circle, Square classes implementing the draw function

Constructors

  • Types: Default, Parameterized, Copy
  • Copy Constructor: Copies properties from one object to another
  • Deep Copy vs Shallow Copy: Important when dealing with dynamic memory

Destructors

  • Purpose: De-allocate memory; inverse of constructor
  • Default Destructor: Provided by compiler
  • Custom Destructor: Needed when handling dynamic memory

Static Keyword

  • Usage: Used with variables and methods
  • Static Variables: Lifetime across the entire execution of the program
  • Static Methods: Called on the class rather than instances
  • Static Objects: Persist for the lifetime of the program

Example: Static Members and Methods

  • Static Variable: Shares a single variable among all objects of a class
  • Static Method: Class method instead of an instance method

Friend Functions and Friend Classes

  • Usage: Provides special access to private members of a class
  • Friend Function: Non-member function with access to private data
  • Friend Class: Another class which can access private data

Summary

  • Review definitions, concepts, and examples
  • Apply OOPS concepts in practical scenarios and code
  • Solve given MCQs to strengthen understanding