39
Operator Overloading

Operator Overloading. C++ 2 Outline General technique Overloading of the assignment operator Overloading the increment and decrement operators

Embed Size (px)

Citation preview

Page 1: Operator Overloading. C++ 2 Outline  General technique  Overloading of the assignment operator  Overloading the increment and decrement operators

Operator Overloading

Page 2: Operator Overloading. C++ 2 Outline  General technique  Overloading of the assignment operator  Overloading the increment and decrement operators

C++ 2

Outline

General technique Overloading of the assignment

operator Overloading the increment and

decrement operators

Page 3: Operator Overloading. C++ 2 Outline  General technique  Overloading of the assignment operator  Overloading the increment and decrement operators

C++ 3

General Technique

In the case of our MyVector class, if v1 and v2 are two vectors, then the sum of them can be printed out with the following statement:

v1.Sum(v2).Display(); But we would like to do this, by using the

form:cout << v1 + v2;

Thus, we have to overload the << and + operators.

Page 4: Operator Overloading. C++ 2 Outline  General technique  Overloading of the assignment operator  Overloading the increment and decrement operators

C++ 4

General Rules

There is no way to define new operators, just the existing ones can be overloaded.

There are some operators which cannot be overloaded, like the scope operator (::), and the member selection operator (.).

There is no possibility to change the nature of an operator (binary or unary, and associativity).

Page 5: Operator Overloading. C++ 2 Outline  General technique  Overloading of the assignment operator  Overloading the increment and decrement operators

C++ 5

How to Overload an Operator?

Define a special member function (or friend function).

The name of the function is composed of the operator keyword, and the respective operator.

Page 6: Operator Overloading. C++ 2 Outline  General technique  Overloading of the assignment operator  Overloading the increment and decrement operators

C++ 6

The Class Declaration

class MyVector {private:

int *elem;int dim;

public:MyVector(int *e, int d);MyVector(const MyVector & v);~MyVector();void SquareVect();MyVector operator +(MyVector & v);ostream& Display(ostream & s) const;

};

Page 7: Operator Overloading. C++ 2 Outline  General technique  Overloading of the assignment operator  Overloading the increment and decrement operators

C++ 7

The + Operator

MyVector MyVector::operator +(MyVector & v)

{if (dim != v.dim)

throw "Different size";int* x = new int[dim];

Page 8: Operator Overloading. C++ 2 Outline  General technique  Overloading of the assignment operator  Overloading the increment and decrement operators

C++ 8

The + Operator

for(int i = 0; i < dim; i++)x[i] = elem[i] + v.elem[i];

MyVector t(x, dim);delete [] x;return t;

}

Page 9: Operator Overloading. C++ 2 Outline  General technique  Overloading of the assignment operator  Overloading the increment and decrement operators

C++ 9

The Display Member Function

ostream & MyVector::Display(ostream & s) const

{for(int i = 0; i < dim; i++)

s << elem[i] << '\t';return s << endl;

}

Page 10: Operator Overloading. C++ 2 Outline  General technique  Overloading of the assignment operator  Overloading the increment and decrement operators

C++ 10

The Inserter Operator

ostream& operator << (ostream & s, const MyVector & v)

{return v.Display(s);

}

Page 11: Operator Overloading. C++ 2 Outline  General technique  Overloading of the assignment operator  Overloading the increment and decrement operators

C++ 11

The main Function

int main() {int x[]={1, 2, 3, 4, 5};MyVector v1(x, 5);int y[]={2, 4, 6, 8, 10};MyVector v2(y, 5);cout << v1+v2;

}

Page 12: Operator Overloading. C++ 2 Outline  General technique  Overloading of the assignment operator  Overloading the increment and decrement operators

C++ 12

Overloading of the Assignment Operator

By default a member-wise assignment is defined.

Generally this gives good results if there are no pointer data members.

In other case we should overload the assignment operator.

Page 13: Operator Overloading. C++ 2 Outline  General technique  Overloading of the assignment operator  Overloading the increment and decrement operators

C++ 13

Example 1 (No Assignment Operator Needed)

#include <iostream>using namespace std;class Fraction {

int numerator;int denominator;

public:Fraction(int a = 0, int b = 1);Fraction operator *(Fraction& r); ostream& Display(ostream &s);

};

Page 14: Operator Overloading. C++ 2 Outline  General technique  Overloading of the assignment operator  Overloading the increment and decrement operators

C++ 14

Constructor

Fraction::Fraction(int a, int b){

numerator = a;denominator = b;

}

Page 15: Operator Overloading. C++ 2 Outline  General technique  Overloading of the assignment operator  Overloading the increment and decrement operators

C++ 15

The * operator

Fraction Fraction::operator *(Fraction& r){

return Fraction(numerator * r.numerator,

denominator * r.denominator);}

Page 16: Operator Overloading. C++ 2 Outline  General technique  Overloading of the assignment operator  Overloading the increment and decrement operators

C++ 16

The Display Member Function

ostream& Fraction::Display(ostream &s)

{return s << numerator << " / " << denominator;

}

Page 17: Operator Overloading. C++ 2 Outline  General technique  Overloading of the assignment operator  Overloading the increment and decrement operators

C++ 17

The << operator

ostream& operator <<(ostream& s, Fraction & t)

{return t.Display(s);

}

Page 18: Operator Overloading. C++ 2 Outline  General technique  Overloading of the assignment operator  Overloading the increment and decrement operators

C++ 18

The main Function

int main() {Fraction x(3, 5);Fraction y(2, 7);Fraction z;z = x * y; // OK. No errorcout << z; // Result: 6 / 36

}

Page 19: Operator Overloading. C++ 2 Outline  General technique  Overloading of the assignment operator  Overloading the increment and decrement operators

C++ 19

Example 2

MyVector has a data member (elem), which is a pointer, thus we must overload the assignment operator.

In other case, the assignmentv2 = v1;

leads to a running-time error.

Page 20: Operator Overloading. C++ 2 Outline  General technique  Overloading of the assignment operator  Overloading the increment and decrement operators

C++ 20

The Error in Visual C++

Page 21: Operator Overloading. C++ 2 Outline  General technique  Overloading of the assignment operator  Overloading the increment and decrement operators

C++ 21

The MyVector Class

class MyVector {private:

int *elem;int dim;

public:MyVector(int *e, int d);MyVector(const MyVector & v);~MyVector();void SquareVect();MyVector operator +(MyVector & v);MyVector& operator =(const MyVector& v);ostream& Display(ostream & s) const;

};

Page 22: Operator Overloading. C++ 2 Outline  General technique  Overloading of the assignment operator  Overloading the increment and decrement operators

C++ 22

The Assignment Operator

MyVector& MyVector::operator =(const MyVector& v) {if (this != &v) {

delete[] elem;dim = v.dim;elem = new int[dim];for(int i = 0; i < dim; i++)

elem[i] = v.elem[i];}return *this;

}

Page 23: Operator Overloading. C++ 2 Outline  General technique  Overloading of the assignment operator  Overloading the increment and decrement operators

C++ 23

The main Function

int main() {int x[]={1, 2, 3, 4, 5};MyVector v1(x, 5);int y[]={2, 4, 6, 8, 10};MyVector v2(y, 5);cout << v1+v2;v2 = v1;cout << v2;

}

Page 24: Operator Overloading. C++ 2 Outline  General technique  Overloading of the assignment operator  Overloading the increment and decrement operators

C++ 24

Output

3 6 9 12 151 2 3 4 5

Page 25: Operator Overloading. C++ 2 Outline  General technique  Overloading of the assignment operator  Overloading the increment and decrement operators

C++ 25

Other Assignment Operators

All op= operators can be overloaded, where op is a binary arithmetical or bitwise operator.

By default these operators are not overloaded.

If op is already overloaded, then we can use it, and the current object can be accessed in the form: *this.

Page 26: Operator Overloading. C++ 2 Outline  General technique  Overloading of the assignment operator  Overloading the increment and decrement operators

C++ 26

The MyVector Class

class MyVector {private:

int *elem;int dim;

public:MyVector(int *e, int d);MyVector(const MyVector & v);

Page 27: Operator Overloading. C++ 2 Outline  General technique  Overloading of the assignment operator  Overloading the increment and decrement operators

C++ 27

The MyVector Class

~MyVector();void SquareVect();MyVector operator +(const MyVector & v);MyVector& operator =(const MyVector& v);MyVector& operator +=(const MyVector& v);ostream& Display(ostream & s) const;

};

Page 28: Operator Overloading. C++ 2 Outline  General technique  Overloading of the assignment operator  Overloading the increment and decrement operators

C++ 28

The += operator

MyVector& MyVector::operator +=(const MyVector& v)

{return *this = *this + v;

} Because the formal parameter v is

declared as const we must have the same in case of the + operator.

Page 29: Operator Overloading. C++ 2 Outline  General technique  Overloading of the assignment operator  Overloading the increment and decrement operators

C++ 29

If there is no const

If this is not the case, then we can define the += operator in the following form:

MyVector& MyVector::operator +=(const MyVector& v)

{return *this = *this + const_cast<MyVector&>(v);

}

Page 30: Operator Overloading. C++ 2 Outline  General technique  Overloading of the assignment operator  Overloading the increment and decrement operators

C++ 30

The main Function

int main() {int x[]={1, 2, 3, 4, 5};MyVector v1(x, 5);int y[]={2, 4, 6, 8, 10};MyVector v2(y, 5);v2 += v1;cout << v2;

}

Page 31: Operator Overloading. C++ 2 Outline  General technique  Overloading of the assignment operator  Overloading the increment and decrement operators

C++ 31

The Copy Constructor and the Assignment Operator

MyVector v2(v1); // copy constructor

MyVector v2 = v1; // copy constructor

MyVector v2; // default constructorv2 = v1; // assignment operator

Page 32: Operator Overloading. C++ 2 Outline  General technique  Overloading of the assignment operator  Overloading the increment and decrement operators

C++ 32

Overloading the Increment and Decrement Operators

pre increment post increment pre decrement post decrement

Page 33: Operator Overloading. C++ 2 Outline  General technique  Overloading of the assignment operator  Overloading the increment and decrement operators

C++ 33

Example

#include <iostream>using namespace std;class Fraction {

int numerator;int denominator;

public:

Page 34: Operator Overloading. C++ 2 Outline  General technique  Overloading of the assignment operator  Overloading the increment and decrement operators

C++ 34

Example

Fraction(int a = 0, int b = 1);Fraction operator *(Fraction& r);Fraction& operator ++(); // pre incrementFraction operator ++( int ); // post incrementostream& Display(ostream &s);

};

Page 35: Operator Overloading. C++ 2 Outline  General technique  Overloading of the assignment operator  Overloading the increment and decrement operators

C++ 35

Constructor

Fraction::Fraction(int a, int b){

numerator = a;denominator = b;

}

Page 36: Operator Overloading. C++ 2 Outline  General technique  Overloading of the assignment operator  Overloading the increment and decrement operators

C++ 36

Pre Increment Operator

Fraction& Fraction::operator ++(){ numerator += denominator; return *this;}

Page 37: Operator Overloading. C++ 2 Outline  General technique  Overloading of the assignment operator  Overloading the increment and decrement operators

C++ 37

Post Increment Operator

Fraction Fraction::operator ++( int ) { Fraction t(numerator,

denominator); numerator += denominator; return t;}

Page 38: Operator Overloading. C++ 2 Outline  General technique  Overloading of the assignment operator  Overloading the increment and decrement operators

C++ 38

The main Function

int main() {Fraction x(3, 4);Fraction y(3, 4);Fraction z;z = x++;cout << "z = " << z << endl;cout << "x = " << x << endl;

z = ++y;cout << "z = " << z << endl;cout << "y = " << y << endl;

}

Page 39: Operator Overloading. C++ 2 Outline  General technique  Overloading of the assignment operator  Overloading the increment and decrement operators

C++ 39

Output

z = 3 / 4x = 7 / 4z = 7 / 4y = 7 / 4