Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Operator Overloading. C++ 2 Outline  General technique  Overloading of the assignment operator  Overloading the increment and decrement operators."— Presentation transcript:

1 Operator Overloading

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

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

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

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

6 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; };

7 C++ 7 The + Operator MyVector MyVector::operator +(MyVector & v) { if (dim != v.dim) throw "Different size"; int* x = new int[dim];

8 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; }

9 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; }

10 C++ 10 The Inserter Operator ostream& operator << (ostream & s, const MyVector & v) { return v.Display(s); }

11 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; }

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

13 C++ 13 Example 1 (No Assignment Operator Needed) #include 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); };

14 C++ 14 Constructor Fraction::Fraction(int a, int b) { numerator = a; denominator = b; }

15 C++ 15 The * operator Fraction Fraction::operator *(Fraction& r) { return Fraction(numerator * r.numerator, denominator * r.denominator); }

16 C++ 16 The Display Member Function ostream& Fraction::Display(ostream &s) { return s << numerator << " / " << denominator; }

17 C++ 17 The << operator ostream& operator <<(ostream& s, Fraction & t) { return t.Display(s); }

18 C++ 18 The main Function int main() { Fraction x(3, 5); Fraction y(2, 7); Fraction z; z = x * y;// OK. No error cout << z;// Result: 6 / 36 }

19 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 assignment v2 = v1;  leads to a running-time error.

20 C++ 20 The Error in Visual C++

21 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; };

22 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; }

23 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; }

24 C++ 24 Output 3 6 9 12 15 1 2 3 4 5

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

26 C++ 26 The MyVector Class class MyVector { private: int *elem; int dim; public: MyVector(int *e, int d); MyVector(const MyVector & v);

27 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; };

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

29 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 (v); }

30 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; }

31 C++ 31 The Copy Constructor and the Assignment Operator MyVector v2(v1);// copy constructor MyVector v2 = v1;// copy constructor MyVector v2; // default constructor v2 = v1; // assignment operator

32 C++ 32 Overloading the Increment and Decrement Operators  pre increment  post increment  pre decrement  post decrement

33 C++ 33 Example #include using namespace std; class Fraction { int numerator; int denominator; public:

34 C++ 34 Example Fraction(int a = 0, int b = 1); Fraction operator *(Fraction& r); Fraction& operator ++(); // pre increment Fraction operator ++( int ); // post increment ostream& Display(ostream &s); };

35 C++ 35 Constructor Fraction::Fraction(int a, int b) { numerator = a; denominator = b; }

36 C++ 36 Pre Increment Operator Fraction& Fraction::operator ++() { numerator += denominator; return *this; }

37 C++ 37 Post Increment Operator Fraction Fraction::operator ++( int ) { Fraction t(numerator, denominator); numerator += denominator; return t; }

38 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; }

39 C++ 39 Output z = 3 / 4 x = 7 / 4 z = 7 / 4 y = 7 / 4


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

Similar presentations


Ads by Google