C++ Tutorial
Class and Object
Reference
Inheritance and Derivation
Polymorphism and Virtual Functions
Operator Overloading
Template
Exception
Object Oriented Advanced
Input/Output Stream
File Operations
In this tutorial, we will learn how to overload math operators for a custom class in C++. Overloading math operators allows you to use these operators naturally and intuitively with instances of your custom classes. We will demonstrate overloading math operators using a simple Fraction
class as an example.
Fraction
class:First, let's create a basic Fraction
class with a constructor and a function to display the fraction:
#include <iostream> class Fraction { int numerator; int denominator; public: // Constructor Fraction(int n, int d) : numerator(n), denominator(d) {} // Function to display the fraction void display() const { std::cout << numerator << "/" << denominator; } };
+
):To overload the addition operator for the Fraction
class, define a member function with one argument representing the right operand. The result should be a new Fraction
object representing the sum of the two fractions:
Fraction operator+(const Fraction& rhs) const { int n = numerator * rhs.denominator + rhs.numerator * denominator; int d = denominator * rhs.denominator; return Fraction(n, d); }
-
):To overload the subtraction operator for the Fraction
class, define a member function with one argument representing the right operand. The result should be a new Fraction
object representing the difference between the two fractions:
Fraction operator-(const Fraction& rhs) const { int n = numerator * rhs.denominator - rhs.numerator * denominator; int d = denominator * rhs.denominator; return Fraction(n, d); }
*
):To overload the multiplication operator for the Fraction
class, define a member function with one argument representing the right operand. The result should be a new Fraction
object representing the product of the two fractions:
Fraction operator*(const Fraction& rhs) const { int n = numerator * rhs.numerator; int d = denominator * rhs.denominator; return Fraction(n, d); }
/
):To overload the division operator for the Fraction
class, define a member function with one argument representing the right operand. The result should be a new Fraction
object representing the quotient of the two fractions:
Fraction operator/(const Fraction& rhs) const { int n = numerator * rhs.denominator; int d = denominator * rhs.numerator; return Fraction(n, d); }
Here's the complete Fraction
class with overloaded math operators:
#include <iostream> class Fraction { int numerator; int denominator; public: // Constructor Fraction(int n, int d) : numerator(n), denominator(d) {} // Overloading the addition operator Fraction operator+(const Fraction& rhs) const { int n = numerator * rhs.denominator + rhs.numerator * denominator; int d = denominator * rhs.denominator; return Fraction(n, d); } // Overloading the subtraction operator Fraction operator-(const Fraction& rhs) const { int n = numerator * rhs.denominator - rhs.numerator * denominator; int d = denominator * rhs.denominator; return Fraction(n, d); } // Overloading the multiplication operator Fraction operator*(const Fraction& rhs) const { int n = numerator * rhs.numerator; int d = denominator * rhs.denominator; return Fraction(n, d); }
Operator overloading for addition, subtraction, multiplication, and division in C++:
#include <iostream> class Complex { private: double real, imag; public: Complex(double r, double i) : real(r), imag(i) {} // Overloading arithmetic operators Complex operator+(const Complex& other) const { return Complex(real + other.real, imag + other.imag); } Complex operator-(const Complex& other) const { return Complex(real - other.real, imag - other.imag); } Complex operator*(const Complex& other) const { return Complex(real * other.real - imag * other.imag, real * other.imag + imag * other.real); } Complex operator/(const Complex& other) const { double denominator = other.real * other.real + other.imag * other.imag; return Complex((real * other.real + imag * other.imag) / denominator, (imag * other.real - real * other.imag) / denominator); } // Display function void display() const { std::cout << real << " + " << imag << "i" << std::endl; } }; int main() { Complex a(1.0, 2.0); Complex b(2.0, 3.0); // Using overloaded arithmetic operators Complex sum = a + b; Complex difference = a - b; Complex product = a * b; Complex quotient = a / b; // Displaying results std::cout << "Sum: "; sum.display(); std::cout << "Difference: "; difference.display(); std::cout << "Product: "; product.display(); std::cout << "Quotient: "; quotient.display(); return 0; }