OOPS - Zero to Advanced

Jul 8, 2024

OOPS (Object-Oriented Programming) - Zero to Advanced Lecture

Introduction

  • Covers entire OOPS concepts from basic to advanced level.
  • Focus on preparing for placement and internship interviews.
  • Important for understanding programming and performing well in technical interviews.

Key Benefits

  • Strong understanding of OOPS benefits interviews and programming skills in companies.
  • Comprehensive coverage of theory, definitions, examples, and coding demonstrations.
  • Ends with 30 MCQs to strengthen understanding.

Objects and Classes

  • Objects: Real-world entities (e.g., pen, laptop, phone). Converted into objects in C++ code.
  • Class: Blueprint for objects. Defines how objects should look and function.
  • Example: Toyota car designs - a standardized blueprint for different cars.

Code Example

  • Teacher System

    • Properties: Name, Department, Subject, Salary.
    • Functions: Change Department, Calculate Salary Tax.
    • Convert these properties and functions into a class and create multiple objects.
    class Teacher {
        std::string name;
        std::string department;
        std::string subject;
        double salary;
    public:
        void setSalary(double s) { salary = s; }
        double getSalary() { return salary; }
    };
    Teacher t1;
    t1.name = "John";
    t1.department = "CS";
    t1.subject = "C++";
    t1.setSalary(25000);
    std::cout << t1.getSalary();
    

Encapsulation

  • Wrapping data and functions into a single unit (Class).
  • Data hiding through private and public access specifiers.
  • Example: Bank system to hide sensitive information like balance and password.

Constructor and Destructor

  • Constructor: Initializes object when created. No return type, same name as class.

  • Destructor: Deallocates memory when object is destroyed. No arguments, same name with ~.

    class Student {
    public:
        Student() { std::cout << "Constructor"; }
        ~Student() { std::cout << "Destructor"; }
    };
    Student s;
    

Inheritance

  • Deriving new classes from existing ones.

  • Promotes code reusability.

  • Types: Single, Multi-level, Multiple, Hierarchical, Hybrid.

    class Person {
    public:
        std::string name;
        int age;
    };
    class Student : public Person {
    public:
        int rollNo;
    };
    

Polymorphism

  • Ability of a function or object to take multiple forms.

  • Types: Compile-time (Function Overloading, Operator Overloading), Run-time (Virtual Functions).

    class Print {
    public:
        void show(int i) { std::cout << i; }
        void show(char c) { std::cout << c; }
    };
    Print p;
    p.show(5); // Prints int
    p.show('a'); // Prints char
    

Abstraction

  • Hiding unnecessary details and showing essential features.

  • Implemented using classes and access specifiers.

    • Example: Abstract classes with virtual functions.
    class Shape {
    public:
        virtual void draw() = 0; // Pure virtual function
    };
    class Circle : public Shape {
    public:
        void draw() { std::cout << "Drawing Circle"; }
    };
    Circle c;
    c.draw();
    

Static Keyword

  • Static Variables: Persistent across function calls. Lifetime of the program.

  • Static Functions: Shared among all objects of the class.

    class Example {
    public:
        static int x;
    };
    int Example::x = 0;
    

Summary

  • Understanding OOPS is essential for performing well in technical interviews and programming in a corporate environment.
  • Focus on encapsulation, inheritance, polymorphism, and abstraction for a strong grasp.
  • Practice with MCQs and real-life implementation examples.