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 = ”< { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/12/3387070/slides/slide_3.jpg", "name": "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 = <

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 = ”< { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/12/3387070/slides/slide_6.jpg", "name": "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 = <

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 = ”< { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/12/3387070/slides/slide_8.jpg", "name": "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 = <

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<=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< { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/12/3387070/slides/slide_10.jpg", "name": "Cont’d int main() { Distance dist1, dist3; dist1.getdist(); Distance dist2(11,6.25); dist3 = dist1 + dist2; cout<

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< { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/12/3387070/slides/slide_12.jpg", "name": "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; Cout >inches; } void showdist() { cout<

13 Cont’d int main() { Distance dist1; dist1.getdist(); Distance dist2(6,2.5); cout< { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/12/3387070/slides/slide_13.jpg", "name": "Cont’d int main() { Distance dist1; dist1.getdist(); Distance dist2(6,2.5); cout<

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