Class: Special Topics 2 For classes using memory allocation

Slides:



Advertisements
Similar presentations
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.
Advertisements

Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
Introduction to Programming Lecture 39. Copy Constructor.
Object Oriented Programming COP3330 / CGS5409.  C++ Automatics ◦ Copy constructor () ◦ Assignment operator =  Shallow copy vs. Deep copy  DMA Review.
Copy Constructors Shallow Copy: –The data members of one object are copied into the data members of another object without taking any dynamic memory pointed.
CS 225 Lab #2 - Pointers, Copy Constructors, Destructors, and DDD.
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.
CS212: Object Oriented Analysis and Design Lecture 12: Operator Overloading-II.
CS212: Object Oriented Analysis and Design Lecture 10: Copy constructor.
CS212: Object Oriented Analysis and Design Lecture 7: Arrays, Pointers and Dynamic Memory Allocation.
Copy Constructors Fall 2008 Dr. David A. Gaitros
The Big Three Based on Weiss “Data Structures and algorithm Analysis CS240 Computer Science II.
Data Structures Using C++ 2E Chapter 3 Pointers. Data Structures Using C++ 2E2 Objectives Learn about the pointer data type and pointer variables Explore.
 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.
More C++ Features True object initialisation
Object-Based Programming Mostly Review. Objects Review what is object? class? member variables? member functions? public members? private members? friend.
CMSC 202, Version 3/02 1 Copy Constructors and Overloaded Assignment.
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.
The Assignment Operator. Rule of Three Any object which manages memory needs: – Custom Destructor – Custom Copy Constructor – Custom Assignment Operator.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 1.
1 // SPECIFICATION FILE (dynarray.h) // Safe integer array class allows run-time specification // of size, prevents indexes from going out of bounds, //
CS212: Object Oriented Analysis and Design Polymorphism (Using C++)
Link-Based Implementations
Yan Shi CS/SE 2630 Lecture Notes
Constructors and Destructors
Learning Objectives Pointers as dada members
Linked Lists Chapter 6 Section 6.4 – 6.6
Default Constructors A default constructor is a constructor that takes no arguments. If you write a class with no constructor at all, C++ will write a.
Andy Wang Object Oriented Programming in C++ COP 3330
Chapter 9 More on Objects and Classes
Constructor & Destructor
Class Operations Pointer and References with class types
Class: Special Topics Copy Constructors Static members Friends this
Memberwise Assignment / Initialization
This pointer, Dynamic memory allocation, Constructors and Destructor
Dynamically Allocated Memory
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Automatics, Copy Constructor, and Assignment Operator
Classes with Dynamically Allocated Data
Chapter 15 Pointers, Dynamic Data, and Reference Types
Chapter 15 Pointers, Dynamic Data, and Reference Types
The Assignment Operator
Copy Constructor CSCE 121 J. Michael Moore.
Constructors and destructors
Constructors and Destructors
Copy Assignment CSCE 121 J. Michael Moore.
Operator overloading Dr. Bhargavi Goswami
Destruction and Copying
Dynamic Memory Management
Chapter 15-3 Pointers, Dynamic Data, and Reference Types
Destructor CSCE 121 J. Michael Moore.
CS148 Introduction to Programming II
Class and Objects In a class, all the functions that operate on the data structure are grouped together in one place along with the data Like a struct.
9-10 Classes: A Deeper Look.
Destructor CSCE 121.
Destruction and Copying
COP 3330 Object-oriented Programming in C++
The Constructors Lecture 7 Fri, Feb 2, 2007.
Copy Assignment CSCE 121.
Dynamic Memory Management
Copy Constructor CSCE 121.
Destructors, Copy Constructors & Copy Assignment Operators
Rule of Three Part 1 & 2.
Destructors, Copy Constructors & Copy Assignment Operators
Constructors & Destructors
9-10 Classes: A Deeper Look.
Rule Of Three Part 3.
Copy Constructors and Overloaded Assignment
SPL – PS3 C++ Classes.
Presentation transcript:

Class: Special Topics 2 For classes using memory allocation Destructors Deep vs. Shallow Copy Assignment Operator

Destructors A destructor is a method that is called automatically when a class instance is destroyed (de-allocated from memory) The opposite of a constructor which is used to create a class instance Defined with a tilde (and no parameters): ~ClassName(); Explicit definition is not required If not defined, compiler will call a default destructor and automatically remove all members Destructor should be defined if the class does explicit memory allocation (ex: using the new operator) Destructor should explicitly free any allocated memory (ex: using delete) If not done, pointer to memory will be destroyed, but memory will remain allocated with no way to reference it!

Deep vs. shallow copy Important to do a deep copy when making a copy of a class instance where memory is explicitly allocated in a class (ex: using new) Avoids problem of having compiler attempts to access deallocated memory Shallow copy Just copy the original pointer value to the pointer in the copied instance Result is that both the original and copied pointers have the same value i.e. point to the same memory location Deep copy Allocate new memory in the copied instance Copy the contents of the original instance to the new memory Result is two independent instances with their own copy of the same data values

Assignment operator Must test for self-assignment Otherwise… If the class does memory allocation Ex: A = A Otherwise… When the existing storage is deallocated, there will be nothing to copy!!! Note also necessary in copy constructors!!! MyClass& MyClass::operator=(const MyClass &rhs) { // Check for self-assignment! if (this == &rhs) // Same object? return *this; // Yes, so skip assignment, // and just return *this. ... // Deallocate, allocate new space, // copy values... return *this; // for cascading! }