Documente Academic
Documente Profesional
Documente Cultură
Pandhare
Operator Overloading:Redefining operator for user-defined data type is called as operator overloading. For built in data type we do not need to specify how a operator should behave, but if we want to used operator with our user defined data type then we have to specify how they should behave by defining special operator function. The general from of an operator function is return_type operator op (argList) { Function body; } We can overload all the c++ operators excepts the following. y y y y Class member access operators (. , .*); Scope resolution operator (::) Size operator (sizeof) Conditional operator (?:) // task defined.
There are two types of operators 1. Unary operator. 2. Binary operator. Overloading Unary operator:Unary operators can be overloaded by defining special operator function. Syntax:return_type operator op() { function body; } This operator function is automatically called whenever the operator is used with the object.
Example:#include<iostream.h> #include<conio.h> class Distance { private: int feet; int inches; public: Distance(int f,int i) { feet=f; inches=i; } void showdist() { cout<<feet<<"\'-"<<inches<<'\"'; } void operator ++() { inches=inches+1; if(inches==12) { feet=feet+1; inches=0; } }};
void main() { Distance a(5,10); a.showdist(); ++a; a.showdist(); ++a; a.showdist(); getch(); } Output:5 10 5 11 6 - 0
Overloading Binary operator:Binary operator can be overloaded by defining special operator function.
Syntax:#include<iostream.h> #include<conio.h> class complex { int real; int imag; public: complex() { real=0; imag=0; }
complex(int x,int y) { real=x; imag=y;} void operator +(complex c) { complex temp; temp.real=real+c.real; temp.imag=imag+c.imag; cout<<"addition is "<<temp; } void display() { cout<<real<<" +j "<<imag<<endl;} };
void main() { complex c1(2,5); complex c2(1,2); complex c3; c1+c2; cout<<"c1= ";c1.display(); cout<<"c2= ";c2.display(); cout<<"c3= ";c3.display(); getch(); } Output:2+j 5 1+j 2 3+j 7
Overloading relational operator:It always return a value true OR false so their return_type must be Boolean or Int. /* program for overloading equal to (==)operator.*/ #include<iostream.h> class circle { int R; circle( int r) { R=r } int operator ==(circle z) { if(R==z.R) return 1; else return 0; } }; void main() { circle a(5),b(7); if(a==b) cout<<Both circles are same<<endl; else cout<<Both circles are different<<endl; }
Type conversions for user defined data_type:Three types of situations might arise in the data conversion between incompatible types. 1. Conversion from basic type to class type. 2. Conversion from class type to basic type. 3. Conversion from one class type to another class type. I)Basic to Class type: To perform such type of operation our object must contain a constructor with single argument. This constructor is automatically called whenever a value is assigned to its objects and the value will be passed as argument to the constructor. Example: class Time { int hrs; int mins; public: Time(int t) { hrs = t/60; mins= t%60; } }; After this conversion the hrs member of T1will contain a value of 1 and mins member a value of 25 void main() { Time t1; int duration = 65; T1=duration; }
II)Class to Basic Type: To perform such type of conversion then our object must contain a special operator function. Syntax:operator data_type() { Function body; } This operator function is automatically called whenever a object is assigned to a variable of build in type. Example: #include<iostream.h> class Circle { int R; public: Circle(int r) { R=r; } operator int() { return R; } } } void main() { Circle a(5); int b; b=c; cout<<Radius is <<b; void area() { float a; a=3.14*R*R; cout<<Area is <<a;
III) Class to class type: To perform such type of operation, the object which is assigned must have an operator function. Syntax:operator data_type() { Function body; } Note:- And the object to which value is assigned must have a constructor with single argument. Example: #include<iostream.h> class Circle { int R; public: Circle(int r) { R=r; } operator int() { return R; } } }; void area() { float a; a=3.14*R*R; cout<<Area is <<a;
class sphere { int R; public: sphere(int r=1) { R=r; } void volume() { float v=(4*3.14*R*R*R)/3.0; cout<<volume is <<v; } };