180 likes | 500 Views
Operator Overloading. Chapter 8. Operator Overloading. Operator overloading gives the opportunity to redefine C++ Operator overloading refers to redefine C++ operators such as +,<,>, additional meanings when they are applied to user defined data types a = b + c d3.addobjects(d1 , d2)
E N D
Operator Overloading Chapter 8
Operator Overloading • Operator overloading gives the opportunity to redefine C++ • Operator overloading refers to redefine C++ operators such as +,<,>, additional meanings when they are applied to user defined data types a = b + c d3.addobjects(d1 , d2) d3 = d1.addobject(d2); d3 = d1 +d2
Overloading Unary Operator class counter { private: int count; // count public: counter(){count = 0;} // constructor intget_count() // return count {return count;} void operator ++(){ ++count;} // increment prefix }; void main() { counter c1, c2 // define and initialization cout<<“c1 = ”<<c1.get_count(); cout<<“c2= ”<<c2.get_count(); ++c1; ++c2; ++c2; cout<<“c1 = ”<<c1.get_count(); cout<<“c2= ”<<c2.get_count(); } Output C1=0 C2=0 C1 =1 C2=2
Cont’d • Operator key word is use to overload the ++ operator void operator ++ () • Compiler distinguish overloaded operator by looking at data type of their operand • ++ operator increments the count data in the object • Operator++() can return a variable of type object for which it is invoked like simple functions c = ++c2;
Example- return class counter { private: intcount; // count public: counter(){count = 0;} intget_count() { return count;} counter operator ++(){ ++count; counter temp; temp.count= count; return temp;} }; void main() { // define and initialization counter c1, c2 cout<<“c1 = ”<<c1.get_count(); cout<<“c2= ”<<c2.get_count(); ++c1; // c1=1; c2=++c1; //c1=2, c2=2 cout<<“c1 = ”<<c1.get_count(); cout<<“c2= ”<<c2.get_count(); }
Nameless Temporary Objects • Make temporary object and gives it same value and return it counter operator ++(){ ++count; counter temp; temp.count= count; return temp;} OR counter operator ++() { ++count; return Counter(count); // must have constructor with single argument }
Postfix notation class counter { private: unsigned int count; Public: counter(){count = 0;} counter(int c){count = c;} unsigned intget_count() {return count;} counter operator ++() {return counter(++count);} counter operator ++(int) {return counter(count++); } }; void main() { counter c1, c2 ; cout<<“c1 = ”<<c1.get_count(); cout<<“c2= ”<<c2.get_count(); ++c1; // c1=1; c2=++c1; //c1=2, c2=2 cout<<“c1 = ”<<c1.get_count(); cout<<“c2= ”<<c2.get_count(); c2=c1++; //c1=3, c2=2 cout<<“c1 = ”<<c1.get_count(); cout<<“c2= ”<<c2.get_count(); }
Overloading Binary Operators-Arithmetic Operator (+) class Distance { private: int feet; float inches; public: Distance(){feet=0;inches=0;} Distance(intft, float in){ feet=ft; inches =in;} void getdist() {cout<<“Enter feet”;cin>>feet; cout<<“Enter inches”;cin>>inches; } void showdist(){ cout<<feet<<inches;} Distance operator + (Distance); }; Distance Distance:: operator + (Distance d2) { int f = feet + d2.feet; float i = inches +d2.inches; if(i>=12){i-=12.0;f++;} return Distance(f,i); }
Cont’d Output Enter feet: 10 Enter inches: 6.5 Dist1 = 10 – 6.5 Dist2=11 – 6.25 Dist3 = 22 - 0.75 int main() { Distance dist1, dist3; dist1.getdist(); Distance dist2(11,6.25); dist3 = dist1 + dist2; cout<<dist1.showdist(); cout<<dist2.showdist(); cout<<dist3.showdist(); }
Cont’d • The argument on the left side of operator is the object (dist1) of which operator is member • The object on the right side of the operator(dist2) is argument to the operator
Comparison operator class Distance { private: int feet; float inches; public: Distance(){feet=0;inches=0;} Distance(intft, float in){ feet=ft; inches =in;} void getdist() {cout<<“Enter feet”;cin>>feet; Cout<<“Enter inches”;cin>>inches; } void showdist(){ cout<<feet<<inches;} bool operator < (Distance); }; bool Distance:: operator < (Distance d2) { float bf1= feet +inches/12; Float bf2= d2.feet + d2.inches/12} return (bf1<bf2)?true:false; }
Cont’d Output Enter feet: 5 Enter inches: 11.5 Dist1 = 5 – 11.5 Dist2=6 – 2.5 dist1 is less than dist2 int main() { Distance dist1; dist1.getdist(); Distance dist2(6,2.5); cout<<dist1.showdist(); cout<<dist2.showdist(); If(dist1 <dist2) cout<<“dist1 Less than dist2”; else cout<<“dist1 greater than dist2”; return 0; }
Data conversion • Conversion between basic types (Implicit conversion) e.g. intvar = floatvar; • Compiler do automatically by calling built in routines • Explicit conversion called casting and is also done by calling same built in routines intvar = static_cast<int>(floatvar); • From basic to user-defined Distance(float meters) { float fltfeet= MTF *meters; feet = int(fltfeet); inches = 2 * (fltfeet –feet) } Distance dist1 = 2.35 // Convert meters to Distance
Cont’d • From user-Defined to Basic using conversion operator Operator float() { flat fracfeet = inches/12; fracfeet +=float(feet); return fracfeet/MFT; } • mtrs = static_cast<float>(dist1); OR mtrs = dist2;