Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 13 Operator Overloading 1. Operator overload In C++, operator can be overload according to class type that you define. Operator overload is closely.

Similar presentations


Presentation on theme: "Chapter 13 Operator Overloading 1. Operator overload In C++, operator can be overload according to class type that you define. Operator overload is closely."— Presentation transcript:

1 Chapter 13 Operator Overloading 1

2 Operator overload In C++, operator can be overload according to class type that you define. Operator overload is closely meaning to function overload, To overload operator, you must define what the operation means relative to the class to which it is applied. To do this, you create an operator function, which defines the action of the operator. Operator function can be either member or non-member of the class. Non- member operator function is often friend function. 2

3 Operator overload Syntax: Type classname::operator#(arg-list) { operation relative to the class } When you use a member function, no parameters are used when overloading a unary operator, and only one parameter is required when overloading a binary operator. Nonmember binary operator functions have two parameters. Nonmember unary operator functions have one parameter. 3

4 Operator overloading using member functions (binary operator) #include Using namespace std; Class Three_d { int x,y,z; Public: Three_d() {x=y=z=0;} Three_d( int a, int b, intc) { x=a; y=b; z=c;} Three_d operator+ (three_d op2); Three_d operator = (three_d op2); Three_d operator ++ (); Void show(); } 4

5 continue // overload operator + Three_d Three_d::operator+ (three_d op2) { Three_d temp; temp.x=x+op2.x; temp.y=y+op2.y; temp.z=z+op2.z; Return temp; } 5

6 continue // overload assignment = Three_d Three_d::operator = (three_d op2) { x= op2.x; y= op2.y; z= op2.z; return *this; } 6

7 continue Three_d ::show() { cout<<“x=“<<x; cout<<“y=“<<y; cout<<“z=“<<z<<endl; } Int main() { Three_d a(1,2,3), b(10,10,10), c; a.show(); b.show(); c=a+b; c.show(); c=a+b+c; c.show(); c=b=a; b.show(); c.show(); Retuen 0; } 7 X=1 y=2 z=3 X=10 y=10 z=10 X=11 y=12 z=13 X=22 y=24 z=26 X=1 y=2 z=3

8 Using member function to overload unary operators // overload the prefix of ++ Three_d Three_d::operator ++ () { x++; y++; z++; return *this; } Int main() // main program { Three_d a(1,2,3); ++a; a.show(); return 0; } // overload the postfix of ++ Three_d Three_d::operator ++ (int notused) /* not used is an integer variable not used by a function but it is a way so compiler can distinguish between prefix and postfix */ { Three_d temp= *this; /* save the original value of object x++; y++; z++; return temp; // return original value } 8

9 Nonmember operators functions binary operators #include Using namespace std; Class Three_d { int x,y,z; Public: Three_d() {x=y=z=0;} Three_d( int a, int b, intc) { x=a; y=b; z=c;} friend Three_d operator+(Three_d op1, Three_d op2); }; // nonmember operator function // this now is a friend function Three_d operator+(Three_d op1, Three_d op2) { Three_d temp; temp.x=op1.x+op2.x; temp.y=op1.y+op2.y; temp.z=op1.z+op2.z; return temp; } 9

10 Nonmember operators functions unary operators If you want to use a friend function to overload the increment or decrement operators, you must pass the object to the function as a reference parameter (& ob). Changes made to the parameter (& ob) will affect the object in the main program. The prefix increment form will take one parameter (which is the object), the postfix will take two parameters (the object and the notused integer). 10

11 Overloading the Relational and Logical Operators An overloaded relational or logical operator returns a true or false value. example: // overload logical == bool Three_d::operator==(Three_d op2) { if (x==op2.x)&&(y==op2.y)&&(z==op2.z) return true; else return false; } 11


Download ppt "Chapter 13 Operator Overloading 1. Operator overload In C++, operator can be overload according to class type that you define. Operator overload is closely."

Similar presentations


Ads by Google