Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.

Slides:



Advertisements
Similar presentations
Data Structures Using C++ 2E
Advertisements

Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
Introduction to Programming Lecture 39. Copy Constructor.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
Chapter 14: Overloading and Templates C++ Programming: Program Design Including Data Structures, Fifth Edition.
Chapter 14: Overloading and Templates
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Copyright 2004 Scott/Jones Publishing Starting Out with C++: Early.
Dynamically Allocated Arrays May 2, Quiz 5 Today.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 13: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
Chapter 15: Operator Overloading
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 14: Overloading and Templates.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
CHAPTER 15 POINTERS, CLASSES, AND VIRTUAL FUNCTIONS.
Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 15: Overloading and Templates.
Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists.
Pointer Data Type and Pointer Variables
Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified by use by the MSU CMPS Dept. Chapter 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.
C++ Programming: From Problem Analysis to Program Design, Second Edition1 Objectives In this chapter you will: Learn about the pointer data type and pointer.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
Pointers and Dynamic Memory Allocation Copyright Kip Irvine 2003, all rights reserved. Revised 10/28/2003.
Dynamically Allocated Arrays December 4, Skip the Rest of this PowerPoint.
The Big Three Based on Weiss “Data Structures and algorithm Analysis CS240 Computer Science II.
1 CS 132 Spring 2008 Chapter 3 Pointers and Array-Based Lists read p
Data Structures Using C++ 2E Chapter 3 Pointers. Data Structures Using C++ 2E2 Objectives Learn about the pointer data type and pointer variables Explore.
Pointer and Array Lists Chapter 3, Summary CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
More C++ Features True object initialisation
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
CMSC 202, Version 3/02 1 Copy Constructors and Overloaded Assignment.
Data Structures Using C++1 Chapter 3 Pointers Dr. Liu.
Review 1 List Data Structure List operations List Implementation Array Linked List.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 14: Pointers.
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
Chapter 3 Pointers and Array-Based Lists Dr. Youssef Harrath
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
Pointers Lecture: 5. Topics 1 Pointers and the Address Operator 2 Pointer Variables 3 The Relationship Between Arrays and Pointers 4 Pointer Arithmetic.
You learned how to declare pointer variables how to store the address of a variable into a pointer variable of the same type as the variable how to manipulate.
Learning Objectives Pointers as dada members
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Memberwise Assignment / Initialization
Dynamically Allocated Memory
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
understanding memory usage by a c++ program
Chapter 15 Pointers, Dynamic Data, and Reference Types
Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes
Class: Special Topics 2 For classes using memory allocation
SPL – PS3 C++ Classes.
Presentation transcript:

Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists

Data Structures Using C++2 Chapter 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

Data Structures Using C++3 Chapter Objectives (cont’d) Learn about pointer arithmetic Discover dynamic arrays Become aware of 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++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: *

Data Structures Using C++5 Pointers Statements: int *p; int num;

Data Structures Using C++6 Pointers

Data Structures Using C++7 Pointers

Data Structures Using C++8 Pointers

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

Data Structures Using C++10 Pointers

Data Structures Using C++11 Pointers x = 50; p = &x; 50

Data Structures Using C++12 Pointers *p = 38;

Data Structures Using C++13 Classes, structs, and Pointer Variables The member access operator “. ” has higher precedence than “ * ”, so *studentPtr.gpa = 3.9; only works if studentPtr is an instance (not a pointer) and gpa is a pointer To dereference studentPtr and access its member gpa, use: (*studentPtr).gpa = 3.9; As a shortcut, use the operator “ -> ”: studentPtr->gpa = 3.9; In general, the syntax for accessing a class (struct) member via its pointer using the operator “ -> ” is pointerVariableName->classMemberName

Data Structures Using C++14 Classes, structs, and Pointer Variables

Data Structures Using C++15 Classes, structs, and Pointer Variables

Data Structures Using C++16 Dynamic Memory Not very interesting when pointers point only to static objects The real fun starts when we dynamically create objects and point pointers to them Allows us to create arrays of custom sizes, minimizing waste and eliminating compile- time constants Handled by commands new and delete

Data Structures Using C++17 Syntax to use operator new new dataType; //to allocate a single variable new dataType[intExp]; //to allocate an array of // variables int *p,*q; p = new int; // single int q = new int[16]; // size-16 array of ints

Data Structures Using C++18 Syntax to use operator delete delete pointer; //to destroy a single dynamic variable delete [] pointer; //to destroy a dynamically created array int *p,*q; p = new int; // single int q = new int[16]; // size-16 array of ints... delete p; // EVERY new MUST be delete [] q; // followed by a delete

Data Structures Using C++19 Operations on pointer variables int *p,*q; Assignment operations p = q; Relational operations p == q; p != q; Limited arithmetic operations p++; p+=5; // done in increments of size of type Array access p[10] = 47; //only valid after new

Data Structures Using C++20 Functions and Pointers void example(int* &p, double *q) {. } p is reference parameter (pointer to int ), q is value (pointer to double )

Data Structures Using C++21 Functions Returning Pointers int* testExp(...) {. } returns a pointer to type int.

Data Structures Using C++22 Shallow Versus Deep Copy and Pointers int *first, *second; first = new int[10]; (Assign values to first)

Data Structures Using C++23 Shallow Versus Deep Copy and Pointers second = first; delete [] second; Shallow copy!!!!!

Data Structures Using C++24 Shallow Versus Deep Copy and Pointers second = new int[10]; for(int j = 0; j < 10; j++) second[j] = first[j]; Deep copy!!!!! Now, deleting second has no effect on first

Data Structures Using C++25 Classes and Pointers class pointerDataClass { public:... private: int x; int lenP; int *p; }; pointerDataClass objectOne; pointerDataClass objectTwo;

Data Structures Using C++26 Classes and Pointers

Data Structures Using C++27 Classes and Pointers objectOne.p = new int[objectOne.lenP];

Data Structures Using C++28 Classes and Pointers What happens when objectOne goes out of scope? What happens to the array pointed to by p?  Memory leak! Need to free up this memory before the object disappears (preferably in the destructor) Again, EVERY new MUST be followed by a delete!

Data Structures Using C++29 Freeing Memory in the Destructor pointerDataClass::~pointerDataClass() { delete [ ] p; } class pointerDataClass { public: ~pointerDataClass();... private: int x; int lenP; int *p; };

Data Structures Using C++30 Assignment Operator

Data Structures Using C++31 Assignment Operator (cont’d) objectTwo = objectOne; //shallow copy The destructor of one deletes the memory for p of both!

Data Structures Using C++32 Assignment Operator (cont’d) How can we make objectTwo = objectOne; a deep copy?

Data Structures Using C++33 Overloading 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 { x = rightObject.x; lenP = rightObject.lenP; if (p != NULL) destroyList(); if (lenP > 0) { p = new int[lenP]; for (int i=0; i<lenP; i++) p[i] = rightObject.p[i]; } //return the object assigned return *this; }

Data Structures Using C++34 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++35 Copy Constructor Built-in constructor called when an object of same type used as parameter pointerDataClass objectThree(objectOne); //shallow copy

Data Structures Using C++36 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++37 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++38 Copy Constructor General syntax to include the copy constructor in the definition of a class: className(const className& otherObject); Function definition similar to overloaded assignment operator

Data Structures Using C++39 Classes with Pointer Data Members The “Big 3” things to remember to do: 1.Include destructor in the class 2.Overload assignment operator for class 3.Include copy constructor

Data Structures Using C++40 Overloading Index Operator ([ ]) Syntax to declare the operator function operator [ ] as a member of a class for nonconstant arrays: Type& operator[](int index); Class classTest {... private: Type *list; int arraySize; }; Type& classTest::operator[](int index) { assert(0 <= index && index < arraySize); return(list[index]); }

Data Structures Using C++41 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++42 In C++, the expression A[i] adds the offset i to address A and dereferences: *(A+i). What is A[i][j] the same as? 1.*(A+i+j) 2.**(A+i+j) 3.*(*(A+i)+j) 4.*(A+n*i+j), where n is the size of the first dimension of A

Data Structures Using C++43 Dynamically allocated multi-dimensional arrays are declared as pointers to pointers, e.g. class Array { private: int **A, n1, n2; public: Array(int,int); } What constructor is correct? 1.Array::Array(int s1,int s2) { n1=s1; n2=s2; A=new int[n1][n2]; } 2.Array::Array(int s1,int s2) { n1=s1; n2=s2; A=new int*[n1]; for (int i=0;i<n1;i++) A[i]=new int[n2]; } 3.Array::Array(int s1,int s2) { n1=s1; n2=s2; A=new int*[n1]; A[n1]=new int[n2]; }

Data Structures Using C++44 Recall the previous question’s implementation of a 2D array: class Array { private: int **A, n1, n2; public: Array(int,int); } I want an overloaded [] operator for this class. Specifically, if x is of type Array, I want to do things like “ cout << x[i][j] ” and “ x[i][j] = 7 ”. How would I declare such an operator in class Array ? 1.int *operator[](int); 2.int *& operator[](int); 3.int **operator[](int,int); 4.int operator[](int,int);