3.4 Features of Object-Oriented Programming
1. Operator Overloading
#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
3. Inheritance
Conclusion
Previous3.3 C++ Language Constructs with Objects and ClassesNext3.5 Pure Virtual Functions and File Handling
Last updated