Upload
primrose-hunt
View
216
Download
0
Tags:
Embed Size (px)
Citation preview
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
Data Structures Using C++ 2E 2
Overloading Binary Operators (cont’d.)
• As member functions (cont’d.)– Function definition
Data Structures Using C++ 2E 3
Overloading Binary Operators (cont’d.)
• As nonmember functions– General syntax
Data Structures Using C++ 2E 4
Overloading Binary Operators (cont’d.)
• As nonmember functions (cont’d.)– Function definition
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
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
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
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
Examples
Example 2-5
Example 2-6
On your own:
Examine CLOSELY the programming example on pg103 – Complex Numbers
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
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
Data Structures Using C++ 2E 12
Function Templates
• Simplifies process of overloading functions• Syntax and example
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
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
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
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
Data Structures Using C++ 2E
Chapter 3Pointers and Array-Based Lists
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
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
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)
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
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
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
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
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
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
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
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
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
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?
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
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
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
Question
• What happens after Fig 8-18 if the code tries to access first as a pointer?
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