PPT9
PPT9
Introduction
• Operator overloading
➢ Enabling C++’s operators to work with class objects
➢ Using traditional operators with user-defined objects
➢ Examples of already overloaded operators
• Operator << is both the stream-insertion operator and the
bitwise left-shift operator
• Operator >> is both the stream-extraction operator and the
bitwise right-shift operator
➢ Compiler generates the appropriate code based on the
manner in which the operator is used.
Introduction (Cont…)
• Overloading an operator
– Write function definition as normal
– Functionname is keywordoperator followed bythe symbol for the
operator being overloaded
– For e.g., operator+ used to overload the addition operator
(+)
• Using operators
– To use an operator on a class object, it must be overloaded unless the
assignment operator (=) or the address operator (&)
• Assignment operator by default performs memberwise assignment
• Address operator (&) by default returns the address of an object
Restrictions on Operator Overloading
• Overloading restrictions
– Precedence of an operator cannot be changed
– Associativity of an operator cannot be changed
– number of operands cannot be changed
• Unary operators remain unary, and binary operators remain binary
• Operators &, *, + and - each have unary and binary versions
• Unary and binary versions can be overloaded separately
• No new operators can be created
– Use only existing operators
• No overloading operators for built-in types
– Cannot change how two integers are added
– Produces a syntax error
What are Operators in C++?
• Arithmetic Operators: +, −, /, ∗, %
• Assignment Operators: +=,+=, −=,−=, /=,/=, ∗=,∗=, %=%=
• Relational Operators: ==,==, !=,!=, >=,>=, <=,<=,etc.
• Logical Operators: &&,&&, ∣∣,∣∣, !!
• Bitwise Operators: &,&, ∣,∣, >>, <<etc.
• Other Operators: sizeof, ?:,etc.
Restrictions on Operator Overloading
• All the C++ operators can be overloaded except the following:
• Scope Resolution Operator (::)
• Conditional Operator (? :)
• Size Operator (sizeof)
• Class Member Access Operators (. and .*)
Operator Functions as Class Members vs. as friend Functions
Note: If left operand is of a different type than class, operator function must be a non-member function
C++ Syntax for Operator Overloading
• class ClassName{
•
• private:
• ...............
•
public:
• ...............
•
• <ReturnType> operator <operator> (arguments){
• ...............
• ...............
• }
• ...............
• };
Creating a Member Operator Function
int main()
{
Distance D1(11, 10), D2(-5, 11);
-D1; // activates operator-() function
D1.displayDistance(); // D1display Output:
-D2; // activates operator-() function F: -11 I:-10 F: 5 I:-11
D2.displayDistance(); // display D2
return 0;
}
Binary Operators
• The binary operators take two arguments.
• Given an object called obj, the expression obj[3] translates into this
call to the operator[ ]( ) function: obj.operator[](3)
Example 1
#include <iostream> int operator[](int i)
using namespace std; { return a[i];
class atype { }
int a[3]; };
public: int main()
atype(int i, int j, int k) { {
a[0] = i; atype ob(1, 2, 3);
a[1] = j; cout << ob[1]; // displays 2
a[2] = k; return 0;
} } Output:
2
Example 2
#include <iostream> int main()
using namespace std; {
class atype { atype ob(1, 2, 3);
int a[3]; cout << ob[1]; // displays 2
public: cout << " ";
atype(int i, int j, int k) { ob[1] = 25; // [] on left of =
a[0] = i; cout << ob[1]; // now displays 25
a[1] = j; return 0;
a[2] = k; }
}
Output:
int &operator[](int i) {
return a[i]; 2 25
}
};
Overloading ()
• When we overload the ( ) function call operator, we are
not creating a new way to call a function.
Example:
double operator()(int a, float f, char *s);
and an object O of its class, then the statement
O(10, 23.34, "hi");
translates into this call to the operator( ) function.
O.operator()(10, 23.34, "hi");
Example
#include <iostream>
using namespace std; // Overload + for loc
class loc { loc loc::operator+(loc op2)
int longitude, latitude; {
public: loc temp;
loc() {} temp.longitude = op2.longitude + longitude;
loc(int lg, int lt) temp.latitude = op2.latitude + latitude;
{ return temp;
longitude = lg; }
latitude = lt;
}
void show() { int main()
cout << longitude << " "; {
cout << latitude << "\n"; loc ob1(10, 20), ob2(1, 1);
} ob1.show();
loc operator+(loc op2); ob1(7, 8); // can be executed by itself
loc operator()(int i, int j); ob1.show();
}; ob1 = ob2 + ob1(10, 10); // can be used in expressions
// Overload ( ) for loc ob1.show();
loc loc::operator()(int i, int j) return 0;
{ }
longitude = i;
latitude = j;
return *this;
}
Overloading the Comma Operator
#include <iostream> cout << op2.longitude << " " << op2.latitude << "\n";
using namespace std; return temp;
class loc { }
int longitude, latitude; // Overload + for loc
public: loc loc::operator+(loc op2)
loc() {} {
loc(int lg, int lt) { loc temp;
longitude = lg; temp.longitude = op2.longitude + longitude;
latitude = lt; temp.latitude = op2.latitude + latitude;
} return temp;
void show() { }
cout << longitude << " "; int main()
cout << latitude << "\n"; {
} loc ob1(10, 20), ob2( 5, 30), ob3(1, 1);
loc operator+(loc op2); ob1.show();
loc operator,(loc op2); ob2.show();
}; ob3.show();
// overload comma for loc cout << "\n";
loc loc::operator,(loc op2) ob1 = (ob1, ob2+ob2, ob3);
{ ob1.show(); // displays 1 1, the value of ob3
loc temp; return 0;
temp.longitude = op2.longitude; }
temp.latitude = op2.latitude;
Overloading ->
• The –> pointer operator, also called the class member access operator,
is considered a unary operator when overloading.
➢ Using constructor
➢ Using Operator Overloading
Example (Using Constructor)
// Program to convert basic type to class type using constructor
int main()
#include <iostream>
{
using namespace std;
int duration;
class Time { cout<<"Enter time duration in minutes";
int hrs, min; cin>>duration;
public: Time t1=duration;
t1.display();
Time (int);
return 0;
void display(); }
};
Time :: Time (int t)
{
During type conversion using the
cout<<"Basic Type to ==> Class Type Conversion..."<<endl;
constructor we can pass only one
hrs=t/60;
argument and we can do type
min=t%60; conversion at the type of
} initialization only.
void Time::display()
{ cout<<hrs<< ": Hours(s)" <<endl; cout<<min<< " Minutes" <<endl;
}
Using Operator Overloading
• Type conversion from basic to class type can also be done by
operator overloading.
operator typename( )
{
//Function statements
}
Continued…
• The conversion function should satisfy the following condition: