Fundamentals 5 min read

Master C++ Operator Overloading: Make Your Classes Behave Like Built‑ins

This tutorial explains why and how to overload arithmetic and stream operators in C++, using a Complex class example to show member and friend function implementations, code snippets, and key concepts for making custom types as intuitive as built‑in types.

php Courses
php Courses
php Courses
Master C++ Operator Overloading: Make Your Classes Behave Like Built‑ins

Why Overload Operators?

Imagine you have a Complex class with real and imaginary parts and you want to use c1 + c2 just like built‑in types instead of calling c1.add(c2). Operator overloading lets custom types behave like primitives.

1. Overloading Arithmetic Operators (+, -)

We overload them with member functions.

#include <iostream>
using namespace std;

class Complex {
private:
    double real;
    double imag;
public:
    Complex(double r = 0, double i = 0) : real(r), imag(i) {}
    // overload +
    Complex operator+(const Complex& other) const {
        return Complex(real + other.real, imag + other.imag);
    }
    // overload -
    Complex operator-(const Complex& other) const {
        return Complex(real - other.real, imag - other.imag);
    }
};

int main() {
    Complex c1(1.0, 2.0);
    Complex c2(3.0, 4.0);
    Complex c3 = c1 + c2; // like 1+2
    Complex c4 = c1 - c2;
    return 0;
}
operator+

is the function name. const Complex& other represents the right‑hand operand.

The function returns a new object without modifying the originals.

2. Overloading Stream Operators (<<, >>)

Because cout << c1 has the object on the right, we need a non‑member (global) function, and it must be declared as a friend to access private members.

class Complex {
private:
    double real;
    double imag;
public:
    Complex(double r = 0, double i = 0) : real(r), imag(i) {}
    friend ostream& operator<<(ostream& os, const Complex& c);
    friend istream& operator>>(istream& is, Complex& c);
};

ostream& operator<<(ostream& os, const Complex& c) {
    os << "(" << c.real << " + " << c.imag << "i)";
    return os;
}
istream& operator>>(istream& is, Complex& c) {
    is >> c.real >> c.imag;
    return is;
}

int main() {
    Complex c1;
    cout << "Enter a complex number (real imag): ";
    cin >> c1; // uses overloaded >>
    cout << c1 << endl; // uses overloaded <<
    return 0;
}
friend

allows the global functions to access private members. ostream& and istream& represent output and input streams.

Returning the stream reference enables chaining like cout << c1 << c2.

Summary

Arithmetic operators (+, -, *, /) are overloaded with member functions.

Stream operators (<<, >>) are overloaded with global functions declared as friend.

Friends let external functions access a class’s private data.

Operator overloading makes code more intuitive; for example, c1 + c2 reads naturally compared to c1.add(c2), allowing custom classes to be used as conveniently as built‑in types.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

CTutorialoperator overloadingcomplex numbers
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.