ЁЯУШ

OOPs Concepts: Zero to Advanced

Jul 4, 2024

OOPs Concepts: Zero to Advanced

Introduction

  • Welcome to the session focused on OOPs (Object-Oriented Programming)
  • Target audience: Students preparing for internships, placements, or college exams in OOPs.
  • Importance of OOPs for tech interviews and strong programming foundation.

Key Topics Covered

  • Theory concepts
  • Examples and code in C++
  • Important definitions and MCQ questions at the end

Object-Oriented Programming (OOPs)

  • What is OOPs?
    • Better way to write code, not always necessary but makes code better and more organized.
    • Real-life usefulness in object representation in code.

Basic Terms in OOPs

  • Class: Blueprint of objects.
  • Object: Instance of a class, representing real-world entities.

Core Concepts in OOPs

  1. Class and Object

    • Example: Teacher class
      • Properties: name, department, subject, salary
      • Methods (member functions): change department, etc.
    • Code Example
    class Teacher { public: string name; string department; string subject; double salary; void changeDepartment(string newDept) { department = newDept; } };
    • Create objects: Teacher t1;
  2. Access Modifiers

    • Types: private, public, protected
    • Controls access to class members
    • Code Example
    class Teacher { private: double salary; public: string name; string department; string subject; }
  3. Encapsulation

    • Wrapping data and methods into a single unit (class).
    • Hides sensitive information using private members.
  4. Constructor and Destructor

    • Constructor: Special function called auto when an object is created. Used for initialization.
    • Destructor: Special function called auto when an object is destroyed. Used for cleanup.
    • Types: Default, Parameterized, Copy
    • Code Example
    class Teacher { public: Teacher() { cout << "Hi, I am a constructor"; } Teacher(string n): name(n) { cout << "Parameterized constructor"; } ~Teacher() { cout << "Destructor called"; } };
  5. Inheritance

    • Deriving new class from existing class.
    • Types: Single, Multilevel, Multiple, Hierarchical, Hybrid
    • Base class (Parent class) and Derived class (Child class)
    • Code Example
    class Person { public: string name; int age; }; class Student : public Person { public: int rollNumber; };
    • Constructor Order: Base class constructor first, then derived class constructor
    • Destructor Order: Derived class destructor first, then base class destructor
  6. Polymorphism

    • Ability to take multiple forms. Two types: Compile-time and Run-time.
    • Compile-time Polymorphism: Function overloading, Constructor overloading
    • Run-time Polymorphism: Function overriding, Virtual functions
    • Code Example
    class Parent { public: virtual void show() { cout << "Parent"; } }; class Child : public Parent { public: void show() override { cout << "Child"; } };
  7. Abstract Classes

    • Base class with at least one pure virtual function.
    • Cannot instantiate objects; used for inheritances
    • Code Example
    class Shape { public: virtual void draw() = 0; // Pure virtual function }; class Circle : public Shape { public: void draw() override { cout << "Drawing Circle"; } };
  8. Static Members

    • Shared among all instances of a class.
    • Static variables/functions persist for the lifetime of the program.
    • Code Example
    class MyClass { public: static int count; MyClass() { count++; } }; int MyClass::count = 0;
  9. Miscellaneous Concepts

    • Friend Function and Friend Class (Explore further as homework)
    • Inter-class interactions
    • Best practices in OOPs

Practice and Revision

  • Solve MCQ questions related to OOPs concepts.
  • Make notes and remember definitions.
  • Apply examples in practical coding scenarios.

Conclusion

  • Review key concepts.
  • Prepare for interviews by deeply understanding and applying OOPs concepts.
  • Best of luck for internships and placements.