Overloading Binary Operators Two ways to overload –As a member function of a class –As a friend...

35
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++ 2E 1

Transcript of Overloading Binary Operators Two ways to overload –As a member function of a class –As a friend...

Page 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.

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++ 2E 1

Page 2: 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.

Data Structures Using C++ 2E 2

Overloading Binary Operators (cont’d.)

• As member functions (cont’d.)– Function definition

Page 3: 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.

Data Structures Using C++ 2E 3

Overloading Binary Operators (cont’d.)

• As nonmember functions– General syntax

Page 4: 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.

Data Structures Using C++ 2E 4

Overloading Binary Operators (cont’d.)

• As nonmember functions (cont’d.)– Function definition

Page 5: 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.

Data Structures Using C++ 2E 5

Overloading the Stream Insertion (<<) and Extraction (>>) 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

Page 6: 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.

Data Structures Using C++ 2E 6

Overloading the Stream Insertion (<<) and Extraction (>>) Operators (cont’d.)

• Overloading the stream extraction operator (>>)– General syntax and function definition

Page 7: 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.

Data Structures Using C++ 2E 7

Overloading the Stream Insertion (<<) and Extraction (>>) 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

Page 8: 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.

Data Structures Using C++ 2E 8

Operator Overloading: Member VersusNonmember

• 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

Page 9: 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.

Examples

Example 2-5

Example 2-6

On your own:

Examine CLOSELY the programming example on pg103 – Complex Numbers

Page 10: 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.

Data Structures Using C++ 2E 10

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

Page 11: 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.

Data Structures Using C++ 2E 11

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

Page 12: 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.

Data Structures Using C++ 2E 12

Function Templates

• Simplifies process of overloading functions• Syntax and example

Page 13: 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.

Data Structures Using C++ 2E 13

• 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

Page 14: 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.

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++ 2E 14

Page 15: 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.

Data Structures Using C++ 2E 15

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

Page 16: 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.

Data Structures Using C++ 2E 16

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

Page 17: 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.

Data Structures Using C++ 2E

Chapter 3Pointers and Array-Based Lists

Page 18: 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.

Data Structures Using C++ 2E 18

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

Page 19: 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.

Data Structures Using C++ 2E 19

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

Page 20: 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.

Data Structures Using C++ 2E 20

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)

Page 21: 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.

Data Structures Using C++ 2E 21

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

Page 22: 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.

Data Structures Using C++ 2E 22

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

Page 23: 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.

Data Structures Using C++ 2E 23

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

Page 24: 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.

Data Structures Using C++ 2E 24

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

Page 25: 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.

Data Structures Using C++ 2E 25

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

Page 26: 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.

Data Structures Using C++ 2E 26

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

Page 27: 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.

Data Structures Using C++ 2E 27

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

Page 28: 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.

Data Structures Using C++ 2E 28

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

Page 29: 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.

Data Structures Using C++ 2E 29

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

Page 30: 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.

Data Structures Using C++ 2E 30

The Pointer Data Type and Pointer Variables (cont’d.)

• Dynamic two-dimensional arrays– Creation

– What's the problem? Is it really dynamic?

Page 31: 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.

Data Structures Using C++ 2E 31

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

Page 32: 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.

Data Structures Using C++ 2E 32

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

Page 33: 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.

Data Structures Using C++ 2E 33

• 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 statementsecond = first; executes

FIGURE 3-18 first and second after the statementdelete [] second; executes

Page 34: 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.

Question

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

Page 35: 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.

Data Structures Using C++ 2E 35

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