Day 48 – Object-Oriented Programming (OOP) Concepts in C++
Class 11 Computer Science
What is Object-Oriented Programming? (Object-Oriented Programming क्या है?)
Object-Oriented Programming (OOP) is a programming paradigm based on the concept of "objects," which contain data in the form of fields (attributes) and code in the form of procedures (methods). It focuses on data abstraction and encapsulation.
Object-Oriented Programming (OOP) एक प्रोग्रामिंग दृष्टिकोण है जो "ऑब्जेक्ट्स" की अवधारणा पर आधारित है। इसमें डेटा को फील्ड्स (एट्रिब्यूट्स) और कोड को प्रोसीजर्स (मेथड्स) के रूप में संग्रहीत किया जाता है। यह डेटा एब्स्ट्रैक्शन और एन्कैप्सुलेशन पर केंद्रित है।
Principles of OOP (OOP के सिद्धांत)
- Encapsulation (एन्कैप्सुलेशन): Binding data and functions into a single unit (class).
डेटा और फ़ंक्शन को एक इकाई (क्लास) में बाँधना। - Inheritance (इनहेरिटेंस): Acquiring properties and behaviors from a parent class.
पैरेंट क्लास से गुण और व्यवहार प्राप्त करना। - Polymorphism (पॉलीमॉर्फिज्म): The ability to use a single interface for different data types.
विभिन्न डेटा प्रकारों के लिए एक ही इंटरफ़ेस का उपयोग करने की क्षमता। - Abstraction (एब्स्ट्रैक्शन): Hiding implementation details and showing only the essential features.
कार्यान्वयन विवरण छिपाना और केवल आवश्यक विशेषताएँ दिखाना।
Advantages of OOP (OOP के लाभ)
- Enhances code reusability through inheritance.
इनहेरिटेंस के माध्यम से कोड पुन: उपयोग में सुधार करता है। - Makes programs modular and easier to maintain.
प्रोग्राम्स को मॉड्यूलर और रखरखाव में आसान बनाता है। - Promotes data security through encapsulation.
एन्कैप्सुलेशन के माध्यम से डेटा सुरक्षा को बढ़ावा देता है।
Basic Structure of OOP in C++ (C++ में OOP की बुनियादी संरचना)
Class: Blueprint for creating objects.
Object: Instance of a class.
Methods: Functions that define the behavior of an object.
Attributes: Data members of a class.
क्लास: ऑब्जेक्ट्स बनाने के लिए ब्लूप्रिंट।
ऑब्जेक्ट: एक क्लास का इंस्टेंस।
मेथड्स: ऑब्जेक्ट का व्यवहार परिभाषित करने वाले फ़ंक्शन्स।
एट्रिब्यूट्स: एक क्लास के डेटा मेंबर्स।
Example of OOP in C++ (C++ में OOP का उदाहरण)
Example 1: Class and Object
#include <iostream>
using namespace std;
class Car {
public:
string brand;
string model;
int year;
void displayDetails() {
cout << "Brand: " << brand << endl;
cout << "Model: " << model << endl;
cout << "Year: " << year << endl;
}
};
int main() {
Car car1; // Object of class Car
car1.brand = "Toyota";
car1.model = "Corolla";
car1.year = 2020;
car1.displayDetails();
return 0;
}
Output:
Brand: Toyota Model: Corolla Year: 2020
Example 2: Inheritance
#include <iostream>
using namespace std;
class Animal {
public:
void eat() {
cout << "This animal eats food." << endl;
}
};
class Dog : public Animal {
public:
void bark() {
cout << "This dog barks." << endl;
}
};
int main() {
Dog dog1;
dog1.eat(); // Inherited function
dog1.bark(); // Own function
return 0;
}
Output:
This animal eats food. This dog barks.
Practice Questions
Multiple Choice Questions (MCQs)
- Which is not a principle of OOP?
(a) Encapsulation | (b) Inheritance | (c) Compilation | (d) Polymorphism - What does a class represent in OOP?
(a) An object | (b) A blueprint for objects | (c) A method | (d) None - What is the primary goal of encapsulation?
(a) Code reusability | (b) Data security | (c) Overloading | (d) None - Which keyword is used for inheritance in C++?
(a) inherit | (b) extends | (c) public | (d) None - What is polymorphism in C++?
(a) Many classes | (b) Many forms | (c) Overriding | (d) None - Which principle hides implementation details?
(a) Inheritance | (b) Abstraction | (c) Polymorphism | (d) None - What is the instance of a class called?
(a) Object | (b) Variable | (c) Method | (d) None - Which operator is used to access class members?
(a) . (dot) | (b) -> (arrow) | (c) * (pointer) | (d) None - What is the output of this code?
class A { public: void display() { cout << "Class A" << endl; } }; class B : public A { public: void display() { cout << "Class B" << endl; } }; int main() { B obj; obj.display(); return 0; }(a) Class A | (b) Class B | (c) Error | (d) None - What is the default access specifier in a class?
(a) Public | (b) Private | (c) Protected | (d) None
Answers to MCQs:
1: (c), 2: (b), 3: (b), 4: (c), 5: (b), 6: (b), 7: (a), 8: (a), 9: (b), 10: (b)
Short Answer Questions
- What are the four principles of OOP?
Answer: Encapsulation, Inheritance, Polymorphism, and Abstraction. - Write a program to demonstrate inheritance in C++.
Answer:class Parent { public: void greet() { cout << "Hello from Parent!" << endl; } }; class Child : public Parent { public: void display() { cout << "Hello from Child!" << endl; } }; - What is the importance of encapsulation in OOP?
Answer: Encapsulation provides data security by binding data and methods into a single unit. - Explain the difference between a class and an object.
Answer: A class is a blueprint, while an object is an instance of the class. - What is abstraction in OOP?
Answer: Abstraction hides implementation details and shows only essential features.
Long Answer Questions
- Explain Object-Oriented Programming principles in detail with examples.
- Write a program to demonstrate polymorphism using function overriding in C++.
- Discuss the advantages and disadvantages of using OOP in programming.
Post a Comment