# 3.4 Features of Object-Oriented Programming

Object-Oriented Programming (OOP) provides powerful features to enhance the modularity, reusability, and functionality of code. This section focuses on **operator overloading**, **data conversion**, and **inheritance** (single, multiple, multilevel, hybrid).

***

### **1. Operator Overloading**

Operator overloading allows the redefinition of operators for user-defined types (e.g., classes). It helps to perform operations on objects intuitively, similar to primitive data types.

**Example**: Overloading the `+` operator for a class representing complex numbers.

```cpp
#include <iostream>
using namespace std;

class Complex {
    int real, imag;
public:
    Complex(int r = 0, int i = 0) : real(r), imag(i) {}

    // Overload the '+' operator
    Complex operator+(const Complex& obj) {
        return Complex(real + obj.real, imag + obj.imag);
    }

    void display() {
        cout << real << " + " << imag << "i" << endl;
    }
};

int main() {
    Complex c1(3, 4), c2(1, 2);
    Complex c3 = c1 + c2; // Using overloaded '+' operator
    c3.display(); // Output: 4 + 6i
    return 0;
}
```

***

### **2. Data Conversion**

Data conversion refers to the process of converting one data type to another. This can involve user-defined types and is achieved by overloading type-casting operators.

**Example**: Converting an object into an integer.

```cpp
#include <iostream>
using namespace std;

class Distance {
    int meters;
public:
    Distance(int m) : meters(m) {}

    // Overload type-casting operator
    operator int() {
        return meters;
    }
};

int main() {
    Distance d(10);
    int meters = d; // Implicit conversion using overloaded operator
    cout << "Distance in meters: " << meters << endl; // Output: Distance in meters: 10
    return 0;
}
```

***

### **3. Inheritance**

Inheritance allows one class to derive properties and methods from another, promoting **code reuse** and a hierarchical structure. There are several types of inheritance in C++.

***

**a. Single Inheritance**\
One class inherits from another.

```cpp
#include <iostream>
using namespace std;

class Parent {
public:
    void show() {
        cout << "This is the Parent class." << endl;
    }
};

class Child : public Parent { // Single inheritance
};

int main() {
    Child obj;
    obj.show(); // Output: This is the Parent class.
    return 0;
}
```

***

**b. Multiple Inheritance**\
A class inherits from more than one base class.

```cpp
#include <iostream>
using namespace std;

class ClassA {
public:
    void displayA() {
        cout << "Class A" << endl;
    }
};

class ClassB {
public:
    void displayB() {
        cout << "Class B" << endl;
    }
};

class ClassC : public ClassA, public ClassB { // Multiple inheritance
};

int main() {
    ClassC obj;
    obj.displayA(); // Output: Class A
    obj.displayB(); // Output: Class B
    return 0;
}
```

***

**c. Multilevel Inheritance**\
A class inherits from another class, which in turn inherits from another class.

```cpp
#include <iostream>
using namespace std;

class Grandparent {
public:
    void displayGrandparent() {
        cout << "Grandparent class" << endl;
    }
};

class Parent : public Grandparent {
public:
    void displayParent() {
        cout << "Parent class" << endl;
    }
};

class Child : public Parent { // Multilevel inheritance
};

int main() {
    Child obj;
    obj.displayGrandparent(); // Output: Grandparent class
    obj.displayParent();      // Output: Parent class
    return 0;
}
```

***

**d. Hybrid Inheritance**\
A combination of two or more types of inheritance (e.g., single and multiple). This often involves using a **virtual base class** to resolve ambiguity.

```cpp
#include <iostream>
using namespace std;

class Base {
public:
    void display() {
        cout << "Base class" << endl;
    }
};

class ClassA : virtual public Base {};
class ClassB : virtual public Base {};
class Derived : public ClassA, public ClassB {};

int main() {
    Derived obj;
    obj.display(); // Output: Base class
    return 0;
}
```

***

### Conclusion

* **Operator Overloading**: Allows custom behavior for operators when used with objects.
* **Data Conversion**: Enables implicit or explicit conversion between objects and primitive types.
* **Inheritance**: Provides different mechanisms (single, multiple, multilevel, hybrid) to reuse and extend functionality.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://nec-license.gitbook.io/books/3.-programming-language-and-its-applications/3.4-features-of-object-oriented-programming.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
