0% found this document useful (0 votes)
11 views9 pages

Operator Overloading

Operator overloading allows giving special meanings to operators in C++, with exceptions for certain operators like scope resolution and conditional operators. It involves creating a class, declaring an operator function, and defining it to implement operations, while adhering to rules such as not changing operator precedence or creating new operators. Examples of overloaded operators include arithmetic, comparison, and increment/decrement operators, demonstrated through classes like Vector and Counter.

Uploaded by

Pratit Raj Giri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views9 pages

Operator Overloading

Operator overloading allows giving special meanings to operators in C++, with exceptions for certain operators like scope resolution and conditional operators. It involves creating a class, declaring an operator function, and defining it to implement operations, while adhering to rules such as not changing operator precedence or creating new operators. Examples of overloaded operators include arithmetic, comparison, and increment/decrement operators, demonstrated through classes like Vector and Counter.

Uploaded by

Pratit Raj Giri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 9

Operator Overloading

Intro..
• Mechanism of giving special meaning to an
operator is known as operator overloading
• We can overload all C++ operators except:
Class member access operators (.,.*)
Scope resolution operator (::)
Size operator (sizeof)
Conditional operator (?:)
Defining operator overloading
• Use operator function
return-type classname :: operator(op-arglist)
{
Function body //task definition
}
• The overloading process involves:
1. Create a class that defines the data type that is to be used in the
overloading operation
2. Declare the operator function operator op() in the public part of the class. It
may be either a member function or a friend function
3. Define the operator function to implement the required operations
• Overloaded operator functions can be invoked as
op x or x op for unary operator interpreted as operator op(x) for friend functions
x op y for binary operators interpreted as x.operator op (y) for member function
operator op(x,y) for friend functions
Key Rules for Operator
Overloading
• You cannot change operator precedence
• You cannot change operator arity (number of
operands)
• You cannot create new operators
• Some operators cannot be overloaded
(::, .*, ., ?:)
• At least one operand must be a user-defined
type
Common Operators That Can Be
Overloaded
• Arithmetic: +, -, *, /, %
• Comparison: ==, !=, <, >, <=, >=
• Assignment: =
• Stream: <<, >>
• Increment/Decrement: ++, --
• Subscript: []
• Function call: ()
Overloading Binary Operator
class Vector {
private:
float x, y;
public:
Vector(float x, float y) : x(x), y(y) {}
Vector operator+(const Vector& other) {
return Vector(x + other.x, y + other.y);
}
void display() {
cout << "(" << x << ", " << y << ")";
}
};
int main() {
Vector v1(1.5, 2.5);
Vector v2(3.0, 4.0);
Vector v3 = v1 + v2; // Uses overloaded + operator
v3.display(); // Output: (4.5, 6.5)
}
Vector operator+(const Vector& other) {
return Vector(x + other.x, y + other.y); }

How it works:
•The + operator is overloaded as a member function
•Takes one parameter (other) which is the Vector to add
•Returns a new Vector with components added together
•When v1 + v2 is called, it's equivalent to v1.operator+(v2)
Key points:
•The left operand is the current object (this)
•The right operand is passed as parameter
•Returns a new object rather than modifying operands
Overloading Unary Operator
class Counter {
private:
int count;
public:
Counter(int c = 0) : count(c) {}
Counter& operator++() { // Prefix ++ overload
++count;
return *this;
}
Counter operator++(int) { // Postfix ++ overload (uses dummy int parameter)
Counter temp = *this;
++count;
return temp;
}
void display() { cout << count; }
};
int main() {
Counter c(5);
++c; // Prefix
c++; // Postfix
c.display(); // Output: 7
}
// Prefix // Postfix
Counter& operator++() { Counter operator++(int) {
++count; Counter temp = *this;
return *this; } ++count;
return temp; }
How it works:
•Prefix version (++obj) returns reference to modified object
•Postfix version (obj++) takes dummy int parameter
•Postfix returns copy of original value before increment
•Both modify the object's internal state
Key points:
•Prefix is more efficient (returns reference)
•Postfix must return by value
•Dummy int distinguishes postfix from prefix

You might also like