Object Orient
Programming
Lec -11+12+13
Base Education System, Wah Cantt - By: Sir Shahnawaz
Topics:
• Operator Overloading,
• Overloaded Operators In C++ Standard Library
• Overloading Binary Operators
• Overloading Unary Operators
• Overloading ++ Operator
• Overloading -- Operator
Base Education System, Wah Cantt - By: Sir Shahnawaz
Introduction: What is Operator
Overloading?
• Imagine a word like "bank." It can mean a financial institution OR the
side of a river. You understand which "bank" it is based on the
context.
• Same with Operators: In C++, operators like +,-,*,/ normally work with
basic types (like int, float, double)
• 5+3 gives 8 (Addition)
• 3.5 – 1.2 gives 2.3 (Subtraction)
• The Problem: What if you have your own custom data type, like a
Point (with x and y coordinates) How would you "add" two Points
objects?
Base Education System, Wah Cantt - By: Sir Shahnawaz
Introduction: What is Operator
Overloading?
• Example:
Point A(1, 2);
Point B(3, 4);
Point C = A + B;
• What does this even mean? C++ doesn't know by default!
• The Solution: Operator Overloading! It's like teaching C++ how to
make its existing operators (+,-,++ etc) work with your custom data
types. You're giving the operator a new "meaning" for your specific
class.
Base Education System, Wah Cantt - By: Sir Shahnawaz
Overloaded Operators in C++
Standard Library
• The C++ standard library already uses operator overloading
extensively.
• Example 1: String Concatenation
• When you do
string s1 = "Hello“, s2 = "World";
string s3 = s1 + s2;
• The + operator, which normally adds numbers, has been "overloaded"
by the string class to mean "concatenate" (join) strings.
Base Education System, Wah Cantt - By: Sir Shahnawaz
Overloaded Operators in C++
Standard Library
• Example 2: Input/Output Streams
• Cout<<“Hello”;
• The << operator (left shift for numbers) is overloaded for cout to
mean "send this data to the console."
• Cin>>myVariable;
• The >> operator (right shift for numbers) is overloaded for cin to
mean "read data from the console into this variable.“
Base Education System, Wah Cantt - By: Sir Shahnawaz
Overloading Binary Operators
• What are Binary Operators? Operators that work with two operands
(things they operate on).
• Examples: +, -, *, /, ==, !=, >, < etc.
• How to Overload Them: You define a special member function inside
your class (or sometimes a non-member function) with the keyword
Operator followed by the operator symbol.
• Example: Overloading + for a Time class
• In next slide
Base Education System, Wah Cantt - By: Sir Shahnawaz
How to overload them:
•You declare a special function inside your class using the operator keyword
followed by the symbol of the operator you want to overload.
•This function takes one argument (the right-hand side operand).
The left-hand side is the object calling the function.
Base Education System, Wah Cantt - By: Sir Shahnawaz
Let's define a simple Time class
#include <iostream> Time operator+(Time otherTime) {
Time newTime; int main() {
using namespace std; Time duration1(1, 45);
[Link] = hours + [Link]; Time duration2(0, 30);
class Time { [Link] = minutes + [Link];
public: cout << "Duration 1: ";
int hours; if ([Link] >= 60) { [Link]();
int minutes; [Link] += [Link] / 60
Time(int h = 0, int m = 0) { [Link] %= 60; cout << "Duration 2: ";
hours = h; } [Link]();
minutes = m; return newTime;
if (minutes >= 60) { } Time totalDuration = duration1 +
hours += minutes / 60; void show() { duration2;
minutes %= 60; cout << hours << "h " << minutes << "m“;
} } cout << "Total Duration: ";
} }; [Link]();
return 0;
}
Base Education System, Wah Cantt - By: Sir Shahnawaz
Explanation
Base Education System, Wah Cantt - By: Sir Shahnawaz
Overloading Unary Operators
• What are Unary Operators? Operators that work with one operand.
• Examples: !A (logical NOT), -A (unary minus, negating a number), ++A
(increment), --A (decrement).
• Examples: - (unary minus, like -5), ++ (increment), -- (decrement).
• How to overload them:
• You declare a special function inside your class using operator
keyword followed by the symbol.
• This function takes no arguments because it only operates on the
object itself.
Base Education System, Wah Cantt - By: Sir Shahnawaz
Overloading Unary Operators
class MyClass {
public:
// ... data members ...
// Function to overload the unary '-' operator
MyClass operator-() {
MyClass result;
// Logic to negate or invert 'this' object
return result; // Return a new MyClass object (or sometimes modify 'this' and return *this)
}
};
Base Education System, Wah Cantt - By: Sir Shahnawaz
Overloading Unary Operators
Base Education System, Wah Cantt - By: Sir Shahnawaz
Overloading ++ Operator
• The ++ (increment) operator increases a value by one.
• How to overload ++?
• A member function named operator++()
• Takes no arguments.
Base Education System, Wah Cantt - By: Sir Shahnawaz
Overloading ++ (Increment)
Operator
Base Education System, Wah Cantt - By: Sir Shahnawaz