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.
#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.
#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.
#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.
#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.
#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.
#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.