The Big Three If you need one of them, you need all of them

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

Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
Introduction to Programming Lecture 39. Copy Constructor.
CS-1030 Dr. Mark L. Hornick 1 Constructors Copy Constructors.
1 Inheritance: From membership point of view Vehicle SUV Honda Pilot Object Number Integer Double Base concept Derived concept Java Base class Derived.
Reusing Code Private or Protected inheritance. A cool class for array valarray class deals with numeric values, and it supports operation such as summing.
Value Semantics CS-240 & CS-341 Dick Steflik. Value Semantics determine how the value(s) of one object are copied to another object in C++ the value semantics.
1 Chapter 6 Lists Plus. ADT Sorted List Operations Transformers n MakeEmpty n InsertItem n DeleteItem Observers n IsFull n LengthIs n RetrieveItem Iterators.
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.
OOP Languages: Java vs C++
CS 1031 C++: Object-Oriented Programming Classes and Objects Template classes Operator Overloading Inheritance Polymorphism.
Introduction to Effective C++ Programming Kwanghee Ko Design Laboratory Department of Ocean Engineering Massachusetts Institute of Technology Day 3.
Programming Languages and Paradigms Object-Oriented Programming (Part II)
1 Data Structures - CSCI 102 CS102 C++ Pointers & Dynamic Objects Prof Tejada.
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
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.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
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.
Bank Account Example public class BankAccount { private double balance; public static int totalAccounts = 0; public BankAccount() { balance = 0; totalAccounts++;
PROGRAMMING 1 – HELPER INSTRUCTIONS ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED BY NANCY M. AMATO AND JORY DENNY 1.
1 Overload assignment = as a member function class point { public:..... point & operator = (const point & a);..... private: double my_x; double my_y;.....
1 Ugly Realities The Dark Side of C++ Chapter 12.
Memory Management.
Chapter 2 Objects and Classes
Yan Shi CS/SE 2630 Lecture Notes
Constructors and Destructors
Pointers and Dynamic Arrays
Learning Objectives Pointers as dada members
Chapter 22 - C++ Templates
Exceptions David Rabinowitz.
Copy Constructor / Destructors Stacks and Queues
Linked Lists Chapter 6 Section 6.4 – 6.6
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Andy Wang Object Oriented Programming in C++ COP 3330
CMPE 135: Object-Oriented Analysis and Design October 17 Class Meeting
Class: Special Topics Copy Constructors Static members Friends this
Memberwise Assignment / Initialization
This pointer, Dynamic memory allocation, Constructors and Destructor
Dynamic Memory Review what is static, automatic, dynamic variables? why are dynamic(ally allocated) variables needed what is program stack? function.
Chapter 2 Objects and Classes
group work #hifiTeam
Dynamically Allocated Memory
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Classes with Dynamically Allocated Data
understanding memory usage by a c++ program
Foundational Data Structures
Stacks as Linked lists top_node typedef Stack_entry Node_entry;
Constructors and Destructors
Indirection.
Summary: Abstract Data Type
Inheritance & Destructors
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.
Passing Arguments and The Big 5
Stacks CS-240 Dick Steflik.
Object-Oriented Programming (Part 2)
Inheritance & Destructors
Chapter 22 - C++ Templates
Chapter 22 - C++ Templates
Class: Special Topics 2 For classes using memory allocation
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Instructor: Dr. Michael Geiger Spring 2019 Lecture 23: Exam 2 Preview
Inheritance & Destructors
Rule of Three Part 1 & 2.
CS 144 Advanced C++ Programming April 30 Class Meeting
9-10 Classes: A Deeper Look.
Copy Constructors and Overloaded Assignment
Presentation transcript:

The Big Three If you need one of them, you need all of them Copy Constructor Assignment operator = Destructor If you need one of them, you need all of them 12/6/2018 IT 279

Overload assignment = as a member function a = b = c = d = e; class point { public: ..... point & operator = (const point & a); private: double my_x; double my_y; }; return type function name parameter list this is the pointer of the calling object C = (A = B) point & point::operator = (const point & a) { my_x = a.my_x; my_y = a.my_y; return *this; // return as this so we can use a=b=c; } A’s my_x and my_y (A is the calling object) 12/6/2018 IT 279

Using = and -> If x is an pointer, use -> instead of . (dot operator) class point { public: ..... point & operator = (const point & a); private: }; point *a, *b; a = new point(5,6); b = new point; .... printp(*a); a->distance(*b); a = b; point a,b(4,5); ..... a = b; .... a = point(2,3); // a = new point(1,2); All = in this block are not the operator = defined in class point. 12/6/2018 IT 279

What can we do? class point { public: ..... point & operator = (const point & a); private: }; point a,b(4,5); ..... a = b; a = point(1,2); point *a, *b; b = new point(1,2); a = new point(5,6); *b = *a; a=b; point *a, *b; a = new point(5,6); b = a; point *a, *b; a = new point(5,6); *b = *a; point *a, *b; a = new point(5,6); *b = point(1,2); point a,b(4,5); ..... a = b; a = new point(1,2); point *a, *b; b = new point(5,6); *b = point(1,2); point *a, *b; a = new point(5,6); b = new point(1,2); *b = *a; 12/6/2018 IT 279

The destructor : to destroy an object printp .... p: string printp(point p) { ...... } int main() { point v(4,5); .... cout << printp(v); ..... } ..... my_x my_y 4 5 Constructed by the constructor Copied by the copy constructor v: ..... When printp(v) in the main function is done, the copy of v must be destroyed. The destructor of Point will be called to do the job my_x my_y 4 5 12/6/2018 IT 279

Define a destructor point::~point() // nothing to be done; class point { public: ..... point & operator = (const point & a); // assignment operator point(const point & a); // copy constructor ~point(); // destructor private: double my_x; double my_y; }; .... point::~point() // nothing to be done; {} In fact, class point doesn’t need to explicitly program the big three; namely, the default ones can do the job. 12/6/2018 IT 279

Dynamic arrays, resizable arrays p i 5 60 700 int a[3]={5,60,700}; int *p,i; p = new int[3]; for (i=0; i<3; i++) p[i] = i; p = new int[i]; for (i=0; i<4; i++) p[i] = i*i; 1 4 5 1 2 3 1 2 1 4 9 2 4 12/6/2018 IT 279

Delete Dynamic arrays 4 5 60 700 1 1 4 3 1 1 9 2 2 3 a p i 4 1 2 int a[3]={5,60,700}; int *p,i; p = new int[3]; for (i=0; i<3; i++) p[i] = i; delete [] p; p = new int[4]; for (i=0; i<4; i++) p[i] = i*i; 1 3 1 4 9 1 4 2 3 12/6/2018 IT 279

Shallow Copy Shallow copy can be done by default copy constructor class Stack { ..... private: int my_size; int *my_stk; // pointer to a dynomic array }; Shallow copy can be done by default copy constructor Stack a ...... Stack b ...... my_size my_stk 4 my_size my_stk 4 Shallow Copy 1 4 9 12/6/2018 IT 279

Deep Copy Deep copy needs to program the copy constructor Stack b class Stack { ..... private: int my_size; int *my_stk; // pointer to a dynomic array }; Deep copy needs to program the copy constructor ...... Stack b my_size my_stk 4 Stack a ...... my_size my_stk 4 new int[4] 1 4 9 1 4 9 Deep Copy 12/6/2018 IT 279

Dynamic Arrays and The Big Three In general, if a dynamic array is used in the class, then its big three need deep copy. Copy Constructor Assignment operator = Destructor Java doesn’t have the destructor 12/6/2018 IT 279

Stack Class class Stack // First in last out { public: Stack(); // default constructor Stack(int *a, int n); // construct and push array a into the stack int pop(); // pop out the top element int push(int a); // push a into the stack int size(); // return the size of the stack; // The following three are so-called the Big Three; // They need to handle the dynamic array *my_size; Stack(const Stack & a); // 1. copy constructor; Stack & operator=(const Stack & a); // 2. assignment operator; ~Stack(); // 3. destructor; private: int *stk; int my_size; int head, tail; }; 12/6/2018 IT 279

The Copy Constructor Stack b Stack a Deep Copy 4 4 3 3 1 1 4 4 9 9 Stack::Stack(const Stack & a) // 1. copy constructor; { my_size = a.my_size; my_head = a.my_head; my_tail = a.my_tail; stk = new int[my_size]; for (int i =0; i<my_size; i++) stk[i] = a.stk[i]; } ...... 4 3 my_size my_head my_tail stk Stack b Stack a ...... 4 3 my_size my_head my_tail stk new int[4] 1 4 9 1 4 9 Deep Copy 12/6/2018 IT 279

Deep Copy for Assignment a = b b ...... my_size my_stk a 4 ...... my_size my_stk 4 1 4 9 1 4 9 Deep Copy 12/6/2018 IT 279

Problem of Deep Copy for Assignment a = b b ...... my_size my_stk a 3 ...... 3 my_size my_stk 4 2 4 6 1 4 9 2 4 6 Deep Copy wasted 12/6/2018 IT 279

Problem of Deep Copy for Assignment a = b b ...... my_size my_stk a 5 ...... 5 my_size my_stk 4 2 4 6 8 1 4 9 2 4 6 8 10 Deep Copy This location belongs to someone else 10 12/6/2018 IT 279

Operator = We have problem! a b Deep Copy wasted a = b 3 4 3 1 4 9 2 4 Stack & Stack::operator=(const Stack & a) // 2. assignment operator; { my_size = a.my_size; my_head = a.my_head; my_tail = a.my_tail; stk = new int[my_size]; for (int i =0; i<my_size; i++) stk[i] = a.stk[i]; return *this; // So, we can use a=b=c; } a = b We have problem! a ...... b ...... 3 my_size my_stk 4 my_size my_stk 3 1 4 9 2 4 6 2 4 6 Deep Copy wasted 12/6/2018 IT 279

Solution for Assignment a = b b ...... my_size my_stk a 5 ...... 5 my_size my_stk 4 new int[5] 2 4 6 8 10 2 4 6 8 10 1 4 9 Deep Copy delete 12/6/2018 IT 279

Another Problem for Assignment Stack & Stack::operator=(const Stack & a) // 2. assignment operator; { my_size = a.my_size; my_head = a.my_head; my_tail = a.my_tail; delete stk[]; stk = new int[my_size]; for (int i =0; i<my_size; i++) stk[i] = a.stk[i]; return *this; // So, we can use a=b=c; } We still have problem! new int[4] a ...... my_size my_stk 4 1 4 9 x delete Deep Copy a = a 12/6/2018 IT 279

Correctly overload = Stack & Stack::operator=(const Stack & a) // 2. assignment operator; { if (my_size != a.my_size) delete [] stk; stk = new int[a.my_size]; } my_size = a.my_size; my_head = a.my_head; my_tail = a.my_tail; for (int i =0; i<my_size; i++) stk[i] = a.stk[i]; return *this; // So, we can use a=b=c; 12/6/2018 IT 279

Desctructor Just delete all dynamic arrays created in this class. Stack::~Stack() // 3. destructor { delete [] stk; } Just delete all dynamic arrays created in this class. 12/6/2018 IT 279

Inheritance and Big-Three // Base class class A { public: A(int a); A(const A & a); //Copy constructor; A & operator = (const A & a); // overload = ~A(); // Destructor; ..... private: int size_A; int *array_A; }; // Derived class class B : public A { public: B(int b); B(const B & b); //Copy constructor; B & operator = (const B & b); // ~B(); // Destructor; ..... private: int size_B; int *array_B; }; In private section!! Use A’s constructor 12/6/2018 IT 279

Inheritance and copy Constructor // Derived class class B : public A { public: B(int b); B(const B & b); //Copy constructor; B & operator = (const B & b); // ~B(); // Destructor; ..... private: int size_B; int *array_B; }; // Derived class B::B(const B & b) :A(b) // call the copy constructor of A; { size_B = b.size_B; array_B = new int[size_B]; for (int i=0; i<size_B; i++) array_B[i] = b. array_B[i]; } 12/6/2018 IT 279

Inheritance and assignment = // Derived class class B : public A { public: B(int b); B(const & B); //Copy constructor; B & operator = (const B & b); // ~B(); // Destructor; ..... private: int size_B; int *array_B; }; X = Y // Derived class B & B::operator = (const B & b) { A::operator=(b); if (size_B != b.size_B()) delete [] array_B; array_B = new int[size_B]; }; for (int i =0; i<size_B; i++) array_B[i] = b.array_B[i]; return *this; } This will take care of the variables in the base class 12/6/2018 IT 279

Inheritance and destructor // Derived class class B : public A { public: B(int b); B(const & B); //Copy constructor; B & operator = (const B & b); // ~B(); // Destructor; ..... private: int size_B; int *array_B; }; Mind your own business // Derived class B::~B() { delete [] array_B; } 12/6/2018 IT 279