Download presentation
Presentation is loading. Please wait.
Published byMeryl Watson Modified over 8 years ago
1
1 Inside the Vector Class: with additional needed methods CPS212CPS212 Gordon College
2
2 Inside the Vector Class: class definition template class Vec { public: Vec(int size = 0); Vec(int, T); ~Vec();// destructor T& back(); const T& back() const; T& operator[] (int i); const T& operator[] (int i) const; void push_back(const T& item); void pop_back(); inline int getSize() const {return size;} inline bool isEmpty() const {return (size==0)?true:false;} inline int getCap() const {return cap;} void reserve(int n); private: int cap; int size; T *vPtr; };
3
3 Inside the Vector Class: the Constructors template Vec ::Vec(int size_): size(0), cap(0), vPtr(NULL) { if (size_ == 0) return; reserve(size_); } template Vec ::Vec(int size_, T value_): size(0), cap(0), vPtr(NULL) { if (size_ == 0) return; reserve(size_); size = size_; for (int i=0;i < size;i++) vPtr[i] = value_; }
4
4 Inside the Vector Class: the Destructor The new operator is used to perform memory allocation from the heap ptr = new T[20]; Heap before after ptr ? If object dies without first calling the Delete operation then allocated memory is lost - memory leak
5
5 Inside the Vector Class: the Destructor template Vec ::~Vec() { if (vPtr != NULL) delete [] vPtr; } Used to free a dynamically allocated array from the heap Dynamic Variable Example int *ptr = new int(3); delete ptr; Dynamic Array Example int *ptr = new int[20]; delete [ ] ptr; Note: must free memory before it becomes inaccessible
6
6 New Functions Needed Destructor –When class object goes out of scope the pointer to the dynamically allocated memory is reclaimed (reused my OS) automatically –The dynamically allocated memory is not –The destructor reclaims dynamically allocated memory
7
7 Inside the Vector Class: the Allocation template void Vec ::reserve(int n) { T *nPtr; if (size>n) n=size; nPtr = new T[n]; if (nPtr == NULL) throw runtime_error ("Vec reserve(): memory allocation failure"); for(int i = 0; i < size; i++) nPtr[i] = vPtr[i]; if (vPtr != NULL) delete [] vPtr; vPtr = nPtr; cap = n; }
8
8 Inside the Vector Class: back() template T& Vec ::back() { if (size == 0) throw runtime_error("Vec back(): vector empty"); return vPtr[size-1]; } template const T& Vec ::back() const { if (size == 0) throw runtime_error("Vec back(): vector empty"); return vPtr[size-1]; }
9
9 Inside the Vector Class: push and pop template void Vec ::push_back(const T& item) { if (size == cap) { if (cap == 0) reserve(1); else reserve(2 * cap); } vPtr[size] = item; size++; } template void Vec ::pop_back() { if (size == 0) throw runtime_error( "Vec pop_back(): vector is empty"); size--; }
10
10 New Functions Needed: Copy Constructor A copy constructor is called whenever a new variable is created from an object. This happens in the following cases (but not in assignment). 1. A variable is declared which is initialized from another object. Cow q(“Bessie”); // constructor is used to build q. Cow r(p); // copy constructor is used to build r. Cow p = q; // copy constructor is used to initialize in declaration. p = q; // Assignment operator, not constructor or copy constructor. 2. argument is passed as value parameter. f(p); // copy constructor initializes formal value parameter. (method call) 3. An object is returned by a function (returns a local copy).
11
11 New Functions Needed: Copy Constructor Copy Constructor – makes a "deep copy" of an objectCopy Constructor C++ calls the copy constructor when the previously mentioned “copy constructor” cases are executed. (if copy constructor is not available then the default constructor is called) If copy is not made, observe results (aliasing problem, "shallow" copy)
12
12 Inside the Vector Class: the copy constructor template Vec ::Vec (const Vec & obj): size(0), cap(0), vPtr(NULL) { if (obj.getCap() == 0) return; reserve(obj.getCap()); size = obj.size; for (int i = 0; i < size; i++) vPtr[i] = obj.vPtr[i]; }
13
13 Assignment operator –Default assignment operator makes shallow copy –Can cause memory leak, dynamically-allocated memory has nothing pointing to it New Functions Needed: Assignment Operator
14
14 Inside the Vector Class: the assignment operator template Vec & Vec ::operator= (const Vec & rhs) { if (vPtr!=NULL) delete [] vPtr; vPtr = new T[rhs.getCap()]; if (vPtr == NULL) throw runtime_error("Vec reserve(): memory allocation failure"); size = rhs.size; for (int i = 0; i < size; i++) vPtr[i] = rhs.vPtr[i]; return *this; }
15
15 Class Design Needs RULE: If a class allocates memory at run time using the new, then it should provide … A destructor A copy constructor An assignment operator These overcome the 2 common dynamic memory allocation problems: memory leak and shallow copy.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.