Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP C C++ C# AWS W3.CSS HOW TO BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING INTRO TO HTML & CSS BASH RUST TOOLS

C++ Tutorial

C++ HOME C++ Intro C++ Get Started C++ Syntax C++ Output C++ Comments C++ Variables C++ User Input C++ Data Types C++ Operators C++ Type Conversion C++ Strings C++ Math C++ Booleans C++ If...Else C++ Switch C++ While Loop C++ For Loop C++ Break/Continue C++ Arrays C++ Structures C++ Enums C++ References C++ Pointers C++ Memory Mgmt

C++ Functions

C++ Functions C++ Function Parameters C++ Function Overloading C++ Scope C++ Recursion C++ Lambda

C++ Classes

C++ OOP C++ Classes/Objects C++ Class Methods C++ Constructors C++ Destructors C++ The this Pointer C++ Access Specifiers C++ Encapsulation C++ Friend Functions C++ Inheritance C++ Polymorphism C++ Operator Overload C++ Templates

C++ Files

C++ Files

C++ Date

C++ Date

C++ Errors

C++ Errors C++ Debugging C++ Exceptions C++ Input Validation

C++ Problem Solving

C++ Problem Solving C++ Build the Logic C++ Test Cases

C++ Data Structures

C++ Data Structures & STL C++ Vectors C++ List C++ Stacks C++ Queues C++ Deque C++ Sets C++ Maps C++ Unordered Map C++ Unordered Set C++ Iterators C++ Algorithms

C++ Namespaces

C++ Namespaces

C++ Projects

C++ Projects

C++ Cert

C++ Certificate

C++ How To

C++ How Tos

C++ Examples

C++ Examples C++ Real-Life Examples C++ Compiler C++ Exercises C++ Quiz C++ Code Challenges C++ Practice Problems C++ Syllabus C++ Study Plan

C++ Reference

C++ Reference C++ Keywords C++ <algorithm> C++ <cctype> C++ <cmath> C++ <cstdlib> C++ <cstring> C++ <ctime> C++ <deque> C++ <fstream> C++ <iomanip> C++ <iostream> C++ <list> C++ <map> C++ <memory> C++ <queue> C++ <random> C++ <set> C++ <sstream> C++ <stack> C++ <stdexcept> C++ <string> C++ <unordered_map> C++ <unordered_set> C++ <vector>


C++ Operator Overloading


Share

Operator Overloading

You already know that the + operator does different things for different types. It can be used to add two numbers, and to join two strings.

But for your own classes, C++ does not know what + should do. With operator overloading, you decide what an operator does for objects of your class.

To overload an operator, create a method called operator followed by +, like operator+:

Example

class Point {
  public:
    int x, y;

    Point(int x, int y) {
      this->x = x;
      this->y = y;
    }

    Point operator+(Point other) {
      return Point(x + other.x, y + other.y);
    }
};

int main() {
  Point p1(1, 2);
  Point p2(3, 4);
  Point p3 = p1 + p2;

  cout << p3.x << ", " << p3.y;
  return 0;
}

Try it Yourself »

p1 + p2 calls p1.operator+(p2). The method adds the x values and the y values, and returns a new Point.


Compare Objects with ==

In the same way, you can overload == to decide when two objects are equal. The method returns a bool:

Example

class Point {
  public:
    int x, y;

    Point(int x, int y) {
      this->x = x;
      this->y = y;
    }

    bool operator==(Point other) {
      return x == other.x && y == other.y;
    }
};

Point p1(1, 2);
Point p2(1, 2);

if (p1 == p2) {
  cout << "The points are equal";
} else {
  cout << "The points are not equal";
}

Try it Yourself »



Print Objects with <<

You can also make cout print your objects, by overloading the << operator. This is done with a function outside the class, because the left side of << is cout, not your object:

Example

Using the Point class from the first example:

ostream& operator<<(ostream& out, Point p) {
  out << "(" << p.x << ", " << p.y << ")";
  return out;
}

Point p1(1, 2);
Point p2(3, 4);

cout << p1 + p2;

Try it Yourself »

The function gets the output stream (out) and the point, writes the point to the stream, and returns the stream. Returning it lets you keep using << after the point, like cout << p << "\n";.

Tip: Only overload operators when the meaning is obvious, like + for adding two points. If it is not clear what an operator does, use a normal method with a good name instead.




Free account

Track your progress

XP earned 0 Day streak 0 Spaces 0 League —

New

Learn in Adventure Mode W3Schools Adventure App

Coding fundamentals as bite-sized lessons and challenges.

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.