chapter 3 n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Chapter 3 PowerPoint Presentation
Download Presentation
Chapter 3

Loading in 2 Seconds...

play fullscreen
1 / 47

Chapter 3 - PowerPoint PPT Presentation


  • 104 Views
  • Uploaded on

Chapter 3. Pointers and Array-Based Lists. Chapter Objectives. Learn about the pointer data type and pointer variables Explore how to declare and manipulate the pointer variables Learn about the address of operator and dereferencing operator Discover dynamic variables

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Chapter 3' - huslu


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
chapter 3

Chapter 3

Pointers and Array-Based Lists

Data Structures Using C++

chapter objectives
Chapter Objectives
  • Learn about the pointer data type and pointer variables
  • Explore how to declare and manipulate the 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

Data Structures Using C++

chapter objectives1
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

Data Structures Using C++

pointer data types and pointer variables
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, *

Data Structures Using C++

pointers
Pointers
  • Statements:

int *p;

int num;

Data Structures Using C++

pointers1
Pointers

Data Structures Using C++

pointers2
Pointers

Data Structures Using C++

pointers3
Pointers

Data Structures Using C++

pointers4
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

Data Structures Using C++

pointers5
Pointers

Data Structures Using C++

pointers6
Pointers

x = 50;

p = &x;

Data Structures Using C++

pointers7
Pointers

*p = 38;

Data Structures Using C++

classes structs and pointer variables
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;

Data Structures Using C++

syntax to use operator new
Syntax to use operator new

new dataType; //to allocate a single variable

new dataType[intExp]; //to allocate an array of variables

Data Structures Using C++

syntax to use operator delete
Syntax to use operator delete

delete pointer; //to destroy a single dynamic variable

delete [] pointer; //to destroy a dynamically created array

Data Structures Using C++

operations on pointer variables
Operations on pointer variables
  • Assignment operations
  • Relational operations
  • Limited arithmetic operations

Data Structures Using C++

functions and pointers
Functions and Pointers

void example(int* &p, double *q)

{

.

.

.

}

Data Structures Using C++

pointers and function return pointers
Pointers and Function Return Pointers

int* testExp(...)

{

.

.

.

}

is a pointer of the type int.

Data Structures Using C++

shallow versus deep copy and pointers
Shallow Versus Deep Copy and Pointers

Data Structures Using C++

shallow versus deep copy and pointers1
Shallow Versus Deep Copy and Pointers

Data Structures Using C++

shallow versus deep copy and pointers2
Shallow Versus Deep Copy and Pointers

second = new int[10];

for(int j = 0; j < 10; j++)

second[j] = first[j];

Data Structures Using C++

classes and pointers some peculiarities
Classes and Pointers: Some Peculiarities

class pointerDataClass

{

public:

...

private:

int x;

int lenP;

int *p;

};

pointerDataClass objectOne;

pointerDataClass objectTwo;

Data Structures Using C++

destructor
Destructor

Data Structures Using C++

destructor1
Destructor

pointerDataClass::~pointerDataClass()

{

delete [ ] p;

}

class pointerDataClass

{

public:

~pointerDataClass();

...

private:

int x;

int lenP;

int *p;

};

Data Structures Using C++

assignment operator
Assignment Operator

Data Structures Using C++

assignment operator1
Assignment Operator

Data Structures Using C++

assignment operator2
Assignment Operator

Data Structures Using C++

overloading the assignment operator
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;

}

Data Structures Using C++

overloading the assignment operator1
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

Data Structures Using C++

copy constructor
Copy Constructor

Data Structures Using C++

copy constructor1
Copy Constructor

Data Structures Using C++

copy constructor2
Copy Constructor

Data Structures Using C++

copy constructor3
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

Data Structures Using C++

copy constructor4
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

Data Structures Using C++

copy constructor5
Copy Constructor

Data Structures Using C++

copy constructor6
Copy Constructor

General syntax to include the copy constructor in the definition of a class:

className(const className& otherObject);

Data Structures Using C++

classes with pointer data members
Classes with Pointer Data Members
  • Include destructor in the class
  • Overload assignment operator for class
  • Include copy constructor

Data Structures Using C++

overloading array index subscript operator
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;

Data Structures Using C++

array based lists
Array-Based Lists
  • List: A collection of elements of the same type
  • Length of list is number of elements in list

Data Structures Using C++

operations performed on a list
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

Data Structures Using C++

operations performed on a list1
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

Data Structures Using C++

time complexity of list operations
Time Complexity of List Operations

Data Structures Using C++

chapter summary
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

Data Structures Using C++