The Big Three Based on Weiss “Data Structures and algorithm Analysis CS240 Computer Science II.

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.
A RRAYS, P OINTERS AND R EFERENCES 1. A RRAYS OF O BJECTS Arrays of objects of class can be declared just like other variables. class A{ … }; A ob[4];
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
Dynamically Allocated Arrays May 2, Quiz 5 Today.
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.
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.
Review of C++ Programming Part II Sheng-Fang Huang.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
CSE 333 – SECTION 4. Overview Pointers vs. references Const Classes, constructors, new, delete, etc. More operator overloading.
Pointer Data Type and Pointer Variables
1 Overloading Overloading allows a function or operator to have a different meaning depending on the type of objects it is used on. Examples: operator+
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures C++ Review Part-I.
1 Inside the Vector Class: with additional needed methods CPS212CPS212 Gordon College.
1 Chapter 15-2 Pointers, Dynamic Data, and Reference Types Dale/Weems.
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.
Concordia TAV 2002 Comp5421_421 Comp5421 Object Oriented Programming Using C++ Efficiently Lecture 4 (2) Tianxiang Shen Summer 2002 Department of Computer.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
More C++ Features True object initialisation
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
CMSC 202, Version 3/02 1 Copy Constructors and Overloaded Assignment.
1 Chapter 1 C++ Basics Review Reading: Sections 1.4 and 1.5.
Data Structures Using C++1 Chapter 3 Pointers Dr. Liu.
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.
Programming Languages and Paradigms C++. C++ program structure  C++ Program: collection of files Header files CPP source files  Files contain class,
The Assignment Operator. Rule of Three Any object which manages memory needs: – Custom Destructor – Custom Copy Constructor – Custom Assignment Operator.
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.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures C++ Review 2.
CS162 - Topic #6 Lecture: Pointers and Dynamic Memory –Review –Dynamically allocating structures –Combining the notion of classes and pointers –Destructors.
1 CS 132 Spring 2008 Chapter 5 Linked Lists p
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
Object-Oriented Programming (OOP) Lecture No. 24.
1 Ugly Realities The Dark Side of C++ Chapter 12.
Memory Management.
Yan Shi CS/SE 2630 Lecture Notes
Pointers and Dynamic Arrays
Learning Objectives Pointers as dada members
Programming with ANSI C ++
Chapter 1 C++ Basics Review
CISC181 Introduction to Computer Science Dr
Class: Special Topics Copy Constructors Static members Friends this
Memberwise Assignment / Initialization
Dynamically Allocated Memory
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Automatics, Copy Constructor, and Assignment Operator
Classes with Dynamically Allocated Data
understanding memory usage by a c++ program
Object Oriented Programming (OOP) Lecture No. 9
Dynamic Memory Copy Challenge
Summary: Abstract Data Type
CS148 Introduction to Programming II
Passing Arguments and The Big 5
CS410 – Software Engineering Lecture #5: C++ Basics III
COP 3330 Object-oriented Programming in C++
Dynamic Memory Copy Challenge
CMSC 341 C++ and OOP.
Class: Special Topics 2 For classes using memory allocation
CMSC 341 C++ and OOP.
CS 144 Advanced C++ Programming March 21 Class Meeting
CMSC 341 C++ and OOP.
CMSC 341 C++ and OOP.
CS 144 Advanced C++ Programming April 30 Class Meeting
SPL – PS3 C++ Classes.
Presentation transcript:

The Big Three Based on Weiss “Data Structures and algorithm Analysis CS240 Computer Science II

The Big Three The destructor The copy constructor The operator= If the does not supply his own, the default version of all three functions provided by the system will be used.

The Destructor called whenever an object goes out of scope. free up resources that were allocated during the use of the object.

Copy Constructor construct a new object that is initialized to a copy of the same type of object. a copy constructor is called when –datatype a = b;// b already defined datatype a(b);// euqivalent to above Note that a = b; // not the same as above –an object is passed using call by value. –an object returned by value.

operator= a = b; assignment operator = is called when one existing object is assigned (in a member- wise fashion) to another existing object.

Problems with the defaults The problem occurs when one or more data members of a class are pointer type. –the default destructor for pointers does not de- allocate the dynamically allocated object pointed to by the pointer! –both the copy constructor and operator= only copy the value (address) of the pointer instead of the object pointer to by the pointer. Thus, we will have two class instances that contain pointers pointing to the same object. This is known as the shallow copy; normally one would expect a deep copy where a clone of the the entire object is made.

Why the defaults do not work: an example class intCell { public: intCell(int initVal = 0){p = new int(initVal);} int read( ) const {return *p;} void set(int x) {*p = x;} private: int *p; };

Why the defaults do not work: an example continued int f() { intCell a(2); intCell b = a;// calls the default copy constructor intCell c; c = b;// calls the default operator= a.set(4); cout << a.read() << ‘\t’ << b.read() << ‘\t’ << a.read() << endl; return 0; } What are the output? Do you expect 4 2 2? It turns out that the outputs are 4 4 4! Why?

The memory leak problem The content of data member p of c object is replaced by that of b object, thus the int object pointed to by p of c is no longer accessible for deletion, resulting in a problems known as memory leak.

The fix: programmer supplies the big three class intCell { public: intCell(int initVal = 0) {p = new int(initVal);} intCell(const intCell & rhs);// copy constructor ~intCell( );// destructor const intCell& operator=(const intCell & rhs); int read( ) const {return *p;} void set(int x) {*p = x;} private: int *p; };

The fix: programmer supplied copy constructor and destructor intCell::intCell(const intCell & rhs) { p = new int(*rhs.p);// rhs is an intCell object }// contents of pointee copied // instead address of pointee // is copied! intCell::~intCell( ) { delete p; }

The fix: programmer supplied operator= cosnt intCell & intCell::operator=(cosnt intCell & rhs) { if (this != &rhs)// avoid copy to self *p = *rhs.p;// rhs is an intCell object return *this; } Note again: the contents of the pointee is copied instead of the address of pointee is copied!