n In C++, operator overloading is a feature that allows operators, such as +
, -
, *
, /
, and <<
, to be overloaded with custom behavior for user-defined types. Operator overloading allows you to use the same syntax with your own classes as you would with built-in types, making your code more natural and intuitive.Here's an example of operator overloading:
Code Block | ||
---|---|---|
| ||
class Complex { private: double real; double imag;// // main.cpp // OperatorOverload // Text examnple modified by Kevin Roark on 3/31/23. // #include <iostream> using namespace std; #include <iostream> using namespace std; // TimeHrMn class represents time with hours and minutes class TimeHrMn { public: ComplexTimeHrMn(doubleint rtimeHours = 0, doubleint itimeMinutes = 0) : real(r), imag(i) {} Complex operator+(const Complex& other) const { return Complex(real + other.real, imag + other.imag); } friend ostream& operator<<(ostream& out, const Complex& c; // constructor void Print() const; // member function to print the time TimeHrMn operator+(TimeHrMn); // overloaded + operator for TimeHrMn operands TimeHrMn operator+(int); // overloaded + operator for TimeHrMn and int operands private: int hours; int minutes; }; // Overloaded + operator for TimeHrMn operands TimeHrMn TimeHrMn::operator+(TimeHrMn pTime) { TimeHrMn timeTotal; timeTotal.hours = hours + pTime.hours; timeTotal.minutes = minutes + pTime.minutes; return timeTotal; } // Overloaded + operator for TimeHrMn and int operands TimeHrMn TimeHrMn::operator+(int pHours) { TimeHrMn timeTotal; out << "(" << c.real << ", " << c.imag << ")"; timeTotal.hours = hours + pHours; timeTotal.minutes = minutes; // Minutes stays the same return out; timeTotal; } // Constructor that initializes the time with hours and minutes TimeHrMn::TimeHrMn(int pHours, int pMinutes) { hours = pHours; minutes = pMinutes; return; } // Member function to print the time void TimeHrMn::Print() const { cout << "H:" << hours << ", " };<< "M:" << minutes << endl; } int main() { TimeHrMn Complex c1time1(13, 222); TimeHrMn Complex c2time2(32, 4);50); TimeHrMn sumTime; Complex c3sumTime = c1time1 + c2time2; // Invokes operator+ for TimeHrMn operands sumTime.Print(); cout << c1sumTime <<= "time1 + "10; << c2 << "// =Invokes "operator+ <<for c3TimeHrMn <<and endl;int operands sumTime.Print(); return 0; } |
In this examplecode, we have defined define a Complex
TimeHrMn
class that represents complex numbers. We have overloaded the +
operator to allow adding two complex numbers together, and we have overloaded the <<
operator to allow printing complex numbers to the consoletime with hours and minutes. The class has a constructor that initializes the time with hours and minutes, a member function Print
that prints the time, and two overloaded +
operators that allow you to add two TimeHrMn
objects together, or add an int
value to a TimeHrMn
object.
The implementation of the operator+
functions simply adds the hours and minutes of the two operands and returns a new TimeHrMn
object with the resulting values.
In the main
function, we create two complex numbers c1
and c2
and add them together using the +
operator. We then print the result using the overloaded <<
operator. TimeHrMn
objects time1
and time2
, and use the overloaded +
operators to add them together or add an int
value to one of them. We also demonstrate the use of the built-in addition operator with an int
value
Operator overloading is useful when you want to make your code more expressive and natural by allowing operators to work with your own classes. However, it should be used with care, as overloading operators can sometimes make your code less readable and more error-prone. When overloading operators, you should follow the conventions and semantics of the built-in operators to avoid confusing users of your code.