2. Introduction
A number of predefined operators can be applied to the built-
in standard types.
These operators can be overloaded, means each of them can
be used for several standard types.
For example: operator + operates on int, float and
double. So + is already OverLoaded ( same interface but
different implementations)
Can + --- ADD A & B
Adding additional processing power to the operator.2
3. Introduction
The operators that can be overloaded are
+ - * / % ^ & | ~ !
= < > += -= *= /= %= ^=
&= |= << >> >>= <<= == != <=
>= && || ++ -- ->* , -> []
() new delete new[] delete[]
The operators that cannot be overloaded are
. .* :: ?: sizeof
3
4. Overloading the assignment operator
(=)
Purpose of assignment operator is to copy one object or
value to another.
Similar to the default constructor, the copy constructor,
and the destructor, the assignment operator is created
automatically for every class that is defined.
But also the assignment operator can be defined
explicitly in the class definition.
How to overload operator?
Write a function with the keyword operatorX, where X is
an operator symbol. For example: operator=
4
5. Sample program to overload
assignment operator (1st. version)
#include<iostream.h> void main()
class Rational { { Rational x(22,7), z;
public: x.print(); cout<<endl;
Rational (int=0, int=1); z.print(); cout<<endl;
void operator=(const Rational&); z = x;
void print() { cout << num << '/’ << den x.print(); cout<<endl;
<< endl;} z.print(); cout<<endl;
private: } //end main block
int num;
int den; Output:
};
22/7
Rational::Rational(int n, int d) 0/1
{ num = n; 22/7
den = d; } // end Rational::Rational() 22/7
void Rational::operator=(const Rational& r) This version does not check for
{ num = r.num;
den = r.den; self assignment (z=z) and allow
} // end Rational::operator=() chained assignments (z = x = y).
5
6. Sample program to overload
assignment operator (1st. version)
Rational::Rational(int n=0, int d=1)
{ num = n;
z x den = d; } // end Rational::Rational()
num = 0 num = 22
den = 1 den = 7
void Rational::operator=(const Rational& r)
Rational x(22,7), z; { num = r.num;
den = r.den;
z = x; } // end Rational::operator=()
6
7. Sample program to overload
assignment operator (2nd. version)
#include<iostream.h> Rational Rational::operator= (const Rational& r)
class Rational { { num = r.num;
public: den = r.den;
Rational (int=0, int=1); return *this; }// end Rational::operator=
Ratinal operator=(const Rational&);
void print() { cout << num << '/’ << den void main()
<< endl;} { Rational x(22,7), y(-3,8), z;
private: x.print(); cout<<endl; // prints 22/7
int num; y.print(); cout<<endl; // prints -3/8
int den; z.print(); cout<<endl; // prints 0/1
}; z = x = y;
x.print(); cout<<endl; // prints -3/8
Rational::Rational(int n, int d) y.print(); cout<<endl; // prints -3/8
{ num = n; z.print(); cout<<endl; // prints -3/8
den = d; }// end Rational::Rational
} //end main block
7
8. Sample program to overload
assignment operator (1st. version)
z x y
num = 0 num = 22 num = -3
den = 1 den = 7 den = 8
void Rational::operator=(const Rational& r)
Rational x(22,7), y(-3,8); z; { num = r.num;
den = r.den;
z = x = y; return *this;
} // end Rational::operator=()
8
9. Overloading the arithmetic operators
(+, -, *, /)
#include<iostream.h> void Rational::operator=(const Rational& r)
class Rational { { num = r.num;
den = r.den;
friend Rational operator* (const Rational&,
return this;
const Rational&);
} // end Rational::operator=()
public:
Rational (int=0, int=1);
void main()
Rational operator=(const Rational&);
{ Rational a(22,7), b(-3, 8), c;
void print() { cout << num << '/’ << den
c = a;
<< endl;}
c.print(); cout<<endl; //prints 22/7
private:
int num; c = a * b;
int den; c.print(); cout<<endl; //prints -66/42
}; } //end main block
Note:
Rational operator*(const Rational& x, const
•The operator* function is not a
Rational& y)
{ Rational result; member function of class Rational but
result.num = x.num * y.num; as a friend of the class Rational.
result.den = x.den * y.den; •The operator* function requires two
return result; } // end Rational operator*()
arguments. 9
10. Sample program to overload
assignment operator (1st. version)
c a b
num = 0 num = 22 * num = -3
den = 1 den = 7 * den = 8
Rational operator*(const Rational& x, const Rational& y)
Rational a(22,7), b(-3,8); c; { Rational result;
result.num = x.num * y.num;
result.den = x.den * y.den;
c = a * b; return result; } // end Rational operator*()
10
11. Overloading the arithmetic assignment
operators (+=, -=, *=, /=)
#include<iostream.h> void Rational::operator=(const Rational& r)
class Rational { { num = r.num;
public: den = r.den;
Rational (int=0, int=1); return *this;
Rational operator=(const Rational&); } // end Rational::operator=()
Rational operator*=(const Rational&);
void print() { cout << num << '/’ << den void main()
<< endl;} { Rational frac1(22,7), frac2(-3, 8), frac3;
private: frac3 = frac1;
int num; frac3.print(); cout<<endl; //prints 22/7
int den; frac1 *= frac2;
}; frac1.print(); cout<<endl; //prints -66/42
} //end main block
Rational Rational::operator*= (const
Rational& x ) By returning *this, the operator can be
{ num = num * x.num; chained, like: x *= y *= z;
den = den * x.den;
return *this; } // end Rational::operator*=()
11