Operator Overloading
2 C++ has the ability to assign special meaning to an operator, called operator overloading one operator can be used to carry out diff operations it is accomplished by a special fun called operator fun which describes the task Syntax data type operator opr(arg list) { function body; } opr operator being overloaded The keyword operator is the function name
3 Operator Overloading Operator function can be either Member function OR Friend function Member function has no argument for unary operation Member function has only one argument for binary operation Friend function will have one argument for unary operation Friend function will have two arguments for binary operation
4 Operator Overloading Overloading unary operator using member fun Eg class s4 { int real,imaginary; public: s4(int x,int y){real=x;imaginary=y;} void show(){cout<<“\n”<<real<<“ + i”<<imaginary;} void operator –() // operator function def { real=real+10; imaginary=imaginary+10;} }; void main() { s4 C(1,2); C.show(); -C; // calling operator fun C.show(); }
5 Operator Overloading Overloading binary operator using member fun Eg class comp { int real,imaginary; public: comp(){}//default con comp(int a,int b){real=a; imaginary=b;} void show(){cout<<“\n”<<real<<“ + i”<<imaginary;} comp operator +(comp c) { comp t; t.real=real+c.real; t.imaginary=imaginary+c.imaginary; return t;} }; Void main() { comp c1(1,2), c2(2,5),c3; c1.show(); c2.show(); c3=c1+c2; c3.show(); }
6 Operator Overloading Overloading unary operator ++ using member fun class fib { int a,b,c; public: fib(){a=-1,b=1,c=a+b;} void display(){cout<<" "<<c;} void operator ++() {a=b; b=c; c=a+b;} }; void main() { fib F; for(int i=1;i<=5;i++) { F.display(); F++; }
7 Operator Overloading Overloading unary operator -using friend fun class s4 { int real,imaginary; public: s4(){} s4(int x,int y){real=x;imaginary=y;} void show(){cout<<"\n"<<real<<" + i"<<imaginary;} friend s4 operator -(s4); // friend fun decla }; s4 operator -(s4 s) // operator function def (friend) { s.real=s.real+10; s.imaginary=s.imaginary+10; return s;} void main() { s4 C(1,2); C.show(); s4 k=-C; // calling operator fun k.show(); }
8 Operator Overloading void main() { clrscr(); s4 A,B(2,7),C(1,2); B.show(); C.show(); A=B-C; A.show(); getch(); } Overloading binary operator -using friend fun class s4 { int real,imaginary; public: s4(){} s4(int x,int y){real=x;imaginary=y;} void show() {cout<<"\n"<<real<<" + i"<<imaginary;} friend s4 operator -(s4,s4); }; s4 operator-(s4 A,s4 B) { s4 t; t.real=A.real+B.real; t.imaginary=A.imaginary+B.imaginary; return t; }
9 Operator Overloading Overloading binary operator + using member fun class str { char *s; public: str(){} str(char *p) { s=new char[strlen(p)+1]; strcpy(s,p); } void show() { cout<<endl<<s; } str operator + (str k) { str t; t.s=new char[strlen(s)+strlen(k.s)]; strcpy(t.s,s); strcat(t.s,k.s); return(t); } }; void main() { clrscr(); str s1,s2="popo",s3="peepe"; s2.show(); s3.show(); getch(); }
10 Operator Overloading Overloading binary operator >using friend fun class str { char *s; public: str(char *l){s=new char[strlen(l)+1];strcpy(s,l);} void show(){cout<<s;} friend int operator >(str,str); }; int operator>(str s1,str s2) { if(strlen(s1.s)>strlen(s2.s)) {return 1;} else{return 0;} } void main() { str A("ajith"),B("sajith"); if(A>B) {cout<<"\n big is";A.show();} else {cout<<"\n big is ";B.show();} getch(); }
11 Operator Overloading Overloading binary operator ==using member fun class str { char *s; public: str(char *p) { s=new char[strlen(p)+1]; strcpy(s,p); } void show() { cout<<endl<<s; } int operator==(str k) { if(strcmp(s,k.s)==0) { return(1); } else { return(0); } } }; void main() { clrscr(); str s2="popo",s3="popo"; s2.show(); s3.show(); if(s2==s3) cout<<"\nequal"; else cout<<"\nnot"; getch(); }
12 Operator Overloading Overloading binary operator =using memb fun class comp {int real,img; public: comp(int a,int b){real=a;img=b;} void show() { cout<<real<<"+ i "<<img; } comp(){} void operator=(comp k) {real=k.real; img=k.img; } }; void main() {comp k1(10,20),k2; cout<<"first complex no "; k1.show(); k2=k1; cout<<"\nsec complex no "; k2.show(); }
13 Operator Overloading Overloading The function call operator () using memb fun class comp {int real,img; public: comp(int a,int b){real=a;img=b;} void show() { cout<<real<<"+ i "<<img; } comp(){} comp operator()(comp k) { comp p; p.real=real+k.real; p.img=img+k.img; return(p); } }; void main() { comp k1(10,20),k2(2,4),k3; cout<<"first complex no "; k1.show(); cout<<"\nsec comples no "; k2.show(); k3=k2(k1); cout<<"\nresult complex no "; k3.show(); }
14 Operator Overloading Overloading subscript operator [] using memb fun class comp {int real,img; public: comp(int a,int b){real=a;img=b;} void show() { cout<<real<<"+ i "<<img; } comp(){} comp operator[](comp k) { comp p; p.real=real+k.real; p.img=img+k.img; return(p); } }; void main() { comp k1(10,20),k2(2,4),k3; cout<<"first complex no "; k1.show(); cout<<"\nsec comples no "; k2.show(); k3=k2[k1]; cout<<"\nresult complex no "; k3.show(); }
1) Only existing operators can be overloaded 2) Cannot able to create new operators 3) cannot change the basic meaning of an operator 4) overloaded operator must have at least one operand Rules for operator overloading
5) we cannot be overloaded the following operators.- Membership operator ::- scope resolution operator ?:- conditional operator sizeof- size operator 6) overloaded operators follow the syntax rules of original operators Rules for operator overloading
7) Unary opr, overloaded by means of a member function, take no arg and does not return values 8) Unary opr, overloaded by means of friend fun, takes one arg Rules for operator overloading
9) Binary opr overloaded through a member fun, take one arg 10) binary opr overloaded through a friend fun take two arg 11) the binary opr such as +, -, *, / must return a value