480 likes | 931 Views
Data Structures Using C . 2. Chapter Objectives. Learn about the pointer data type and pointer variablesExplore how to declare and manipulate the pointer variablesLearn about the address of operator and dereferencing operatorDiscover dynamic variablesExamine how to use the new and delete operators to manipulate dynamic variables.
E N D
1: Data Structures Using C++ 1 Chapter 3 Pointers and Array-Based Lists
2: Data Structures Using C++ 2
3: Data Structures Using C++ 3 Chapter Objectives Learn about pointer arithmetic
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
4: Data Structures Using C++ 4 Pointer Data Types and Pointer Variables Pointer variable: variable whose content is a memory address
Syntax to declare pointer variable:
dataType *identifier;
Address of operator: Ampersand, &
Dereferencing operator: Asterisk, *
5: Data Structures Using C++ 5 Pointers
Statements:
int *p;
int num;
6: Data Structures Using C++ 6 Pointers
7: Data Structures Using C++ 7 Pointers
8: Data Structures Using C++ 8 Pointers
9: Data Structures Using C++ 9 Pointers Summary of preceding diagrams
&p, p, and *p all have different meanings
&p means the address of p
p means the content of p
*p means the content pointed to by p, that is pointed to by the content of memory location
10: Data Structures Using C++ 10 Pointers
11: Data Structures Using C++ 11 Pointers
12: Data Structures Using C++ 12 Pointers
13: Data Structures Using C++ 13 Classes, structs, and Pointer Variables The syntax for accessing a class (struct) member using the operator -> is
pointerVariableName->classMemberName
Therefore, the statement
(*studentPtr).gpa = 3.9;
is equivalent to the statement
studentPtr->gpa = 3.9;
14: Data Structures Using C++ 14 Classes, structs, and Pointer Variables
15: Data Structures Using C++ 15 Classes, structs, and Pointer Variables
16: Data Structures Using C++ 16 Syntax to use operator new new dataType; //to allocate a single variable
new dataType[intExp]; //to allocate an array of variables
17: Data Structures Using C++ 17 Syntax to use operator delete delete pointer; //to destroy a single dynamic variable
delete [] pointer; //to destroy a dynamically created array
18: Data Structures Using C++ 18 Operations on pointer variables Assignment operations
Relational operations
Limited arithmetic operations
19: Data Structures Using C++ 19 Functions and Pointers void example(int* &p, double *q)
{
.
.
.
}
20: Data Structures Using C++ 20 Pointers and Function Return Pointers int* testExp(...)
{
.
.
.
}
is a pointer of the type int.
21: Data Structures Using C++ 21 Shallow Versus Deep Copy and Pointers
22: Data Structures Using C++ 22 Shallow Versus Deep Copy and Pointers
23: Data Structures Using C++ 23 Shallow Versus Deep Copy and Pointers second = new int[10];
for(int j = 0; j < 10; j++)
second[j] = first[j];
24: Data Structures Using C++ 24 Classes and Pointers: Some Peculiarities class pointerDataClass
{
public:
...
private:
int x;
int lenP;
int *p;
};
pointerDataClass objectOne;
pointerDataClass objectTwo;
25: Data Structures Using C++ 25 Classes and Pointers: Some Peculiarities
26: Data Structures Using C++ 26 Destructor
27: Data Structures Using C++ 27 Destructor pointerDataClass::~pointerDataClass()
{
delete [ ] p;
}
class pointerDataClass
{
public:
~pointerDataClass();
...
private:
int x;
int lenP;
int *p;
};
28: Data Structures Using C++ 28 Assignment Operator
29: Data Structures Using C++ 29 Assignment Operator
30: Data Structures Using C++ 30 Assignment Operator
31: Data Structures Using C++ 31 Overloading the Assignment Operator Function Prototype (to be included in the definition of the class):
const className& operator=(const className&);
Function Definition:
const className& className::operator=(const className& rightObject)
{
//local declaration, if any
if(this != &rightObject) //avoid self-assignment
{
//algorithm to copy rightObject into this object
}
//return the object assigned
return *this;
}
32: Data Structures Using C++ 32 Overloading the Assignment Operator Definition of function operator=:
Only one formal parameter
Formal parameter generally const reference to particular class
Return type of function is reference to particular class
33: Data Structures Using C++ 33 Copy Constructor
34: Data Structures Using C++ 34 Copy Constructor
35: Data Structures Using C++ 35 Copy Constructor
36: Data Structures Using C++ 36 Copy Constructor If a class has pointer data members:
During object declaration, the initialization of one object using the value of another object would lead to a shallow copying of the data if the default memberwise copying of data is allowed
If, as a parameter, an object is passed by value and the default member-wise copying of data is allowed, it would lead to a shallow copying of the data
37: Data Structures Using C++ 37 Copy Constructor The copy constructor automatically executes in the following situations
When an object is declared and initialized by using the value of another object
When, as a parameter, an object is passed by value
When the return value of a function is an object
38: Data Structures Using C++ 38 Copy Constructor
39: Data Structures Using C++ 39 Copy Constructor General syntax to include the copy constructor in the definition of a class:
className(const className& otherObject);
40: Data Structures Using C++ 40 Classes with Pointer Data Members Include destructor in the class
Overload assignment operator for class
Include copy constructor
41: Data Structures Using C++ 41 Overloading Array Index (Subscript) Operator ([ ]) Syntax to declare the operator function operator [ ] as a member of a class for nonconstant arrays:
Type& operator[](int index);
Syntax to declare the operator function operator [ ] as a member of a class for constant arrays:
const Type& operator[](int index) const;
42: Data Structures Using C++ 42 Array-Based Lists List: A collection of elements of the same type
Length of list is number of elements in list
43: Data Structures Using C++ 43 Operations performed on a list Create the list; initialized to an empty state
Determine whether the list is empty
Determine whether the list is full
Find the size of the list
Destroy, or clear, the list
Determine whether an item is the same as a given list element
44: Data Structures Using C++ 44 Operations performed on a list Insert an item in the list at the specified location
Remove an item from the list at the specified location
Replace an item at the specified location with another item
Retrieve an item from the list at the specified location
Search the list for a given item
45: Data Structures Using C++ 45 UML Diagram of the class arrayListType
46: Data Structures Using C++ 46 Time Complexity of List Operations
47: Data Structures Using C++ 47 Chapter Summary Pointer data types and variables
Dynamic variables
Pointer arithmetic
Dynamic arrays
Shallow and deep copying
Peculiarities of classes with pointer data members
Processing lists