Download presentation
Presentation is loading. Please wait.
Published byNorman Gibson Modified over 9 years ago
1
1 Lecture 17 Operator Overloading
2
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.
3
3 Introduction The operators that can be overloaded are +-*/%^&|~! =<>+=-=*=/=%=^= &=|= >>>= =&&||++--->*, - >[] ()newdeletenew[]delete[] The operators that cannot be overloaded are..*::?: sizeof
4
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=
5
5 Sample program to overload assignment operator (1st. version) #include class Rational { public: Rational (int=0, int=1); void operator=(const Rational&); void print() { cout << num << '/’ << den << endl;} private: int num; int den; }; Rational::Rational(int n, int d) { num = n; den = d; } // end Rational::Rational() void Rational::operator=(const Rational& r) { num = r.num; den = r.den; } // end Rational::operator=() void main() { Rational x(22,7), z; x.print(); cout<<endl; z.print(); cout<<endl; z = x; x.print(); cout<<endl; z.print(); cout<<endl; } //end main block This version does not check for self assignment (z=z) and allow chained assignments (z = x = y). Output: 22/7 0/1 22/7
6
6 Sample program to overload assignment operator (1st. version) z num den Rational x(22,7), z; x num den = 22 = 7 = 0 = 1 Rational::Rational(int n=0, int d=1) { num = n; den = d; } // end Rational::Rational() void Rational::operator=(const Rational& r) { num = r.num; den = r.den; } // end Rational::operator=() z = x;
7
7 Sample program to overload assignment operator (2nd. version) #include class Rational { public: Rational (int=0, int=1); Ratinal operator=(const Rational&); void print() { cout << num << '/’ << den << endl;} private: int num; int den; }; Rational::Rational(int n, int d) { num = n; den = d; }// end Rational::Rational Rational Rational::operator= (const Rational& r) { num = r.num; den = r.den; return *this; }// end Rational::operator= void main() { Rational x(22,7), y(-3,8), z; x.print(); cout<<endl; // prints 22/7 y.print(); cout<<endl;// prints -3/8 z.print(); cout<<endl;// prints 0/1 z = x = y; x.print(); cout<<endl;// prints -3/8 y.print(); cout<<endl; // prints -3/8 z.print(); cout<<endl; // prints -3/8 } //end main block
8
8 Sample program to overload assignment operator (1st. version) Rational x(22,7), y(-3,8); z; Rational Rational::operator=(const Rational& r) { num = r.num; den = r.den; return *this; } // end Rational::operator=() z num den x num den = 22 = 7 = 0 = 1 z = x = y; y num den = -3 = 8
9
9 Overloading the arithmetic operators (+, -, *, /) #include class Rational { friend Rational operator* (const Rational&, const Rational&); public: Rational (int=0, int=1); Rational operator=(const Rational&); void print() { cout << num << '/’ << den << endl;} private: int num; int den; }; Rational operator * (const Rational& x, const Rational& y) { Rational result; result.num = x.num * y.num; result.den = x.den * y.den; return result; } // end Rational operator*() Rational Rational::operator=(const Rational&r) { num = r.num; den = r.den; return *this; } // end Rational::operator=() void main() { Rational a(22,7), b(-3, 8), c; c = a; c.print(); cout<<endl;//prints 22/7 c = a * b; c.print(); cout<<endl;//prints -66/42 } //end main block Note: The operator * function is not a member function of class Rational but as a friend of the class Rational. The operator * function requires two arguments.
10
10 Sample program to overload assignment operator (1st. version) Rational a(22,7), b(-3,8); c; Rational operator * (const Rational& x, const Rational& y) { Rational result; result.num = x.num * y.num; result.den = x.den * y.den; return result; } // end Rational operator*() c num den a num den = 22 = 7 = 0 = 1 c = a * b; b num den = -3 = 8 * *
11
11 Overloading the arithmetic assignment operators (+=, -=, *=, /=) #include class Rational { public: Rational (int=0, int=1); Rational operator=(const Rational&); Rational operator*=(const Rational&); void print() { cout << num << '/’ << den << endl;} private: int num; int den; }; Rational Rational::operator *= (const Rational& x ) { num = num * x.num; den = den * x.den; return *this; } // end Rational::operator*=() Rational Rational::operator=(const Rational&r) { num = r.num; den = r.den; return *this; } // end Rational::operator=() void main() { Rational frac1(22,7), frac2(-3, 8), frac3; frac3 = frac1; frac3.print(); cout<<endl; //prints 22/7 frac1 *= frac2; frac1.print(); cout<<endl; //prints -66/42 } //end main block By returning *this, the operator can be chained, like: x *= y *= z;
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.