You are on page 1of 27

CSE205

Object Oriented Programming Language


Overloading
Operators

2
What do we really want ?
class Complex{ main(){
int real; int i=2,j = 3,k;
int imaginary; i = j + k; Sorry !
public: Not Allowed
Complex();
Complex C1(1,2), C2(4,5), C3
Complex(int r, int i); C3=C1.add(C2) ;
void setVal(int r, int i); C3 = C1 + C2;
int getReal(); }
int getImaginary();

Complex add (Complex C);


};

3
Wish fulfilled
class Complex{ main(){
int real; int i=2,j = 3,k;
int imaginary; i = j + k; OK !
public: It will work
Complex();
Complex C1(1,2), C2(4,5), C3
Complex(int r, int i); C3=C1.add(C2) ;
void setVal(int r, int i);
+ operator C3 = C1 + C2;
int getReal(); }
int getImaginary(); Overloaded
Complex add (Complex C);
Complex operator + (Complex C) ;

};

4
Overloading Operators

 Allows user-defined types to act like


built in types
 Another way to make a function call
 Decreases programming complexity
 Increases readability

5
Overloaded operators
Unary and binary operators can be overloaded:
+ - * / % ^ & | ~
! = < > += -= *= /= %=
^= &= |= << >> >>= <<= ==
!= <= >= && || ++ -- ,
() []
operator new operator delete
operator new[] operator delete[]

6
Operators you can't overload

. .* :: ?: sizeof

7
Restrictions
 Only existing operators can be overloaded
(you can't create a ** operator for
exponentiation)
 You can’t change how they act!
 Preserve number of operands
 Preserve precedence
 Preserve associativity

8
How to overload
 Just a function with an operator name!
return_type operator OP (arglist)
First Argument
1. Can be a member function Implicit

Complex operator +(const Complex&);


First Argument
2. Can be a global (free) function explicit
Complex operator +(Complex&, Complex&);

9
Syntax
 Binary Operator
 Member Function
 ret-type operator op( arg )
 Global Function
 ret-type operator op( arg1, arg2 )
 Unary Operator
 Member Function
 ret-type operator op()
 Global Function
 ret-type operator op( arg )

10
Binary Operators (Member
functions)
class Complex { main(){
int r, i;
C3 ==3,k;
int i=2,j C1.operator+(C2)
public:
i = j + k;
Complex( ) {r=0 ; i=0; }
Complex C1(1,2), C2(4,5), C3
Complex( int a, int b ){r=a; i=b; }
Complex operator+(Complex c) { C3 = C1 + C2;
Complex temp; C3.show();
temp.r = r + c.r; }

temp.i = i + c.i;
return temp; Output: ?
}
void show(){cout<<r<<“+”<<i<<“i”};
};

11
Unary Operator (Member Functions)
class Complex { Overloaded
int r, i; Binary ‘-’ main(){
public: C3 == 3,k;
int i=2,j C1.operator- (C2)
Complex operator-(Complex c) { i = j + k;
Complex temp; Complex C1(1,2), C2(4,5), C3
temp.r = r - c.r; Overloaded
Unary ‘-’ C3 = C1 C3 - C2;= C1.operator-()
temp.i = i - c.i;
return temp; } C3.show();
Complex operator-() { C3 = - C1;
Complex temp;
temp.r = -r ; C3.show();
temp.i = -i ; }
return temp; }
void show(){cout<<r<<“+”<<i<<“i”}; Output: ?
};

12
Relational Operators (Member
Functions)
class Complex {
int r, i; main(){
public: int i=2,j =C1.operator>(C2)
3,k;
………… i = j + k;C1.operator==(C2)
bool operator==(Complex& C ) ;
Complex C1(1,2), C2(4,5), C3
bool operator!= (Complex& C ) ; C1.operator<(C2)
if (C1> C2) cout << “Greater”;
bool operator< (Complex& C ) ;
if (C1==C2) cout << “Equal”;
bool operator> (Complex& C ) ;
bool operator<=(Complex& C ) ; if (C1 < C2) cout << “Smaller”;
}
bool operator>=(Complex& C ) ;
void show(){cout<<r<<“+”<<i<<“i”};
}; Output: ?

13
Relational Operators (Member
Functions)
bool Complex::operator>(Complex& C ) {
if (mod() > C.mod()) return true;
else return false;
}
bool Complex::operator==(Complex& C ) {
if (mod() == C.mod()) return true;
else return false;
Using already
} overloaded ==
operator
bool Complex::operator!=(Complex& C ) {
if ( *this == C) return false;
else return true;
}

14
operator ++ (Same for --)
Prefix
class Complex { Overloaded main(){ Operator
int r, i; Prefix ++
public: int i=2, j = 3, k;
Postfix
Complex operator++() { k = ++ i; Operator
++r ; ++ i ; Overloaded k = j++C3
; = C1.operator++()
Postfix ++
return *this ; Complex C1(1,2), C2(4,5),C3;
} C3 = C2.operator++(0)
Complex operator++ ((int
) { unused) { C3 = ++C1;
r++ ; C1.show();
i++ ; C3 = C2++;
return *this; C2.show();
} }
};
Output: ?
15
Operator as a global function
Complex operator+(Complex& C1, Complex& C2){
int r = C1.getReal() + C2. getReal();
int i = C1.getImaginary() + C2. getImaginary();
Complex temp (r,i);
return temp;
}

main(){ C3 = operator+(C1,C2)
Complex C1(1,2) ,C2(3,4), C3;
C3 = C1 + C2;
C3.show();
}

16
Operator as a global function (Friend)
class Complex {
int r, i;
public:
………
friend Complex operator+(Complex& C1, Complex& C2);
………
};
Complex operator+(Complex& C1, Complex& C2){
int r = C1. r + C2 . r;
int i = C1 . i + C2 . i;
Complex temp (r,i);
return temp;
}

17
Global Function
 binary operators require two arguments
 unary operators require one argument
 If you don't have access to private data
members, then the global function must use
the public interface.

18
Why Global function ?
Complex operator+(int x, Complex c){
class Complex { Complex temp;
int r, i; temp.r = x + C . r ;
public: temp.i = x + C.i ;
return temp;
Complex operator+(int x) {
}
Complex temp;
temp.r = r+x ; main(){
temp.i = i+x ;
C3 = C1.operator+(i)
int i = 2;
return temp ; Complex C1(1,2) ,C2(3,4), C3;
}
C3 = C1C3 + i; = operator+( i, C2 )
friend Complex operator+(int x,
C3.show();
Complex c );
C3 = i + C2;
};
C3.show();
}

19
Overload Assignment (=)
class student { main(){ st2.operator=(st1)
char* name;
st3.operator=(st2.operator=(st1)) student st1(“St1”), st2(“St2”),st3;
public:
student(char* p) {… } st2 = st1;
~student(){ … } cout << st2.getName();
st3 = st2 = st1;
student& operator=(student& st){ }
delete[] name;
name = new char[strlen(st.name)];
strcpy(name,st.name);
return *this
}

char* getName() { return name;}


};

20
Assignment operator ( = )
• The assignment operator= for a class type is by
default generated by the compiler to have
member-by-member assignment (based on
bitwise copying).
• The operator=() returns reference to the object
and has one argument of type reference to object.
Its function prototype is:
element_type& operator=(element_type& );
• Must be overloaded as nonstatic member
functions, and normally return by reference to
the object.

21
Why overload = ?
Anytime a class needs an explicit copy constructor
defined, it also needs an assignment operator defined.
In classes that use new to construct objects, a copy
constructor and operator= should be explicitly
provided.
In general, it is usual to provide a default and copy
constructor and operator= with any class that uses
pointers in its implementation.
To prevent assignment, explicitly declare
operator= as private

22
What Left ?
Overloaded main(){
class Complex { Inserter ‘>>’ int i;
int r, i; operator
cin >> i;
public:
Overloaded operator>>(cin,C1)
cout << i;
friend istream
Extractor ‘<<’ operator>>(istream& in,
operator Complex &C); Complex C1,C2,C3;
operator<<(cout,C1)
friend ostream operator<<(ostream& out, cin >> C1 ;
operator>>(operator>>(cin,C2),C3)
Complex &C); cout << C1;
};
cin >> C2 >> C3;
istream operator>>(istream& in, Complex &C){
in >> C.r >> C.i ; cout << C2 << C3;
return in; }
operator<<(operator<<(cout,C2),C3)
ostream operator<<(ostream& out, Complex C){
out << C.r << C.i ; }
return out; }

23
Defining a stream inserter
 Has to be a 2-argument free function
 First argument is an istream&
 Second argument is a reference to an object
istream& operator>>(istream& is, T& obj) {
// specific code to read obj
return is;
}
 Return an istream& for chaining
cin >> a >> b >> c; // ((cin >> a) >> b) >> c;

24
Creating a stream extractor
 Has to be a 2-argument free function
 First argument is an ostream&
 Second argument is any value
ostream& operator<<(ostream& os, T& obj) {
// specific code to write obj
return os;
}
 Return an ostream& for chaining
cout << a << b << c; // ((cout << a) << b) << c;

25
Operator []
class iArray{ main(){
int a[10];
int* a;
for (int i=0;i<10;i++)
public:
cin>>a[i];
iArray (int size){
for (int i=0;i<10;i++)
a = new int [size]; arr.operator[](i)
cout << a[3];
}
iArray arr(10);
~iArray(){
for (int i=0;i<10;i++)
delete[] a;
cin>>arr[i];
} for (int i=0;i<10;i++)
int& operator[](int i){ cout << arr[i];
return a[i];
}
}
};

26
Operator []
 Must be a member function
 Act as a binary operator
 Single argument
 Implies that the object it is being called for
acts like an array, so it should return a
reference
 Basic format
 type& operator[](int i)

27

You might also like