0% found this document useful (0 votes)
22 views5 pages

Unit 1 Deepseek

Object-Oriented Programming (OOP) is a paradigm that organizes software design around objects, which consist of data and behavior, promoting modular and reusable code. Key features of OOP include encapsulation, abstraction, inheritance, and polymorphism, while C++ enhances C by incorporating these OOP principles. The document also contrasts procedure-oriented programming with OOP, highlighting differences in approach, data security, reusability, and program structure.

Uploaded by

sahilkamboj12312
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views5 pages

Unit 1 Deepseek

Object-Oriented Programming (OOP) is a paradigm that organizes software design around objects, which consist of data and behavior, promoting modular and reusable code. Key features of OOP include encapsulation, abstraction, inheritance, and polymorphism, while C++ enhances C by incorporating these OOP principles. The document also contrasts procedure-oriented programming with OOP, highlighting differences in approach, data security, reusability, and program structure.

Uploaded by

sahilkamboj12312
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

### **Introduction to Object-Oriented Programming (OOP)**

Object-Oriented Programming (OOP) is a programming paradigm that organizes software design


around **objects** rather than functions and logic. An object is a self-contained unit that consists of
**data (attributes)** and **behavior (methods)**. OOP focuses on creating reusable, modular, and
scalable code.

---

### **Basic Features of OOP**

1. **Encapsulation**:

- Bundling data (attributes) and methods (functions) that operate on the data into a single unit
(object).

- Protects data from outside interference by restricting direct access (using access modifiers like
`private`, `public`, etc.).

2. **Abstraction**:

- Hiding complex implementation details and showing only the necessary features of an object.

- Example: A car’s steering wheel hides the complexity of the steering mechanism.

3. **Inheritance**:

- Allows a class to inherit properties and methods from another class.

- Promotes code reusability and hierarchical classification.

- Example: A `Dog` class can inherit from an `Animal` class.

4. **Polymorphism**:

- The ability of objects to take on multiple forms.

- Example: A function can behave differently depending on the object that calls it (e.g., method
overriding).

---

### **Basic Components of a C++ Program**

1. **Classes**:

- Blueprint for creating objects.

- Defines the structure (attributes and methods) of an object.


```cpp

class Dog {

public:

string name; // Attribute

void bark() { // Method

cout << name << " says Woof!" << endl;

};

```

2. **Objects**:

- Instances of a class.

- Example: `Dog myDog;` creates an object `myDog` of the `Dog` class.

3. **Functions/Methods**:

- Actions or behaviors that an object can perform.

4. **Variables/Attributes**:

- Data members that store the state of an object.

5. **Main Function**:

- The entry point of a C++ program.

- Example:

```cpp

int main() {

Dog myDog;

myDog.name = "Buddy";

myDog.bark(); // Output: Buddy says Woof!

return 0;

```

---

### **Program Structure in C++**

A typical C++ program has the following structure:


1. **Include Headers**:

- Use `#include` to include libraries.

- Example: `#include <iostream>` for input/output operations.

2. **Class Definition**:

- Define the class and its members.

3. **Main Function**:

- The starting point of execution.

4. **Compilation and Execution**:

- Write code in a `.cpp` file (e.g., `program.cpp`).

- Compile using a C++ compiler (e.g., `g++ program.cpp -o program`).

- Run the executable (e.g., `./program`).

---

### **Difference Between Procedure-Oriented (C) and Object-Oriented (C++) Languages**

| **Aspect** | **Procedure-Oriented (C)** | **Object-Oriented (C++)**


|

|---------------------------|----------------------------------------------------|-------------------------------------------------|

| **Approach** | Focuses on functions and procedures. | Focuses on objects and


their interactions. |

| **Data Security** | No data hiding; all data is accessible. | Data is secured using
encapsulation. |

| **Reusability** | Limited reusability of code. | High reusability through


inheritance. |

| **Program Structure** | Program is divided into functions. | Program is divided into


classes and objects. |

| **Example** | A program to add two numbers uses functions. | A program to model a


car uses classes. |

| **Complexity Management** | Difficult to manage for large programs. | Easier to manage


due to modularity. |

---

### **Example in C (Procedure-Oriented)**


```c

#include <stdio.h>

// Function to add two numbers

int add(int a, int b) {

return a + b;

int main() {

int result = add(5, 10);

printf("Result: %d\n", result); // Output: Result: 15

return 0;

```

---

### **Example in C++ (Object-Oriented)**

```cpp

#include <iostream>

using namespace std;

// Class to represent a calculator

class Calculator {

public:

int add(int a, int b) {

return a + b;

};

int main() {

Calculator calc;

int result = calc.add(5, 10);

cout << "Result: " << result << endl; // Output: Result: 15

return 0;

}
```

---

### **Key Takeaways**

- OOP is about organizing code using **objects**, making it modular and reusable.

- C++ extends C by adding OOP features like classes, objects, inheritance, and polymorphism.

- Procedure-oriented programming (C) focuses on functions, while OOP (C++) focuses on objects and
their interactions.

Let me know if you need further clarification! 😊

You might also like