Operator Overloading. C++ 2 Outline General technique Overloading of the assignment operator ...

Preview:

Citation preview

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.

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).

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.

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;

};

C++ 7

The + Operator

MyVector MyVector::operator +(MyVector & v)

{if (dim != v.dim)

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

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;

}

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;

}

C++ 10

The Inserter Operator

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

{return v.Display(s);

}

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;

}

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.

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);

};

C++ 14

Constructor

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

numerator = a;denominator = b;

}

C++ 15

The * operator

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

return Fraction(numerator * r.numerator,

denominator * r.denominator);}

C++ 16

The Display Member Function

ostream& Fraction::Display(ostream &s)

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

}

C++ 17

The << operator

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

{return t.Display(s);

}

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

}

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.

C++ 20

The Error in Visual C++

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;

};

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;

}

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;

}

C++ 24

Output

3 6 9 12 151 2 3 4 5

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.

C++ 26

The MyVector Class

class MyVector {private:

int *elem;int dim;

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

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;

};

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.

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);

}

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;

}

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

C++ 32

Overloading the Increment and Decrement Operators

pre increment post increment pre decrement post decrement

C++ 33

Example

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

int numerator;int denominator;

public:

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);

};

C++ 35

Constructor

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

numerator = a;denominator = b;

}

C++ 36

Pre Increment Operator

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

C++ 37

Post Increment Operator

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

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

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;

}

C++ 39

Output

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

Recommended