OPERATOR OVERLOADING Introduction Rules Types Programs Introduction Allows

  • Slides: 13
Download presentation
OPERATOR OVERLOADING Introduction Rules Types Programs

OPERATOR OVERLOADING Introduction Rules Types Programs

Introduction Allows us to define the behavior of operators when applied to objects of

Introduction Allows us to define the behavior of operators when applied to objects of a class � Operator Overloading does not allow us to alter the meaning of operators when applied to built-in types ◦ Example: 2+3 addition, 2*3 multiplication By operator overloading, we can apply the operators to our own abstract data type like this ◦ Example; Class A{ main() int a, b; { public: A obj 1(2, 3); A(int x, int y){a=x; b=y; } -obj 1; }; }

To Overload an Operator � Steps � Overloading an operator �Write function definition as

To Overload an Operator � Steps � Overloading an operator �Write function definition as normal �Function name : operator keyword followed by the symbol �Operator+ () � Applying operators on objects ◦ Create objects for the class ◦ Apply the operator on objects �object 1 + object 2 �-object 1

Rules to Overload an operator � Rules ◦ Precedence of an operator cannot be

Rules to Overload an operator � Rules ◦ Precedence of an operator cannot be changed ◦ Associativity of an operator cannot be changed ◦ Arity (number of operands) cannot be changed ◦ No new operators can be created ( Ex: +-) �Use only existing operators ◦ No overloading operators for built-in types �Cannot change how two integers are added ◦ Produces a syntax error (Ex; 2+3) Operator functions can be member or nonmember functions

Rules to Overloade an operator � C++ operators that can be overloaded � C++

Rules to Overloade an operator � C++ operators that can be overloaded � C++ Operators that cannot be overloaded

Types � Syntax Return type class name : : operator symbol(args){ } Using Member

Types � Syntax Return type class name : : operator symbol(args){ } Using Member Function � Unary Operator Overloading Ex: A operator –() ( ) � Binary Operator Overloading Ex: A operator –(A object) ( ) Using Non Member Function (use Friend Keyword) � Unary Operator Overloading Ex: A operator –(A object) ( ) � Binary Operator Overloading Ex: A operator –(A object 1, A object 2) ( )

Unary operator : Member Function class A { int inches, feet; public: A(int x,

Unary operator : Member Function class A { int inches, feet; public: A(int x, int y) { inches=x; feet=y; } void display() { cout <<"n"<<inches<<" "<<feet; } void operator-() { inches=-inches; feet=-feet; } }; int main() { A obj 1(20, 10); obj 1. display(); -obj 1; obj 1. display(); }

Binary operator Using Member Function class A { int a, b; public: A(int x,

Binary operator Using Member Function class A { int a, b; public: A(int x, int y) { a=x; b=y; } void display() { cout <<"n"<<inches<<" "<<feet; } void operator+(A o 1); }; Void A : : operator +(A o 1) { a=a+o 1. a; b=b+o 1. b; }

int main() { A obj 1(20, 10); A. obj 2(10, 20); obj 1+obj 2;

int main() { A obj 1(20, 10); A. obj 2(10, 20); obj 1+obj 2; obj 1. display(); }

Unary operator : Non Member Function class A { int a, b public: A()

Unary operator : Non Member Function class A { int a, b public: A() { a=b=0; } A(int x, int y) { a=x; b =y; } void display() { cout <<"n"<<a<<" "<<b; } friend A operator-(A) }; A operator-(A d 1) { A d 2; d 2. a=-d 1. a; d 2. b=-d 1. b; return d 2; }

int main() { A obj 1(20, 10); obj 1. display(); A C; C=-obj 1;

int main() { A obj 1(20, 10); obj 1. display(); A C; C=-obj 1; C. display(); }

Unary operator : Non Member Function class A { int a, b public: A()

Unary operator : Non Member Function class A { int a, b public: A() { a=b=0; } A(int x, int y) { a=x; b =y; } void display() { cout <<"n"<<a<<" "<<b; } friend A operator+(A, A) }; A operator+(A d 1, A d 2) { A d 3; d 3. a=-d 1. a+d 2. a; d 3. b=-d 1. b+d 2. b; return d 3; }

int main() { A obj 1(20, 10); A obj 2(20, 10); obj 1. display();

int main() { A obj 1(20, 10); A obj 2(20, 10); obj 1. display(); Obj 2. display(); A C; C=obj 1+obj 2; C. display(); }