GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||
GeezerVison is showing www.geeksforgeeks.org — Operator Overloading in C++ - GeeksforGeeks Operator Overloading in C++Last Updated : 31 Aug, 2026 Operator overloading allows existing C++ operators such as +, -, *, and [] to be given a specific meaning for user-defined types. It enables objects of classes or structures to be used with operators in a natural and readable way.
Example: + operator is overloaded to add two Number objects.
Output 15 Explanation: The + operator is overloaded as a member function of Number. When n1 + n2 is used, it calls n1.operator+(n2) and returns a new object containing their sum. Syntax of Operator OverloadingAn operator is overloaded by defining a function using the operator keyword followed by the operator symbol.
Types of Operator OverloadingOperators can be classified based on the number of operands they work with. 1. Unary Operator OverloadingUnary operators work with a single operand. Examples include ++, --, !, and unary -.
Output 6 Explanation: The prefix ++ operator is overloaded to increase the object's value by one. 2. Binary Operator OverloadingBinary operators work with two operands. Examples include +, -, *, /, and ==.
Try It Yourself Output 15 Explanation: The overloaded + operator takes two Number objects and returns a new object containing the sum of their values. Member and Non-Member Operator FunctionsAn operator can generally be overloaded using a member function or a non-member function.
A non-member operator function can be declared as a friend when it needs access to private or protected members. Operators That Can Be OverloadedMost C++ operators can be overloaded for user-defined types, including:
Operators That Cannot Be OverloadedSome operators have fixed language-level behavior and cannot be customized through operator overloading.
Reasons These Operators Cannot Be OverloadedThese operators are closely tied to fundamental C++ language operations and therefore have fixed semantics.
Rules of Operator OverloadingOperator overloading follows several restrictions defined by C++.
Operator Functions vs Normal Functions
Comment ExploreStandard Template Library(STL) Practice & Problems | ||||||||||||||||||||||||||||||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |