1 / 21

Operator Overloading

Operator Overloading . Examples. Class Point. Int main() { Point p1,p2; p1.getdata(); p2.getdata(); p1.putdata(); p2.putdata(); }. Class Point { int x,y,z ; Public: Void getdata () { cin >> x >> y >> z;} Void putdata ()

talbot
Télécharger la présentation

Operator Overloading

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Operator Overloading Examples Prepared By -- Prajakta Mulye

  2. Class Point Int main() { Point p1,p2; p1.getdata(); p2.getdata(); p1.putdata(); p2.putdata(); } Class Point { intx,y,z; Public: Void getdata() { cin >> x >> y >> z;} Void putdata() {cout << x << y << z;} }; Prepared By -- PrajaktaMulye

  3. Class Point Int main() { Point p1,p2,p3; p1.getdata(); p2.getdata(); p3 = p1 + p2; // Error p3.putdata(); } Class Point { intx,y,z; Public: Void getdata() { cin >> x >> y >> z;} Void putdata() {cout << x << y << z;} }; Prepared By -- PrajaktaMulye

  4. Class Point Point Point::operator +(Point &obj) { Point temp; temp.x = x + obj.x; temp.y = y + obj.y; temp.z = z + obj .z; return temp; } int main() { Point p1,p2,p3; p1.getdata(); p2.getdata(); p3 = p1 + p2; //will invoke operator +(point) p3.putdata(); } Class Point { intx,y,z; Public: Void getdata() { cin >> x >> y >> z;} Void putdata() {cout << x << y << z;} Point operator+(Point&obj); }; Prepared By -- PrajaktaMulye

  5. Class Point Point operator +(Point &c1,Point &c2) { Point temp; temp.x = c1.x + c2.x; temp.y = c1.y + c2.y; temp.z = c1.z + c2 .z; return temp; } int main() { Point p1,p2,p3; p1.getdata(); p2.getdata(); p3 = p1 + p2; //will invoke friend fn + p3.putdata(); } Class Point { intx,y,z; Public: Void getdata() { cin >> x >> y >> z;} Void putdata() {cout << x << y << z;} friend Point operator +(Point &,Point &); }; Prepared By -- PrajaktaMulye

  6. Class Point Point Point::operator *(Point &obj) { Point temp; temp.x = x * obj.x; temp.y = y * obj.y; temp.z = z * obj .z; return temp; } int main() { Point p1,p2,p3; p1.getdata(); p2.getdata(); p3 = p1 * p2; //will invoke operator * p3.putdata(); } Class Point { intx,y,z; Public: Void getdata() { cin >> x >> y >> z;} Void putdata() {cout << x << y << z;} Point operator *(Point&obj); }; Prepared By -- PrajaktaMulye

  7. Class Point Point Point::operator +(Point &obj) { Point temp; temp.x = x + obj.x; temp.y = y + obj.y; temp.z = z + obj .z; return temp; } int main() { Point p1,p2,p3; int a = 20; p1.getdata(); p2.getdata(); p3 = p1 + p2; //will invoke operator + p3 = p1 + 10; // Error p3 = p1 + a; // Error p3.putdata(); } Class Point { intx,y,z; Public: Void getdata() { cin >> x >> y >> z;} Void putdata() {cout << x << y << z;} Point operator+(Point&obj); }; Prepared By -- Prajakta Mulye

  8. Class Point Point Point::operator + (int n) { Point temp; temp.x = x + n; temp.y = y + n; temp.z = z + n; return temp; } int main() { Point p1,p2,p3; int a = 20; p1.getdata(); p2.getdata(); p3 = p1 + p2; //will invoke operator +(point) p3 = p1 + 10; //will invoke operator +(int) p3 = p1 + a; //will invoke operator +(int) p3.putdata(); } Class Point { intx,y,z; Public: Void getdata() { cin >> x >> y >> z;} Void putdata() {cout << x << y << z;} Point operator+(Point&obj); Point operator +(int ); }; Point Point::operator +(Point &obj) { Point temp; temp.x = x + obj.x; temp.y = y + obj.y; temp.z = z + obj .z; return temp; } Prepared By -- PrajaktaMulye

  9. Class Point Point Point::operator + (int n) { Point temp; temp.x = x + n; temp.y = y + n; temp.z = z + n; return temp; } int main() { Point p1,p2,p3; int a = 20; p1.getdata(); p2.getdata(); p3 = p1 + p2; //will invoke operator +(point) p3 = p1 + 10; // will invoke operator +(int) p3 = 10+ p1; //Error p3.putdata(); } Class Point { intx,y,z; Public: Void getdata() { cin >> x >> y >> z;} Void putdata() {cout << x << y << z;} Point operator+(Point&obj); Point operator +(int ); }; Point Point::operator +(Point &obj) { Point temp; temp.x = x + obj.x; temp.y = y + obj.y; temp.z = z + obj .z; return temp; } Prepared By -- PrajaktaMulye

  10. Point operator + (intn,Point &obj) { Point temp; temp.x = obj.x+ n; temp.y = obj.y+ n; temp.z = obj.z+ n; return temp; } int main() { Point p1,p2,p3; int a = 20; p1.getdata(); p2.getdata(); p3 = p1 + p2; //will invoke operator +(point) p3 = p1 + 10; // will invoke operator +(int) p3 = 10 + p1; //will invoke friend fn p3.putdata(); } Class Point { intx,y,z; Public: Void getdata() { cin >> x >> y >> z;} Void putdata() {cout << x << y << z;} Point operator+(Point&obj); Point operator +(int ); friend Point operator+(int , Point &obj); }; Point Point::operator +(Point &obj) { Point temp; temp.x = x + obj.x; temp.y = y + obj.y; temp.z = z + obj .z; return temp; } Point Point::operator + (int n) { Point temp; temp.x = x + n; temp.y = y + n; temp.z = z + n; return temp; } Prepared By -- Prajakta Mulye

  11. void point::operator – () { x = -x; y = -y; z = -z ; } int main() { Point p1; p1.getdata() -p1; //will invoke operator –() p3.putdata(); } /* Ovreloading unary –() */ Class Point { intx,y,z; Public: void getdata() { cin >> x >> y >> z;} Void putdata() {cout << x << y << z;} void operator –(); }; Prepared By -- PrajaktaMulye

  12. StringClass (Overloading of operator + ) Main() { String s1(“abc”),s2(“xyz”),s3; s3 = s1+s2; // will invoke operator +(string) } Class String { char str[100]; public: String() {str = ‘\0’;} String(char s[]) {strcpy(str,s);} String(const String &s) { strcpy(str,s.str);} String operator +(String &obj); }; String String::operator +(String &obj) { String temp; strcpy(temp.str,str); strcat(temp.str,obj.str); } Prepared By -- Prajakta Mulye

  13. StringClass(Overloading of operator ==) Main() { String s1(“abc”),s2(“xyz”),s3; If(s1==s2) // will invoke operator ==(string) { cout<<“\n Strings are same”; } else { cout<<“\nStrings are not same”; } } Class String { char str[100]; public: String() {str = ‘\0’;} String(char s[]) {strcpy(str,s);} String(const String &s) { strcpy(str,s.str);} intoperator ==(String &obj); }; int String::operator ==(String &obj) { if(strcmp(str,obj.str)) return 1; else return 0; } Prepared By -- Prajakta Mulye

  14. StringClass(Overloading of unary operator ! ) main() { String s1(“abc”),s2(“xyz”),s3; !s1; //will invoke operator !() s1.display(); return 0; } Class String { char str[100]; public: String() {str = ‘\0’;} String(char s[]) {strcpy(str,s);} String(const String &s) { strcpy(str,s.str);} void operator !(); void display() {cout<<end<<str;} }; void String::operator !() { intlen = strlen(str); for(inti = 0 ;i<len;i++) if(str[i] >=‘A’ && str[i] <=‘Z’) str[i] += 32; if(str[i]>=‘a’ && str[i] <= ‘z’) str[i] -= 32; } Prepared By -- PrajaktaMulye

  15. int String::operator > (String &obj) { int len1 = strlen(str); int len2 = strlen(obj.str); if(len1 > len2) return 1; else return 0; } int String::operator !=(String &obj) { if(strcmp(str,obj.str)) return 0; else return 1; } main() { String s1(“abc”),s2(“xyz”); if(s1 > s2) //will invoke operator >(string) cout<<“1st string is greater”; else cout<<“2nd string is greater”; If(s1 != s2) //will invoke operator !=(string ) cout<<“String not same”; else cout<<“Strings are same”; return 0; } StringClass(Overloading of operators > and != ) Class String { char *str; intlen; public: String() {len =0 ;str = new char[len+1]; str=“‘\0”;} String(char *s) {len= strlen(s); str= new char[len+1]; strcpy(str,s);} String(const String &obj) { len = strlen(obj.str) ; str= new char[len+1]; strcpy(str,obj.str);} intoperator>(String &obj); intoperator !=(String &obj); void display() {cout<<end<<str;} }; Prepared By -- PrajaktaMulye

  16. main() { clrscr(); number n1(10,20),n2; cout<<"\n Values after preincrement"; n2 = ++n1; //will invoke operator++() cout<<"\n Values for n1: "; n1.putdata(); cout<<"\n Values for n2: "; n2.putdata(); cout<<endl<<endl; cout<<"\n Values after postincrement"; n2 = n1++; //will invoke operator++(int) cout<<"\n Values for n1: "; n1.putdata(); cout<<"\n Values for n2: "; n2.putdata(); return 0; } Class number Overloading of unary operator ++ class number { inta,b; public: number() {a=0;b=0;} number(intx,int y) {a=x;b=y;} numberoperator++() { a++; b++; return *this; } number operator++(int x) { number temp; temp = *this; a++; b++; return temp; } void putdata() { cout<<endl<<a<<" "<<b;} }; Prepared By -- PrajaktaMulye

  17. main() { clrscr(); number n1(10,20),n2(2,3); cout<<"\n Values of n1 :"; n1.putdata(); cout<<"\n Values of n2 :"; n2.putdata(); n1 += n2; //will invoke operator +=(number) cout<<"\n Values of n1: "; n1.putdata(); cout<<"\n Values of n2 :"; n2.putdata(); } Overloading of unary operator += class number { inta,b; public: number() {a=0;b=0;} number(intx,int y) {a=x;b=y;} number operator+=(number &obj) { a=a+obj.a; b=b+obj.b; return *this; } void putdata() { cout<<endl<<a<<" "<<b;} }; Prepared By -- Prajakta Mulye

  18. Overload >> and << main() { clrscr(); time t1; cin>>t1; // will invoke operator >> cout<<t1; / will invoke operator << return 0; } class time { inthh,mm,ss; public: friend istream & operator >>(istream &,time &); friend ostream & operator <<(ostream &,time &); }; istream & operator >>(istream &din,time &obj) { din>>obj.hh>>obj.mm>>obj.ss; return(din); } ostream & operator <<(ostream &dout,time &obj) { dout<<obj.hh<<" : "<<obj.mm<<" : "<<obj.ss; return(dout); } Prepared By -- PrajaktaMulye

  19. Overload >> and << main() { clrscr(); vector obj; cin>>obj; // will invoke operator >> cout<<obj; // will invoke operator << return 0; } #idefine size 5 class vector { int a[size]; public: friend istream & operator >>(istream &,vector&); friend ostream & operator <<(ostream &,vector&); }; istream & operator >>(istream &din,vector &obj) { for(inti=0;i<size;i++) din>>obj.a[i]; return(din); } ostream & operator <<(ostream &dout,vector&obj) { for(inti=0;i<size;i++) dout<<endl<<obj.a[i]; return dout; } Prepared By -- PrajaktaMulye

  20. Overload >> and << main() { clrscr(); vector obj; cin>>obj; // will invoke operator >> cout<<obj; // will invoke operator << return 0; } #idefine size 5 class vector { int a[size]; public: friend voidoperator >>(istream &,vector&); friend void operator <<(ostream &,vector&); }; void operator >>(istream &din,vector &obj) { for(inti=0;i<size;i++) din>>obj.a[i]; } void operator <<(ostream &dout,vector&obj) { for(inti=0;i<size;i++) dout<<endl<<obj.a[i]; } Prepared By -- PrajaktaMulye

  21. Matrix Class Opeartor binary + & unary - matrix matrix::operator +(matrix &obj) { matrix temp; for(inti=0;i<3;i++) for(int j=0;j<3;j++) temp.a[i][j] = a[i][j] + obj.a[i][j]; return temp; } void matrix::operator -() { for(inti=0;i<3;i++) for(int j=0;j<3;j++) a[i][j] = - a[i][j]; } main() { matrix m1,m2,m3; cin>>m1; cin>>m2; m3 = m1+m2;//will invoke operator binary + cout<<m3; -m1; //will invoke operator unary - Cout<<m1; return 0; } class matrix { int a[3][3]; public: friend void operator >>(istream &,matrix &); friend void operator <<(ostream &,matrix &); matrix operator +(matrix &); void operator -(); }; void operator >>(istream &din,matrix &obj) { for(inti=0;i<3;i++) for(j=0;j<3;j++) din>>a[i][j]; } void operator <<(ostream &dout,matrix &obj) { for(inti=0;i<3;i++) { for(j=0;j<3;j++) dout<<a[i][j]<<" "; cout<<endl; } } Prepared By -- PrajaktaMulye

More Related