Presentation is loading. Please wait.

Presentation is loading. Please wait.

Overloading Binary Operators Two ways to overload –As a member function of a class –As a friend function As member functions –General syntax Data Structures.

Similar presentations


Presentation on theme: "Overloading Binary Operators Two ways to overload –As a member function of a class –As a friend function As member functions –General syntax Data Structures."— Presentation transcript:

1 Overloading Binary Operators Two ways to overload –As a member function of a class –As a friend function As member functions –General syntax Data Structures Using C++ 2E1

2 2 Overloading Binary Operators (cont’d.) As member functions (cont’d.) –Function definition

3 Data Structures Using C++ 2E3 Overloading Binary Operators (cont’d.) As nonmember functions –General syntax

4 Data Structures Using C++ 2E4 Overloading Binary Operators (cont’d.) As nonmember functions (cont’d.) –Function definition

5 Data Structures Using C++ 2E5 Overloading the Stream Insertion ( >) Operators Operator function overloading insertion operator and extraction operator for a class –Must be nonmember function of that class Overloading the stream extraction operator (>>) –General syntax

6 Data Structures Using C++ 2E6 Overloading the Stream Insertion ( >) Operators (cont’d.) Overloading the stream extraction operator (>>) –General syntax and function definition

7 Data Structures Using C++ 2E7 Overloading the Stream Insertion ( >) Operators (cont’d.) Overloading unary operations –Similar to process for overloading binary operators –Difference: unary operator has only one argument Process for overloading unary operators –If operator function is a member of the class: it has no parameters –If operator function is a nonmember ( friend function of the class): it has one parameter

8 Data Structures Using C++ 2E8 Operator Overloading: Member Versus Nonmember Certain operators can be overloaded as –Member functions or nonmember functions Example: binary arithmetic operator + –As a member function Operator + has direct access to data members Need to pass only one object as a parameter –As a nonmember function Must pass both objects as parameters Could require additional memory and computer time Recommendation for efficiency –Overload operators as member functions

9 Examples Example 2-5 Example 2-6 On your own: Examine CLOSELY the programming example on pg103 – Complex Numbers

10 Data Structures Using C++ 2E10 Function Overloading Creation of several functions with the same name –All must have different parameter set Parameter types determine which function to execute –Must give the definition of each function –Example: original code and modified code with function overloading

11 Data Structures Using C++ 2E11 Templates Function template –Writing a single code segment for a set of related functions Class template –Writing a single code segment for a set of related classes Syntax –Data types: parameters to templates

12 Data Structures Using C++ 2E12 Function Templates Simplifies process of overloading functions Syntax and example

13 Data Structures Using C++ 2E13 Used to write a single code segment for a set of related classes Called parameterized types –Specific class generated based on parameter type Syntax and example Class Templates

14 Header File and Implementation File of a Class Template Not possible to compile implementation file independently of client code Solution –Put class definition and definitions of the function templates directly in client code –Put class definition and definitions of the function templates together in same header file –Put class definition and definitions of the functions in separate files (as usual): include directive to implementation file at end of header file Data Structures Using C++ 2E14

15 Data Structures Using C++ 2E15 Summary Inheritance and composition –Ways to relate two or more classes –Single and multiple inheritance –Inheritance: an ‘‘is a’’ relationship –Composition: a ‘‘has a’’ relationship Private members of a base class are private to the base class –Derived class cannot directly access them Public members of a base class can be inherited either as public, protected, and private by the derived class

16 Data Structures Using C++ 2E16 Summary (cont’d.) Three basic principles of OOD –Encapsulation, inheritance, and polymorphism Operator overloading –Operator has different meanings with different data types –Operator function: function overloading an operator friend function: nonmember of a class Function name can be overloaded Templates –Write a single code segment for a set of related functions or classes

17 Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists

18 Data Structures Using C++ 2E18 Objectives Learn about the pointer data type and pointer variables Explore how to declare and manipulate pointer variables Learn about the address of operator and dereferencing operator Discover dynamic variables Examine how to use the new and delete operators to manipulate dynamic variables Learn about pointer arithmetic

19 Data Structures Using C++ 2E19 Objectives (cont’d.) Discover dynamic arrays Become aware of the shallow and deep copies of data Discover the peculiarities of classes with pointer data members Explore how dynamic arrays are used to process lists Learn about virtual functions Become aware of abstract classes

20 Data Structures Using C++ 2E20 The Pointer Data Type and Pointer Variables Pointer data types – Values are computer memory addresses – No associated name – Domain consists of addresses (memory locations) Pointer variable – Contains an address (memory address)

21 Data Structures Using C++ 2E21 The Pointer Data Type and Pointer Variables (cont’d.) Address of operator ( & ) – Unary operator – Returns address of its operand Dereferencing operator ( * ) – Unary operator Different from binary multiplication operator – Also known as indirection operator – Refers to object where the pointer points (operand of the * ) See bottom pg 133

22 Data Structures Using C++ 2E22 The Pointer Data Type and Pointer Variables (cont’d.) Pointers and classes – Dot operator (. ) Higher precedence than dereferencing operator ( * ) – Member access operator arrow ( -> ) Simplifies access of class or struct components via a pointer Consists of two consecutive symbols: hyphen and ‘‘greater than’’ symbol – Syntax pointerVariableName -> classMemberName

23 Data Structures Using C++ 2E23 The Pointer Data Type and Pointer Variables (cont’d.) Initializing pointer variables – No automatic variable initialization in C++ – Pointer variables must be initialized If not initialized, they do not point to anything – Initialized using Constant value 0 (null pointer) Named constant NULL – Number 0 Only number directly assignable to a pointer variable

24 Data Structures Using C++ 2E24 The Pointer Data Type and Pointer Variables (cont’d.) Dynamic variables – Variables created during program execution Real power of pointers – Two operators new : creates dynamic variables delete : destroys dynamic variables Reserved words

25 Data Structures Using C++ 2E25 The Pointer Data Type and Pointer Variables (cont’d.) Operator new – Allocates single variable – Allocates array of variables – Syntax new dataType; new dataType[intExp]; – Allocates memory (variable) of designated type Returns pointer to the memory (allocated memory address) Allocated memory: uninitialized

26 Data Structures Using C++ 2E26 The Pointer Data Type and Pointer Variables (cont’d.) Operator delete – Destroys dynamic variables – Syntax delete pointerVariable; delete [ ] pointerVariable; – See pgs 139-141 – Memory leak Memory space that cannot be reallocated – Dangling pointers Pointer variables containing addresses of deallocated memory spaces Avoid by setting deleted pointers to NULL after delete

27 Data Structures Using C++ 2E27 The Pointer Data Type and Pointer Variables (cont’d.) Operations on pointer variables – Operations allowed Assignment, relational operations; some limited arithmetic operations Can assign value of one pointer variable to another pointer variable of the same type Can compare two pointer variables for equality Can add and subtract integer values from pointer variable – Danger Accidentally accessing other variables’ memory locations and changing content without warning

28 Data Structures Using C++ 2E28 The Pointer Data Type and Pointer Variables (cont’d.) Dynamic arrays – Static array limitation Fixed size Not possible for same array to process different data sets of the same type – Solution Declare array large enough to process a variety of data sets Problem: potential memory waste – Dynamic array solution Prompt for array size during program execution

29 Data Structures Using C++ 2E29 The Pointer Data Type and Pointer Variables (cont’d.) Dynamic arrays (cont’d.) – Dynamic array An array created during program execution – Dynamic array creation Use new operator – Example int *p; p=new int[10]; See Example 3-4 pg 148

30 Data Structures Using C++ 2E30 The Pointer Data Type and Pointer Variables (cont’d.) Dynamic two-dimensional arrays – Creation – What's the problem? Is it really dynamic?

31 Data Structures Using C++ 2E31 The Pointer Data Type and Pointer Variables (cont’d.) Dynamic two-dimensional arrays (cont’d.) – Declare board to be a pointer to a pointer int **board; – Declare board to be an array of 10 rows and 15 columns To access board components, use array subscripting notation See Example 3-5 pg 151

32 Data Structures Using C++ 2E32 The Pointer Data Type and Pointer Variables (cont’d.) Shallow vs. deep copy and pointers – Pointer arithmetic may create unsuspected or erroneous results – Shallow copy Two or more pointers of same type Points to same memory Points to same data

33 Data Structures Using C++ 2E33 Shallow copy The Pointer Data Type and Pointer Variables (cont’d.) FIGURE 3-16 Pointer first and its array FIGURE 3-17 first and second after the statement second = first; executes FIGURE 3-18 first and second after the statement delete [] second; executes

34 Question What happens after Fig 8-18 if the code tries to access first as a pointer?

35 Data Structures Using C++ 2E35 The Pointer Data Type and Pointer Variables (cont’d.) Deep copy – Two or more pointers have their own data – See code at bottom of pg 154 FIGURE 3-19 first and second both pointing to their own data


Download ppt "Overloading Binary Operators Two ways to overload –As a member function of a class –As a friend function As member functions –General syntax Data Structures."

Similar presentations


Ads by Google