0% found this document useful (0 votes)
18 views5 pages

Operator Overloading

The document discusses operator overloading in C++ using unary and binary operators. It provides examples of overloading the increment operator (++) as a member function and non-member function. It also demonstrates overloading the addition operator (+) as a member function to add two complex number objects.

Uploaded by

Er Ashish Baheti
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views5 pages

Operator Overloading

The document discusses operator overloading in C++ using unary and binary operators. It provides examples of overloading the increment operator (++) as a member function and non-member function. It also demonstrates overloading the addition operator (+) as a member function to add two complex number objects.

Uploaded by

Er Ashish Baheti
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 5

Operator overloading using unary operator

#include<iostream.h> #include<conio.h> class score { private: int val; public: score() { val=0; } void operator++() { val=val+1; } int show() { return(val); } }; int main() { score s1,s2; cout<<"\n Initial value of s1 object = "<<s1.show(); cout<<"\n Initial value of s2 object = "<<s2.show(); ++s1; ++s1; ++s2; cout<<"\n Final value of s1 object = "<<s1.show(); cout<<"\n Final value of s2 object = "<<s2.show(); getch(); return 0; }

Returning object in unary operator overloading


#include<iostream.h> #include<conio.h> class score { private: int val; public: score() { val=0; } score operator++() { score temp; val=val+1; temp.val=val; return temp; } int show() { return(val); } }; int main() { score s1,s2; cout<<"\n Initial value of s1 object = "<<s1.show(); cout<<"\n Initial value of s2 object = "<<s2.show(); ++s1; s2=++s1; cout<<"\n Final value of object s1= "<<s1.show(); cout<<"\n Final value of object s2= "<<s2.show(); getch(); return 0; }

Overloading using binary operators


#include<iostream.h> #include<conio.h> class complex { private: double real,img; public: complex() { real=img=0.0; } void read() { cout<<"\n Enter real and imaginary part : "; cin>>real>>img; } complex operator+(complex cc2) { complex temp; temp.real=real+cc2.real; temp.img=img+cc2.img; return(temp); } } void show() { if(img>0) cout<<real<<"+"<<img<<"i"<<endl; else cout<<real<<img<<"i"<<endl; } }; int main() { complex c1,c2,c3; cout<<"\n Enter complex number c1 : "; c1.read(); cout<<"\n Enter complex number c2 : "; c2.read(); c3=c1+c2; c3.show(); getch(); return 0;

#include<iostream.h> #include<conio.h> class score { private: int val; public: score() { val=0; } int show() { return(val); } friend void operator++(score &); friend void operator++(score &,int); }; void operator++(score &s) { s.val=s.val+1; }

Unary operator overloading Using non member function


void operator++(score &s,int v) { s.val=s.val+1; } int main() { score s1,s2; cout<<"\n Initial value of s1 object = "<<s1.show(); cout<<"\n Initial value of s2 object = "<<s2.show(); ++s1; s2++; cout<<"\n Final value of s1 object = "<<s1.show(); cout<<"\n Final value of s2 object = "<<s2.show(); getch(); return 0; }

binary operator overloading Using non member function


#include<iostream.h> #include<conio.h> class complex { double real,img; public: complex() { real=0.0; img=0.0; } complex(double r,double i) { real=r; img=i; } void show() { if(img>0) cout<<real<<"+"<<img<<"i"<<endl; else cout<<real<<img<<"i"<<endl; } friend complex operator+(complex,complex); }; complex operator+(complex cc1,complex cc2) { complex temp; temp.real=cc1.real+cc2.real; temp.img=cc1.img+cc2.img; return(temp); } int main() { complex c3,c2(2.0,3.0),c1(1.5,2.5); c3=c1+c2; c3.show(); getch(); return 0; }

You might also like