...
Info |
---|
In C++, operator overloading is a feature that allows operators, such as |
...
Code Block | ||
---|---|---|
| ||
// main.cpp // OperatorOverload // Text examnple modified by Kevin Roark on 3/31/23. // Please note that the class Complex { private: double real; double imag; has been included on the main file - this is done for //. For simplicity to understand the code #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); // 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 friendTimeHrMn ostream& operator<<(ostream& out, const Complex& cand 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 + c2; time2; // 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 example, we have defined a Complex
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 console.
...
This C++ code demonstrates the usage of operator overloading in the context of a TimeHrMn class, which represents time with hours and minutes. The code includes the class declaration and implementation within the main program file for simplicity.
Here's a breakdown of the code:
The code includes the necessary header files:
#include <iostream>
: This header file provides input/output operations, such ascout
for outputting data to the console.using namespace std;
: This line is used to avoid writingstd::
before each standard library function. It allows the code to directly use functions likecout
without thestd::
prefix.
The code defines the TimeHrMn class, which represents time with hours and minutes.
The class has two private member variables:
hours
andminutes
.The class has a public constructor
TimeHrMn(int timeHours = 0, int timeMinutes = 0)
that initializes the time with provided hours and minutes.The class has a public member function
Print()
that prints the time (hours and minutes) to the console.The class overloads the
+
operator for two different scenarios:The overloaded
+
operator for TimeHrMn operands is defined asTimeHrMn operator+(TimeHrMn)
. It adds the hours and minutes of two TimeHrMn objects and returns a new TimeHrMn object.The overloaded
+
operator for TimeHrMn and int operands is defined asTimeHrMn operator+(int)
. It adds an integer value to the hours of a TimeHrMn object and returns a new TimeHrMn object.
In the
main()
function, several TimeHrMn objects are created:time1
,time2
, andsumTime
.time1
is initialized with 3 hours and 22 minutes, andtime2
is initialized with 2 hours and 50 minutes.The
+
operator is used to addtime1
andtime2
, and the result is assigned tosumTime
. This invokes the overloaded+
operator for TimeHrMn operands.The
Print()
member function is called onsumTime
to display the result.Next, the
+
operator is used to add 10 hours totime1
, and the result is assigned tosumTime
. This invokes the overloaded+
operator for TimeHrMn and int operands.The
Print()
member function is called onsumTime
again to display the updated result.
Overall, this code demonstrates how to overload the +
operator to perform addition with TimeHrMn objects and between TimeHrMn objects and integers. It showcases how operator overloading can be used to make the code more expressive and intuitive when working with custom classes.
...
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.