Class 11-12

Class 11 Computer Science

BSEB 100-Day Study Plan
Notes, MCQs, and Solutions

Explore Class 11

Class 12 Computer Science

Advanced Topics & Practical Guide
Board Exam Preparation

Explore Class 12

Day 52: Encapsulation and Abstraction in C++ – Concepts, Benefits, and Examples | Class 11, Chapter 5

Learn encapsulation & abstraction in C++ with Day 52 of Class 11 Computer Science, Chapter 5. Understand concepts, advantages, and practical examples.

Day 52 – Encapsulation and Abstraction in C++

Class 11 Computer Science – Chapter 5

What is Encapsulation? (Encapsulation क्या है?)

Encapsulation in C++ is the process of binding data and functions that operate on the data into a single unit, known as a class. It helps in restricting direct access to certain members of the object.
C++ में Encapsulation वह प्रक्रिया है जिसमें डेटा और उस पर ऑपरेट करने वाले फ़ंक्शन्स को एक इकाई (क्लास) में बांधा जाता है। यह ऑब्जेक्ट के कुछ सदस्यों तक सीधे पहुंच को प्रतिबंधित करने में मदद करता है।

Advantages of Encapsulation (Encapsulation के लाभ)

  • Enhances data security by hiding internal details.
    आंतरिक विवरणों को छिपाकर डेटा सुरक्षा को बढ़ाता है।
  • Improves code modularity and reusability.
    कोड मॉड्यूलैरिटी और पुन: उपयोगिता में सुधार करता है।
  • Allows controlled access using access specifiers (public, private, protected).
    एक्सेस स्पेसिफायर्स (पब्लिक, प्राइवेट, प्रोटेक्टेड) का उपयोग करके नियंत्रित पहुंच की अनुमति देता है।

What is Abstraction? (Abstraction क्या है?)

Abstraction in C++ is the process of hiding the implementation details and showing only the essential features of an object.
C++ में Abstraction वह प्रक्रिया है जिसमें कार्यान्वयन विवरणों को छिपाया जाता है और केवल एक ऑब्जेक्ट की आवश्यक विशेषताएँ दिखाई जाती हैं।

Advantages of Abstraction (Abstraction के लाभ)

  • Reduces complexity by hiding unnecessary details.
    अनावश्यक विवरणों को छिपाकर जटिलता को कम करता है।
  • Improves code readability and maintainability.
    कोड की पठनीयता और रखरखाव में सुधार करता है।
  • Provides flexibility to change implementation without affecting users.
    उपयोगकर्ताओं को प्रभावित किए बिना कार्यान्वयन को बदलने की लचीलापन प्रदान करता है।

Examples of Encapsulation and Abstraction in C++ (C++ में Encapsulation और Abstraction के उदाहरण)

Example 1: Encapsulation Using Access Specifiers

#include <iostream>
using namespace std;

class Employee {
private:
    string name;
    int age;

public:
    void setDetails(string empName, int empAge) {
        name = empName;
        age = empAge;
    }

    void displayDetails() {
        cout << "Name: " << name << endl;
        cout << "Age: " << age << endl;
    }
};

int main() {
    Employee emp;
    emp.setDetails("John", 30);
    emp.displayDetails();
    return 0;
}

Output:

Name: John
Age: 30

Example 2: Abstraction Using Abstract Class

#include <iostream>
using namespace std;

class Shape {
public:
    virtual void draw() = 0; // Pure virtual function
};

class Circle : public Shape {
public:
    void draw() override {
        cout << "Drawing Circle" << endl;
    }
};

class Rectangle : public Shape {
public:
    void draw() override {
        cout << "Drawing Rectangle" << endl;
    }
};

int main() {
    Shape* shape1 = new Circle();
    Shape* shape2 = new Rectangle();

    shape1->draw();
    shape2->draw();

    delete shape1;
    delete shape2;
    return 0;
}

Output:

Drawing Circle
Drawing Rectangle

Practice Questions

Multiple Choice Questions (MCQs)

  1. What does encapsulation bind together in a single unit?
    (a) Data and methods | (b) Objects and classes | (c) Functions and loops | (d) None
  2. Which access specifier is used to hide members of a class?
    (a) Public | (b) Private | (c) Protected | (d) None
  3. What is abstraction primarily used for?
    (a) Hiding implementation details | (b) Hiding data | (c) Reusing code | (d) None
  4. Which of the following enables data hiding?
    (a) Abstraction | (b) Encapsulation | (c) Inheritance | (d) None
  5. What is a pure virtual function?
    (a) Function without implementation | (b) Function with private access
    (c) Function used in inheritance | (d) None
  6. What is the primary advantage of encapsulation?
    (a) Faster execution | (b) Data security | (c) Code flexibility | (d) None
  7. Which feature hides implementation details in a class?
    (a) Encapsulation | (b) Abstraction | (c) Polymorphism | (d) None
  8. What is the output of the following code?
    class A {
    private:
        int x;
    public:
        void set(int value) {
            x = value;
        }
        int get() {
            return x;
        }
    };
    
    int main() {
        A obj;
        obj.set(5);
        cout << obj.get();
        return 0;
    }
            
    (a) 0 | (b) 5 | (c) Error | (d) None
  9. Which of the following can be considered as abstraction?
    (a) Interfaces | (b) Abstract classes | (c) Both | (d) None
  10. What is the default access specifier in a class?
    (a) Public | (b) Private | (c) Protected | (d) None

Answers to MCQs:

1: (a), 2: (b), 3: (a), 4: (b), 5: (a), 6: (b), 7: (b), 8: (b), 9: (c), 10: (b)

Short Answer Questions

  1. What is encapsulation in C++?
    Answer: Encapsulation is the process of bundling data and methods into a single unit, providing controlled access.
  2. Explain the difference between encapsulation and abstraction.
    Answer: Encapsulation binds data and methods, while abstraction hides implementation details and shows essential features.
  3. Write a program to demonstrate encapsulation in C++.
    Answer:
    class Demo {
    private:
        int num;
    public:
        void set(int value) {
            num = value;
        }
        int get() {
            return num;
        }
    };
    
  4. What is the purpose of abstract classes in C++?
    Answer: Abstract classes are used to provide a blueprint for derived classes with at least one pure virtual function.
  5. Write an example of abstraction using a pure virtual function.
    Answer:
    class Shape {
    public:
        virtual void draw() = 0;
    };
    

Long Answer Questions

  1. Explain the concept of encapsulation with a detailed example in C++.
  2. Discuss abstraction in C++ and its advantages with an example program.
  3. Compare and contrast encapsulation and abstraction in object-oriented programming.

Post a Comment