logo

Unit 5: Object-Oriented Programming (OOP) (10 Hours)

Computer Science - Class 12

MCQ questions

Unit 5: Object-Oriented Programming (OOP) (10 Hours)

5.1 Programming Paradigms: Procedural, Structural, and Object-Oriented

Programming paradigms are different styles or approaches to programming that dictate how solutions are formulated.

  • Procedural Programming:
    • Based on the concept of procedure calls.
    • Emphasizes functions or procedures to operate on data.
    • Example languages: C, Pascal.
    • Advantages: Simplicity and ease of understanding; good for small programs.
  • Structural Programming:
    • An extension of procedural programming.
    • Focuses on improving the clarity and efficiency of code by using structures (such as control flow structures).
    • Encourages breaking programs into smaller, manageable pieces.
    • Example languages: C, Ada.
  • Object-Oriented Programming:
    • Organizes software design around data (objects) rather than functions and logic.
    • Promotes concepts such as encapsulation, inheritance, and polymorphism.
    • Example languages: C++, Python.
    • Advantages: Improved code reusability, scalability, and maintainability.

5.2 Features of OOP: Class, Object, Polymorphism, and Inheritance

OOP is characterized by several key features:

  • Class:

    • A blueprint for creating objects.
    • Encapsulates data for the object and methods to manipulate that data.
    • Example in C++:
      cpp
      class Car { public: string color; string model; void displayInfo() { cout << "Model: " << model << ", Color: " << color << endl; } };
  • Object:

    • An instance of a class.
    • Contains real values instead of variables.
    • Example:
      cpp
      Car myCar; // myCar is an object of class Car myCar.color = "Red"; myCar.model = "Toyota"; myCar.displayInfo(); // Output: Model: Toyota, Color: Red
  • Polymorphism:

    • The ability to present the same interface for different data types.
    • Achieved through:
      • Method Overloading: Multiple functions with the same name but different parameters.
        cpp
        class Math { public: int add(int a, int b) { return a + b; } double add(double a, double b) { return a + b; } };
      • Method Overriding: A derived class provides a specific implementation of a method already defined in its base class.
        cpp
        class Animal { public: virtual void sound() { cout << "Animal sound"; } }; class Dog : public Animal { public: void sound() override { cout << "Bark"; } };
  • Inheritance:

    • Mechanism by which one class (derived class) can inherit the attributes and methods of another class (base class).
    • Types of inheritance:
      • Single Inheritance: One derived class inherits from one base class.
      • Multiple Inheritance: One derived class inherits from multiple base classes.
      • Multilevel Inheritance: A class is derived from another derived class.
      • Hierarchical Inheritance: Multiple classes are derived from the same base class.
    • Example:
      cpp
      class Vehicle { // Base class public: void start() { cout << "Starting vehicle"; } }; class Car : public Vehicle { // Derived class public: void honk() { cout << "Car honks"; } };

5.3 Advantages of OOP

  • Modularity: Code is organized into discrete classes and objects, enhancing clarity and organization.
  • Reusability: Classes and objects can be reused across different programs, saving time and effort.
  • Maintainability: Easier to update and maintain code due to modular design.
  • Abstraction: Hides complex implementation details and exposes only the necessary features to the user.
  • Inheritance: Facilitates code reuse and extension through derived classes.

5.4 Applications of OOP

OOP is widely used across various domains due to its flexibility and effectiveness. Some applications include:

  • Software Development: Used in creating desktop applications, mobile apps, and web applications.
  • Game Development: Helps in modeling complex interactions between game objects.
  • Simulation and Modeling: Useful in scientific computing, simulations, and modeling real-world systems.
  • Database Management: Facilitates the design of object-oriented databases that model complex data relationships.