Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> C Language

C++ Operator Overloading – A Practical Guide with Code Examples

What is Operator Overloading?

Using operator overloading in C++, you can assign multiple meanings to an operator within the same scope. The goal is to give a custom behavior to an operator for a user‑defined type.

With operator overloading, you can redefine most C++ operators and perform different operations using a single symbol.

In this tutorial you will learn:

Syntax

To overload a C++ operator, define a special member function inside a class as follows:

class class_name
{
    ...
    public:
        return_type operator symbol (argument(s))
        {
            ...
        }
    ...
};

The components are:

Example 1:

#include <iostream>
using namespace std;

class TestClass {
private:
    int count;
public:
    TestClass() : count(5) {}
    void operator --() {
        count = count - 3;
    }
    void Display() {
        cout << "Count: " << count;
    }
};

int main() {
    TestClass tc;
    --tc;
    tc.Display();
    return 0;
}

Output:

C++ Operator Overloading – A Practical Guide with Code Examples

Here is a screenshot of the code:

C++ Operator Overloading – A Practical Guide with Code Examples

Code Explanation:

  1. Include <iostream> to access input/output facilities.
  2. Use the std namespace for brevity.
  3. Define a class TestClass.
  4. Make count a private data member.
  5. Provide a public constructor that initializes count to 5.
  6. Overload the prefix -- operator to subtract 3 from count.
  7. Implement Display() to print the current value.
  8. In main(), create an instance, apply the overloaded operator, and display the result.

Different Approaches to Operator Overloading in C++

Overloading can be achieved through:

  1. Member functions
  2. Non‑member functions (friend or free functions)
  3. Friend functions (when private data access is needed)

Can All C++ Operators be Overloaded?

No. The following operators are not overloadable:

Things to Remember

  1. Overloading only affects user‑defined types; built‑in types retain their original behavior.
  2. The assignment (=) and address‑of (&) operators are automatically overloaded for classes that provide copy constructors or assignment operators.
  3. Operator precedence remains unchanged; you can use parentheses to enforce evaluation order.
  4. Three operators—::, ., ?:—are explicitly forbidden from overloading.

Rules for Operator Overloading

How to Overload an Operator

Example 1: Increment Operator

#include <iostream>
using namespace std;

class OperatorOverload {
private:
    int x;
public:
    OperatorOverload() : x(10) {}
    void operator ++() {
        x += 2;
    }
    void Print() {
        cout << "The Count is: " << x;
    }
};

int main() {
    OperatorOverload ov;
    ++ov;
    ov.Print();
    return 0;
}

Output:

C++ Operator Overloading – A Practical Guide with Code Examples

Screenshot of the code:

C++ Operator Overloading – A Practical Guide with Code Examples

Code Explanation:

  1. Include <iostream> and use the std namespace.
  2. Define class OperatorOverload with a private int x.
  3. Constructor initializes x to 10.
  4. Overload the prefix ++ operator to add 2 to x.
  5. Implement Print() to display the updated value.
  6. In main(), create an object, invoke the overloaded operator, and print the result.

Example 2: Complex Number Addition

#include <iostream>
using namespace std;

class TestClass {
private:
    int real, over;
public:
    TestClass(int rl = 0, int ov = 0) : real(rl), over(ov) {}

    TestClass operator + (TestClass const &obj) {
        TestClass result;
        result.real = real + obj.real;
        result.over = over + obj.over;
        return result;
    }
    void print() {
        cout << real << " + i" << over << endl;
    }
};

int main() {
    TestClass c1(9, 5), c2(4, 3);
    TestClass c3 = c1 + c2;
    c3.print();
}

Output:

C++ Operator Overloading – A Practical Guide with Code Examples

Screenshot of the code:

C++ Operator Overloading – A Practical Guide with Code Examples

Code Explanation:

  1. Include <iostream> and use the std namespace.
  2. Define TestClass with real and over components.
  3. Provide a constructor for initialization.
  4. Overload the + operator to perform complex‑number addition.
  5. Return a new TestClass instance containing the sum.
  6. Implement print() to display the complex number.
  7. In main(), create two instances, add them, and print the result.

Summary

C Language

  1. C++ Function Overloading: A Practical Guide
  2. Master C++ Operator Overloading: Practical Examples & Best Practices
  3. C++ do‑while Loop: Syntax, Practical Examples, and Nested Loops
  4. Mastering C++ Pointers: Concepts, Examples & Practical Applications
  5. Mastering C++ Char Data Types: Declaration, ASCII, and Conversion Techniques
  6. C++ Polymorphism Explained: Practical Examples & Key Concepts
  7. C++ Functions Explained with Practical Code Examples
  8. Python round() Function Explained with Practical Examples
  9. Mastering Python's map() Function: Syntax, Examples, and Best Practices
  10. Mastering C++ Overloading: Functions & Operators Explained