Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 8. Operator Overloading Operator overloading gives the opportunity to redefine C++ Operator overloading refers to redefine C++ operators such.

Similar presentations


Presentation on theme: "Chapter 8. Operator Overloading Operator overloading gives the opportunity to redefine C++ Operator overloading refers to redefine C++ operators such."— Presentation transcript:

1 Chapter 8

2 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

3 Overloading Unary Operator class counter { private: int count; // count public: counter(){count = 0;} // constructor int get_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; cout<<“c1 = ”<<c1.get_count(); cout<<“c2= ”<<c2.get_count(); } Output C1=0 C2=0 C1 =1 C2=2

4

5 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;

6 Example- return class counter { private: int count; // count public: counter(){count = 0;} int get_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(); }

7 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 }

8 Postfix notation class counter { private: unsigned int count; Public: counter(){count = 0;} counter(int c){count = c;} unsigned int get_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(); }

9 Overloading Binary Operators- Arithmetic Operator (+) class Distance { private: int feet; float inches; public: Distance(){feet=0;inches=0;} Distance(int ft, float in){ feet=ft; inches =in;} void getdist() {cout >feet; cout >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); }

10 Cont’d int main() { Distance dist1, dist3; dist1.getdist(); Distance dist2(11,6.25); dist3 = dist1 + dist2; cout<<dist1.showdist(); cout<<dist2.showdist(); cout<<dist3.showdist(); }

11 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

12 Comparison operator class Distance { private: int feet; float inches; public: Distance(){feet=0;inches=0;} Distance(int ft, float in){ feet=ft; inches =in;} void getdist() {cout >feet; Cout >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; }

13 Cont’d 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; }

14 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 (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

15 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 (dist1); OR mtrs = dist2;

16 Explicit -keyword Explicit is used in the declaration of constructors to indicate that conversion of an initializer should not take place. e.g. Distance dist1 = 2.35 class A { public: A(int);...}; A declaration like: A a1 = 37; or a(37) // says to call the A(int) constructor to create an A object from the integer value To disable such converting constructor use explicit keyword class A { public: explicit A(int);… };

17 Mutable - keyword C++ keyword declaring a member non-constant even if it is a member of a const object. class scrollbar{ private: int size; mutable string owner; … Public: scrollbar(int sz, string own){size =sz; owner=own;} void setSize(int sz){size = sz;} void setOwner (string own){owner= own;}... }; int main(){ const scrollbar sbar(60, “window1”); sbar.Setsize(100); // illegal sbar.setOwner(“Window2”); // this is OK


Download ppt "Chapter 8. Operator Overloading Operator overloading gives the opportunity to redefine C++ Operator overloading refers to redefine C++ operators such."

Similar presentations


Ads by Google