11.Operatoroverloading Part1.Pptx
11.Operatoroverloading Part1.Pptx
Outline
❖ Why we need operator overloading?
Class Complex
{ private:
int real, img;
};
Int main()
{
Complex comp1,comp2,comp3;
cin>>comp1>>comp2; // extraction operator >> is overloaded
comp3=comp1+comp2; // arithmetic operator + is overloaded
cout<<comp3; // insertion operator << is overloaded
If (comp1==comp3) // relational operators == is overloaded
comp3 +=comp1 // arithmetic assignment operator += is overloaded
}
Operator overloading in C++
What is operator overloading?
❖ It is a type of compile time polymorphism in which an operator is overloaded to give
user defined meaning to it
❖ Operator overloading is a way of providing new implementation of existing operators
to work with user-defined data types
❖ A new implementation can be done by defining a special function called operator
function
❖ Unary operator operates on single operand where as the binary operator operates on
two operands
❖ Hence two different operator function formats are required to overload unary and
binary operator
+ - * / % ^
& | ~ ! , =
+= -= /= %= ^= &=
|= *= <<= >>= [] ()
Output:
Before calling Operator Overloading: value of n is: 5
After calling Operator Overloading: value of n is: 0
Operator overloading in C++
Unary operator overloading using member function
Prefix (++) Increment Operator
class Check int main()
{ {
private: Check obj;
int i; cout<<"obj:";
public: obj.Display();
Check(): i(0) { } ++obj; //obj.operator++();
void operator ++() cout<<"++obj:";
{ obj.Display();
++i; return 0;
} }
void Display()
{
cout << “ i=" << i << endl;
}
}; Output:
obj: i=0
++obj: i=1
Output:
obj: i=5
--obj: i=4
obj--: i=3
Operator overloading in C++
Unary operator overloading using member function
Prefix, Postfix( -- )decrement Operator
Output:
obj: i=5
--obj: i=4
obj--: i=4
Operator overloading in C++
()-Parentheses (function call) Operator Overloading using
member function
class Distance { void displayDistance() {
private: cout << "F: " << feet << " I:" << inches << endl;
int feet,inches;
}
public:
};
Distance():feet(0),inches(0)
int main() {
{}
Distance D1(11, 10), D2;
Distance(int f, int i) :feet(f),inches(i)
{ } cout << "First Distance : ";
Distance operator()(int f, int i ) { D1.displayDistance();
Distance D; D2 = D1(5, 10); // D1.operator(5,10);
D.inches = inches+i ; cout << "Second Distance D1+(5,10) :";
if(D.inches>=12) D2.displayDistance();
{ D.feet = feet+f+1; return 0;
D.inches-=12; }
}
else Output:
D.feet = feet+f; First Distance : F: 11 I:10
return D; Second Distance D1+(5,10) :F: 17 I:8
}
Operator overloading in C++
Subscript or array index operator ([ ] ) overloading using
member function
class Array void print()
{ { for(int i = 0; i < size; i++)
private: cout<<array[i]<<" ";
int *array; cout<<endl;
int size; }
public: };
Array(int *temp,int s) int main()
{ array=temp; { int a[] = {1, 2, 4, 5};
size=s; Array arr1(a, 4);
} arr1.print();
int &operator[](int index) arr1[2] = 6;
{ arr1.print();
if (index >= size) arr1[8] = 6;
{ return 0;
cout << "Array index out of bound, exiting"; }
exit(0); Output:
} 1245
return array[index]; 1265
} Array index out of bound, exiting
Operator overloading in C++