CS148 Introduction to Programming II

Slides:



Advertisements
Similar presentations
Chapter 18 Vectors and Arrays
Advertisements

Chapter 18 Vectors and Arrays John Keyser’s Modification of Slides by Bjarne Stroustrup
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.
Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
Lecture 20: 11/12/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
Introduction to Programming Lecture 39. Copy Constructor.
Lecture 18: 4/11/2003CS148 Spring CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
Dynamically Allocated Arrays May 2, Quiz 5 Today.
1 Class Constructors a class constructor is a member function whose purpose is to initialize the private data members of a class object the name of a constructor.
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.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
Dale Roberts 1 Classes Constructors & Destructors Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer.
CS212: Object Oriented Analysis and Design Lecture 7: Arrays, Pointers and Dynamic Memory Allocation.
Copy Constructors Fall 2008 Dr. David A. Gaitros
1 Inside the Vector Class: with additional needed methods CPS212CPS212 Gordon College.
1 Chapter 15-2 Pointers, Dynamic Data, and Reference Types Dale/Weems.
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 Recall Definition of Stack l Logical (or ADT) level: A stack is an ordered group of homogeneous items (elements), in which the removal and addition of.
More C++ Features True object initialisation
1 Overview of C++ CS Data Structure. 2 Value parameters int abc (int a, int b, int c) // a, b, and c are the { // formal parameters a = a * 2; return.
What happens... l When a function is called that uses pass by value for a class object like our dynamically linked stack? StackType MakeEmpty Pop Push.
1 CSC241: Object Oriented Programming Lecture No 05.
Lecture 10: 2/17/2003CS148 Spring CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
1 // SPECIFICATION FILE (dynarray.h) // Safe integer array class allows run-time specification // of size, prevents indexes from going out of bounds, //
1 11/30/05CS150 Introduction to Computer Science 1 Structs.
CS162 - Topic #6 Lecture: Pointers and Dynamic Memory –Review –Dynamically allocating structures –Combining the notion of classes and pointers –Destructors.
Lecture 17: 4/4/2003CS148 Spring CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
Learners Support Publications Constructors and Destructors.
1 Ugly Realities The Dark Side of C++ Chapter 12.
Yan Shi CS/SE 2630 Lecture Notes
Constructors and Destructors
Pointers and Dynamic Arrays
Copy Constructor / Destructors Stacks and Queues
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
CISC181 Introduction to Computer Science Dr
Class: Special Topics Copy Constructors Static members Friends this
CS150 Introduction to Computer Science 1
Memberwise Assignment / Initialization
This pointer, Dynamic memory allocation, Constructors and Destructor
Dynamically Allocated Memory
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
CS148 Introduction to Programming II
Chapter 15 Pointers, Dynamic Data, and Reference Types
Chapter 15 Pointers, Dynamic Data, and Reference Types
Classes Copy Constructors
Department of Computer and Information Science, School of Science, IUPUI CSCI 265 Classes Dale Roberts, Lecturer Computer Science, IUPUI
CS148 Introduction to Programming II
Constructors and destructors
Constructors and Destructors
Chapter 15-3 Pointers, Dynamic Data, and Reference Types
9-10 Classes: A Deeper Look.
C++ Constructor Insanity CSE 333 Summer 2018
CS149D Elements of Computer Science
CS148 Introduction to Programming II
CS148 Introduction to Programming II
CS410 – Software Engineering Lecture #5: C++ Basics III
COP 3330 Object-oriented Programming in C++
CS149D Elements of Computer Science
Class: Special Topics 2 For classes using memory allocation
The Constructors Lecture 7 Fri, Feb 2, 2007.
CS148 Introduction to Programming II
CS148 Introduction to Programming II
Destructors, Copy Constructors & Copy Assignment Operators
Destructors, Copy Constructors & Copy Assignment Operators
CS148 Introduction to Programming II
CS 144 Advanced C++ Programming April 30 Class Meeting
9-10 Classes: A Deeper Look.
CS148 Introduction to Programming II
Classes Class Data Members & Initializers
Presentation transcript:

CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture 16: 3/24/2003 Lecture 16: 3/24/2003 CS148 Spring 2003

Outline Classes and dynamic data Use of destructors Shallow copy Deep copy using a deep copy function Deep copy using a copy-constructor Lecture 16: 3/24/2003 CS148 Spring 2003

Destructors and Dynamic Data 1/2 When a class instance allocates dynamic data on the free store (using new) , a destructor is used to properly deallocate the previously allocated data (using delete) Dynamically Allocates data pointed to by msgStr class Date { public: Date (int Mo, int Day, int Yr, const char* msgStr); void print(); ~Date(); //destructor invoked when a class object is destroyed private: int mo; int day; int yr; char* msg; }; Deallocates data pointed to by msgStr Lecture 16: 3/24/2003 CS148 Spring 2003

Destructors and Dynamic Data 2/2 When a class instance allocates dynamic data on the free store (using new in a constructor) , a destructor is used to properly deallocate the previously allocated data (using delete) 4 msg Free Store ‘X’ ‘y’ ‘Z’ ‘\0’ 15 2001 Date::Date(int Mo, int Day, int Yr, const char* msgStr) { … //allocate space for msg msg = new char[strlen(msgStr) + 1]; //copy the contents of msgStr into msg strcpy (msg,msgStr); } Date::~Date() delete [] msg; void main () //create an instance of class Date Date date1 (4,15,2001,“XyZ"); Free Store After the call to destructor Lecture 16: 3/24/2003 CS148 Spring 2003

Shallow copy When using the built in assignment operator with class objects a shallow copy is performed. A shallow copy copies one class object to another without copying any pointed-to data Free Store ‘X’ ‘y’ ‘Z’ ‘\0’ 4 15 2001 5 16 2002 ‘A’ ‘B’ ‘C’ ‘D’ void main () { //create an instance of class Date Date date1 (4,15,2001,“XyZ"); Date date2 (5,16,2002,”ABCD”); date1 = date2; } Free Store ‘X’ ‘y’ ‘Z’ ‘\0’ 5 16 2002 ‘A’ ‘B’ ‘C’ ‘D’ What happens when the destructor is invoked when main function is exited? Lecture 16: 3/24/2003 CS148 Spring 2003

Deep copy using a deep copy function 1/2 A deep copy is an operation that not only copies one class object to another but also makes copies of any pointed-to data Instead of built-in assignment operator use a deep copy function //deep copy function void Date::copyFrom (Date otherDate) { mo = otherDate.mo; day = otherDate.day; yr = otherDate.yr; delete []msg; msg = new char[strlen(otherDate.msg)+1]; strcpy(msg,otherDate.msg); } void main () //create an instance of class Date Date date1 (4,15,2001,“XyZ"); Date date2 (5,16,2002,”ABCD”); date1.copyFrom(date2); What happens when date2 is passed by value to copyFrom? What happens when copyFrom is exited? What happens when the destructor is called upon main function exit? Lecture 16: 3/24/2003 CS148 Spring 2003

Deep copy using a deep copy function 2/2 date1.copyFrom(date2); By default, date2 is copied to the copyFrom function using a shallow copy When copyFrom is exiting, date2 copy is destroyed prompting a call to the destructor The destructor deallocates the original pointed-to char array Free Store ‘X’ ‘y’ ‘Z’ ‘\0’ 4 15 2001 5 16 2002 ‘A’ ‘B’ ‘C’ ‘D’ main function 5 16 2002 copyFrom function Copy of date2 Created using a shallow copy Lecture 16: 3/24/2003 CS148 Spring 2003

Deep copy using a copy-constructor Include a copy-constructor Date (const Date & otherDate); The copy constructor will be invoked when Passing a copy of an argument to a parameter (pass by value) Initialization in a variable declaration Date date3 = date2; Returning an object as the value of a function return someObject; //copy constructor Date::Date (const Date& otherDate) { mo = otherDate.mo; day = otherDate.day; yr = otherDate.yr; msg = new char[strlen(otherDate.msg)+1]; strcpy(msg,otherDate.msg); } void main () //create an instance of class Date Date date1 (4,15,2001,“XyZ"); Date date2 (5,16,2002,”ABCD”); date1.copyFrom(date2); The date2 copy will be created by invoking the copy-constructor on date2 Lecture 16: 3/24/2003 CS148 Spring 2003

Deep copy using a copy-constructor 2/2 date1.copyFrom(date2); //in the presence of a copy-constructor Free Store ‘X’ ‘y’ ‘Z’ ‘\0’ ‘A’ ‘B’ ‘C’ ‘D’ main function 4 15 2001 5 16 2002 When copyFrom is exiting, date2 copy is destroyed prompting a call to the destructor The destructor deallocates the dynamic char array created using the copy-constructor leaving the one pointed to by date2.msg intact ‘A’ ‘B’ ‘C’ ‘D’ ‘\0’ 5 16 2002 copyFrom function Copy of date2 created using the copy-constructor Lecture 16: 3/24/2003 CS148 Spring 2003

Summary When dealing with classes and dynamic data provide the following functions A destructor to deallocate dynamic data A deep copy function that copies pointed-to data in addition to class object members A copy-constructor to guarantee deep copying of class objects If deep copying is not provided, the default is shallow copying which does not copy pointed-to data Lecture 16: 3/24/2003 CS148 Spring 2003