Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "The Big Three If you need one of them, you need all of them"— Presentation transcript:

1 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

2 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

3 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

4 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

5 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

6 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

7 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

8 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

9 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

10 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

11 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

12 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

13 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

14 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

15 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

16 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

17 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

18 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

19 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

20 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

21 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

22 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

23 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

24 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

25 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


Download ppt "The Big Three If you need one of them, you need all of them"

Similar presentations


Ads by Google