Presentation is loading. Please wait.

Presentation is loading. Please wait.

More on Operator Overloading CS-2303, C-Term 20101 More on Operator Overloading CS-2303 System Programming Concepts (Slides include materials from The.

Similar presentations


Presentation on theme: "More on Operator Overloading CS-2303, C-Term 20101 More on Operator Overloading CS-2303 System Programming Concepts (Slides include materials from The."— Presentation transcript:

1 More on Operator Overloading CS-2303, C-Term 20101 More on Operator Overloading CS-2303 System Programming Concepts (Slides include materials from The C Programming Language, 2 nd edition, by Kernighan and Ritchie, from C: How to Program, 5 th and 6 th editions, by Deitel and Deitel, and from The C++ Programming Language, 3 rd edition, by Bjarne Stroustrup)

2 More on Operator Overloading CS-2303, C-Term 20102 Case Study — Array Class Problems with pointer-based arrays in C++:– –No range checking. –Cannot be compared meaningfully with == –No array assignment (array names are const pointers). –If array passed to a function, size must be passed as a separate argument. Can we do better? –I.e., can we create a data type that looks like an array but has the missing properties

3 More on Operator Overloading CS-2303, C-Term 20103 Point of Operator Overloading By using C++ classes and operator overloading, … … one can significantly change the capabilities of a built-in type In this case, of the built-in array type

4 More on Operator Overloading CS-2303, C-Term 20104 Case Study — Array Class Implement an Array class with:– –Range checking –Array assignment ( = ) –Arrays that know their own size. –Array comparisons with == and != –Output/input of entire arrays with > Deitel & Deitel, §22.8

5 More on Operator Overloading CS-2303, C-Term 20105 Array Class Copy Constructor Used whenever copy of object is needed: –Passing by value (return value or parameter). –Initializing an object with a copy of another of same type. Array newArray( oldArray ); or Array newArray = oldArray; –Both are identical –newArray is a copy of oldArray.

6 More on Operator Overloading CS-2303, C-Term 20106 Definition – Copy Constructor A constructor of some type T of the form T (const & T); The single argument must be a const reference to an existing object of same type Creates a duplicate of the existing object Used whenever a copy of an object is needed Including arguments to functions, results returned from functions Copy constructors always take reference argument! Reason: avoid infinite recursion!

7 More on Operator Overloading CS-2303, C-Term 20107 Most operators overloaded as member functions (except > which must be global functions) Prototype for copy constructor != operator simply returns opposite of == operator – only need to define the == operator Array Class Definition

8 More on Operator Overloading CS-2303, C-Term 20108 Array Class Definition Note weird return type:– constant reference Not explained in D&D

9 More on Operator Overloading CS-2303, C-Term 20109 Operators for accessing specific elements of Array object Note: An example of pointer data member Array Class Definition (continued)

10 More on Operator Overloading CS-2303, C-Term 201010 Array Class Implementation

11 More on Operator Overloading CS-2303, C-Term 201011 We must declare a new integer array so the objects do not point to the same memory Array Class Implementation (continued)

12 More on Operator Overloading CS-2303, C-Term 201012 Want to avoid self assignment This would be dangerous if this is the same Array as right Array Class Implementation (continued) Note that Copy Constructor and assignment are not the same thing. (But they are similar!)

13 More on Operator Overloading CS-2303, C-Term 201013 integers1[ 5 ] calls integers1.operator[]( 5 ) Array Class Implementation (continued)

14 More on Operator Overloading CS-2303, C-Term 201014 Array Class Implementation (continued) Should be some error checking here Notice the return type

15 More on Operator Overloading CS-2303, C-Term 201015 Array Class Implementation (continued) Notice the return type

16 More on Operator Overloading CS-2303, C-Term 201016 Retrieve number of elements in Array Use overloaded >> operator to input Array Class Usage

17 More on Operator Overloading CS-2303, C-Term 201017 Use overloaded << operator to output Use overloaded != operator to test for inequality Use copy constructor Use overloaded = operator to assign Array Class Usage (continued)

18 More on Operator Overloading CS-2303, C-Term 201018 Use overloaded == operator to test for equality Use overloaded [] operator to access individual integers, with range-checking Array Class Usage (continued)

19 More on Operator Overloading CS-2303, C-Term 201019 Questions?

20 More on Operator Overloading CS-2303, C-Term 201020 Note about Type Conversion Any single-argument constructor can be used as a type-conversion between the argument type and the object type May be invoked implicitly or explicitly Explict:– let a and b be complex a = b + complex(2.0) Implicit:– whenever compiler figures out it needs one type but has another a = b + 2.0/* NO overloaded ‘+’ operator for complex, real */

21 More on Operator Overloading CS-2303, C-Term 201021 Forcing explicit Type Conversion Sometimes, you don’t want compiler to make an implicit conversion Prefix constructor by explicit in type definition –Example:– (line 15 of Array class definition) explicit Array(int =10); –Suppresses use of this constructor for an implicit type conversion.

22 More on Operator Overloading CS-2303, C-Term 201022 Questions?


Download ppt "More on Operator Overloading CS-2303, C-Term 20101 More on Operator Overloading CS-2303 System Programming Concepts (Slides include materials from The."

Similar presentations


Ads by Google