Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 6 Current »

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.

//  main.cpp
//  OperatorOverload
//  Text examnple modified by Kevin Roark on 3/31/23.
//  Please note that the class 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:
   TimeHrMn(int timeHours = 0, int timeMinutes = 0); // 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;
   
   timeTotal.hours = hours + pHours;
   timeTotal.minutes = minutes; // Minutes stays the same
   
   return 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 time1(3, 22);
   TimeHrMn time2(2, 50);
   TimeHrMn sumTime;
   
   sumTime = time1 + time2; // Invokes operator+ for TimeHrMn operands
   sumTime.Print();
   
   sumTime = time1 + 10;    // Invokes operator+ for TimeHrMn and int operands
   sumTime.Print();
   return 0;
}

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 as cout for outputting data to the console.

    • using namespace std;: This line is used to avoid writing std:: before each standard library function. It allows the code to directly use functions like cout without the std:: prefix.

  • The code defines the TimeHrMn class, which represents time with hours and minutes.

    • The class has two private member variables: hours and minutes.

    • 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 as TimeHrMn 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 as TimeHrMn 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, and sumTime.

  • time1 is initialized with 3 hours and 22 minutes, and time2 is initialized with 2 hours and 50 minutes.

  • The + operator is used to add time1 and time2, and the result is assigned to sumTime. This invokes the overloaded + operator for TimeHrMn operands.

  • The Print() member function is called on sumTime to display the result.

  • Next, the + operator is used to add 10 hours to time1, and the result is assigned to sumTime. This invokes the overloaded + operator for TimeHrMn and int operands.

  • The Print() member function is called on sumTime 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.

  • No labels