cs32 week 1
play

CS32 - Week 1 Umut Oztok June 24, 2016 Umut Oztok CS32 - Week 1 - PowerPoint PPT Presentation

CS32 - Week 1 Umut Oztok June 24, 2016 Umut Oztok CS32 - Week 1 Administration Email:umut@ucla.edu Office hours: R 09:30am-12:30pm (BH 4663) Umut Oztok CS32 - Week 1 Constructor Special member function to initialize an instance of a


  1. CS32 - Week 1 Umut Oztok June 24, 2016 Umut Oztok CS32 - Week 1

  2. Administration Email:umut@ucla.edu Office hours: R – 09:30am-12:30pm (BH 4663) Umut Oztok CS32 - Week 1

  3. Constructor Special member function to initialize an instance of a class. Called whenever a class object is created. Has the same name as the class name. No return type, so returns nothing. Can have parameters. A constructor without any parameter is called ”default constructor”. A class can have more than one constructor. But they cannot have the exact same parameters/types. Where to put its implementation? Inside the class definition (i.e., header file) Outside the class definition (i.e., cpp file) Umut Oztok CS32 - Week 1

  4. Constructor Syntax of Constructor ClassName (); ClassName (Parameter p 1 , ..., Parameter p n ); class Circ { public : Circ(); //default constructor Circ( float x, float y, float r); ... private : float m_x, m_y, m_r; }; Umut Oztok CS32 - Week 1

  5. Destructor Special member function to de-initialize an instance of a class. Called whenever a class object is destroyed. No return type, so returns nothing. Cannot have parameters. A class can have at most one destructor. Where to put its implementation? Inside the class definition (i.e., header file) Outside the class definition (i.e., cpp file) When do you need it? Whenever you need to do any kind of maintenance before the object of the class is destroyed. Cleaning the memory allocated, closing a file, etc. Umut Oztok CS32 - Week 1

  6. Destructor Syntax of Destructor ~ClassName (); DON’T FORGET TILDE class Circ { public : Circ(); //default constructor Circ( float x, float y, float r); ~Circ(); //destructor ... private : float m_x, m_y, m_r; }; Umut Oztok CS32 - Week 1

  7. Dynamic Memory Usage Sometimes you won’t know size of some variables before runtime. E.g., the memory needed may depend on user input. In such cases, you need to dynamically allocate memory. Also, whenever you are done with allocated memory, you should deallocate (free) it. Otherwise, you have memory leaks. In C++, we use operators new and delete for dynamic memory management. In Java, there is automatic garbage collection, so you don’t need to worry about freeing stuff. Umut Oztok CS32 - Week 1

  8. Dynamic Memory Allocation Syntax of new Operator pointer = new type; pointer = new type [number of elements]; int * foo; foo = new int [10]; Circ* a_ptr; a_ptr = new Circ(1,2,3); Circ* b_ptr; b_ptr = new Circ[10]; Umut Oztok CS32 - Week 1

  9. Dynamic Memory Deallocation Syntax of delete Operator delete pointer; delete [] pointer; int * foo; foo = new int [10]; delete [] foo; Circ* a_ptr; a_ptr = new Circ(1,2,3); delete a_ptr; Circ* b_ptr; b_ptr = new Circ[10]; delete [] b_ptr; Umut Oztok CS32 - Week 1

  10. A Digression How do you check whether the new operation fails? That is, you try to allocate memory but OS cannot perform your request? int * foo; foo = new int [10]; if (foo == nullptr) { //failure } else { //success } How do you check whether the delete operation fails? That is, you try to free memory but OS cannot perform your request? You don’t :) Umut Oztok CS32 - Week 1

  11. Memory Allocation on Class Types What is really happening when the following code is executed? Circ* a_ptr; a_ptr = new Circ(1,2,3); delete a_ptr; new delete OS allocates memory. The destructor for class is The constructor for class is called (if class has one). called on this memory (if class OS frees the memory. has one). Umut Oztok CS32 - Week 1

  12. Copy Constructor Special member function to initialize an instance of a class from an existing instance of the same class. In fact, it is nothing but a special constructor with a parameter of the class type. When is it being called? Creating an instance using another instance. Circ a(1,2,3); Circ b(a); Assigning an uninitialized instance with another one. Circ a(1,2,3); Circ b = a; Passing an argument by value. compute_area(Circ a); Umut Oztok CS32 - Week 1

  13. Copy Constructor In fact C++ always provides you with a default copy constructor, which basically copies all data members to be exactly the same. Why do we need our own copy constructor? Allocate dynamic memory. Open system resources, e.g., opening a file. Umut Oztok CS32 - Week 1

  14. Copy Constructor Steps to implement a copy constructor: Determine how much memory is allocated in the existing variable. Allocate the same amount of memory for the new variable. Copy the content from the existing variable to new one. Umut Oztok CS32 - Week 1

  15. Copy Constructor Syntax of Copy Constructor ClassName (const ClassName&); class Circ { public : ... //copy constructor Circ( const Circ& oldVar) { m_x = oldVar.m_x; m_y = oldVar.m_y; m_r = oldVar.m_r; } ... private : float m_x, m_y, m_r; } Umut Oztok CS32 - Week 1

  16. Real use of Copy Constructor class Circles { private : int size; Circ* circles; public : ... ~Circles() { delete [] circles;} //destructor Circles( const Circles& src) { //copy constructor size = src.size; circles = new Circ[size]; for ( int i=0; i<size; i++) circles[i] = src.circles[i]; } }; If you rely on default constructor, you do shallow copy. But, you need deep copy. So, you should write your own default constructor. Umut Oztok CS32 - Week 1

  17. Assignment Operator Assignment operator "=". C++ always provides you with a default assignment operator. However, default one copies all the data members. So, shallow copy again. As for copy constructor, when you allocate dynamic memory, you need to override default assignment operator. Umut Oztok CS32 - Week 1

  18. Assignment Operator Steps to override an assignment operator. Free all dynamic memory used by target instance. Re-allocate memory for target instance. Copy all contents from source instance to target instance Return a reference to target instance. So, more work to do, compared to copy constructor. The reason is target instance is already initialized, so need to take care of allocated memory. Umut Oztok CS32 - Week 1

  19. Assignment Operator Syntax of Assignment Operator ClassName& operator= (const ClassName& src); class Circ { public : ... Circ& operator =( const Circ& src) { //assignment op. if ( this != &src) { m_x = src.m_x; m_y = src.my; m_r = src.m_r; } return (* this ); } private : float m_x, m_y, m_r; }; Umut Oztok CS32 - Week 1

  20. Real use of Assignment Operator class Circles { private : int size; Circ* circles; public : ... //assignment operator Circles& operator =( const Circles& src) { if ( this != &src) { delete [] circles; size = src.size; circles = new Circ[size]; for ( int i=0; i<size; i++) circles[i] = src.circles[i]; } return (* this ); } }; Umut Oztok CS32 - Week 1

  21. Copy Constructor vs. Assignment Operator // #1 // #3 main () { Circles print() { Circles a(4), b(5); Circles f(10); b = a; return f; } } // #2 main () { main () { Circles g = print(); Circles c(4), d(c); } c = d; } Umut Oztok CS32 - Week 1

  22. Assignment Operator Revisited Current approach: //assignment operator Circles& operator =( const Circles& src) { if ( this != &src) { delete [] circles; size = src.size; circles = new Circ[size]; for ( int i=0; i<size; i++) circles[i] = src.circles[i]; } return (* this ); } That is, delete memory first, reallocate it, and then copy. What is not good with that method? Code duplication (destructor and copy constructor). Umut Oztok CS32 - Week 1

  23. Assignment Operator Revisited How to prevent code duplication? Copy-and-swap approach. Use swap function. void swap(Circles& c) { ... //exchange size and c.size (integers) ... //exchange circles and c.circles (pointers) } Umut Oztok CS32 - Week 1

  24. Assignment Operator Revisited //assignment operator Circles& operator =( const Circles& src) { if ( this != &src) { Circles temp(src); swap(temp); } return (* this ); } This provides you no code duplication: Deallocation - automatically done by destructor. Reallocation and copy - automatically done by copy constructor. Umut Oztok CS32 - Week 1

  25. Slides Slides are available at http://www.cs.ucla.edu/~umut/cs32 Umut Oztok CS32 - Week 1

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend