Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 // SPECIFICATION FILE (dynarray.h) // Safe integer array class allows run-time specification // of size, prevents indexes from going out of bounds, //

Similar presentations


Presentation on theme: "1 // SPECIFICATION FILE (dynarray.h) // Safe integer array class allows run-time specification // of size, prevents indexes from going out of bounds, //"— Presentation transcript:

1 1 // SPECIFICATION FILE (dynarray.h) // Safe integer array class allows run-time specification // of size, prevents indexes from going out of bounds, // allows aggregate array copying and initialization. class DynArray { public: DynArray( /* in */ int arrSize ); // Constructor. // PRE: arrSize is assigned // POST: IF arrSize >= 1 && enough memory THEN // Array of size arrSize is created with // all elements == 0 ELSE error message. DynArray( const DynArray& otherArr ); // Copy constructor. // POST: this DynArray is a deep copy of otherArr // Is implicitly called for initialization. 1

2 2 // SPECIFICATION FILE continued (dynarray.h) ~DynArray( ); // Destructor. // POST: Memory for dynamic array deallocated. int ValueAt ( /* in */ int i ) const; // PRE: i is assigned. // POST: IF 0 <= i < size of this array THEN // FCTVAL == value of array element at index i // ELSE error message. void Store ( /* in */ int val, /* in */ int i ) // PRE: val and i are assigned // POST: IF 0 <= i < size of this array THEN // val is stored in array element i // ELSE error message. 2

3 3 // SPECIFICATION FILE continued (dynarray.h) void CopyFrom ( /* in */ DynArray otherArr); // POST: IF enough memory THEN // new array created (as deep copy) // with size and contents // same as otherArr // ELSE error message. private: int* arr ; int size ; }; 3

4 4 class DynArray 80 40 90 ? ? Private data: size 5 arr 6000 Free store 6000 DynArray Store ValueAt DynArray ~DynArray CopyFrom

5 5 DynArray beta(5); //constructor ? ? ? Private data: size 5 arr 2000 Free store 2000 DynArray Store ValueAt DynArray ~DynArray CopyFrom beta

6 6 DynArray::DynArray( /* in */ int arrSize ) // Constructor. // PRE: arrSize is assigned // POST: IF arrSize >= 1 && enough memory THEN // Array of size arrSize is created with // all elements == 0 ELSE error message. { int i; if ( arrSize < 1 ) { cerr << “DynArray constructor - invalid size: “ << arrSize << endl; exit(1); } arr = new int[arrSize] ; // allocate memory size = arrSize; for (i = 0; i < size; i++) arr[i] = 0; } 6

7 7 beta.Store(75, 2); ? 75 ? ? Private data: size 5 arr 2000 Free store 2000 DynArray Store ValueAt DynArray ~DynArray CopyFrom beta

8 8 void DynArray::Store ( /* in */ int val, /* in */ int i ) // PRE: val and i are assigned // POST: IF 0 <= i < size of this array THEN // arr[i] == val // ELSE error message. { if ( i = size ) { cerr << “Store - invalid index : “ << i << endl; exit(1) ; } arr[i] = val ; } 8

9 9 ? ? Private: size 4 arr 3000 3000 Private: size 5 arr 2000 2000 ? 75 ? ? gamma beta DynArray gamma(4);//constructor DynArray Store ValueAt DynArray ~DynArray CopyFrom DynArray Store ValueAt DynArray ~DynArray CopyFrom

10 10 ? -8 ? Private: size 4 arr 3000 3000 Private: size 5 arr 2000 2000 ? 75 ? ? gamma beta gamma.Store(-8,2); DynArray Store ValueAt DynArray ~DynArray CopyFrom DynArray Store ValueAt DynArray ~DynArray CopyFrom

11 11 int DynArray::ValueAt ( /* in */ int i ) const // PRE: i is assigned. // POST: IF 0 <= i < size THEN // FCTVAL == arr[i] // ELSE halt with error message. { if ( i = size ) { cerr << “ValueAt - invalid index : “ << i << endl; exit(1) ; } return arr[i]; } 11

12 12 Why is a destructor needed? When a DynArray class variable goes out of scope, the memory space for data members size and pointer arr is deallocated. But the dynamic array that arr points to is not automatically deallocated. A class destructor is used to deallocate the dynamic memory pointed to by the data member.

13 13 DynArray::~DynArray( ); // Destructor. // POST: Memory for dynamic array deallocated. { delete [ ] arr ; } 13 class DynArray Destructor

14 14 Copy Constructor l An overloaded constructor l When an object is passed to a function, a bitwise (exact) copy of that object is made and given to the function. l If the object contains a pointer to allocated memory, the copy will point to the memory as does the original object.

15 15 Copy Constructor l If the copy makes a change to the contents of this memory, it will be changed for the original object too. l Also, when the function terminates, the copy will be destroyed, causing its destructor to be called. l That can free dynamically allocated memory, used by the original object as well.

16 16 What happens... l When a function is called that uses pass by value for a class object of DynArray type? ? 75 ? ? Private: size 5 arr 2000 2000 DynArray Store ValueAt DynArray ~DynArray CopyFrom

17 17 // FUNCTION CODE void SomeFunc( DynArray someArr ) // Uses pass by value {. } 17 Passing a Class Object by Value

18 18 By default, Pass-by-value makes a shallow copy DynArray beta(5); // CLIENT CODE. SomeFunc( beta ); // function call beta someArr ? 75 ? ? 2000 DynArray. Private: size 5 arr 2000 DynArray. Private: size 5 arr 2000 shallow copy

19 19 // FUNCTION CODE void SomeFunc( DynArray someArr ) // Uses pass by value { someArr.Store(290, 2);. } WHAT HAPPENS IN THE SHALLOW COPY SCENARIO? 19 Suppose SomeFunc calls Store

20 20 DynArray beta(5); // CLIENT CODE. SomeFunc( beta); beta.arr[2] has changed beta someArr ? 290 ? ? 2000 DynArray. Private: size 5 arr 2000 DynArray. Private: size 5 arr 2000 shallow copy

21 21 beta.arr[2] has changed NOTICE THAT NOT JUST FOR THE SHALLOW COPY, BUT ALSO FOR ARGUMENT beta, THE DYNAMIC DATA HAS CHANGED! beta someArr ? 290 ? ? 2000 DynArray. Private: size 5 arr 2000 DynArray. Private: size 5 arr 2000 shallow copy

22 22 CONSTRUCTOR COPY CONSTRUCTOR DESTRUCTOR Classes with Data Member Pointers Need

23 23 Shallow Copy vs. Deep Copy l a shallow copy copies only the class data members, and does not make a copy of any pointed-to data l a deep copy copies not only the class data members, but also makes a separate stored copy of any pointed-to data

24 24 What’s the difference? l a shallow copy shares the pointed to dynamic data with the original class object l a deep copy makes its own copy of the pointed to dynamic data at different locations than the original class object

25 25 ? 75 ? ? 4000 DynArray. Private: size 5 arr 4000 beta someArr deep copy ? 75 ? ? 2000 DynArray. Private: size 5 arr 2000 Making a (Separate) Deep Copy

26 26 Assignment and Initialization l In both cases the value of one object is given to another. l Copy constructor only applies to initializations.

27 27 Initialization of Class Objects l Initialization can occur three ways: n an object is used to initialize another in a declaration statement, n passing an object argument by value to a function, n returning a temporary object as the return value of a function, l by default, C++ uses shallow copies for these initializations

28 28 default copy constructor l C++ automatically provides a default copy constructor that simply duplicates the object. l It is possible to specify precisely how one object will initialize another by defining a copy constructor. l Copy constructor do not affect assignment operations.

29 29 Common form of Copy Constructor classname(const classname &obj) {... } Here obj is a reference to an object that is being used to initialize another object. Time t1=t2; // explicitly initializing t1 func1(t2); // t2 passed as a parameter t2=func1(); // t2 receiving a returned object In the first two cases, a reference to t2 would be passed to the copy constructor. In the third, a reference to the object returned by funct2() is passed to the copy constructor.

30 30 As a result... l when a class has a data member pointer to dynamically allocated data, you should write what is called a copy constructor l the copy constructor is implicitly called in initialization situations and makes a deep copy of the dynamic data in a different memory location

31 31 More about Copy Constructors l when there is a copy constructor provided for a class, the copy constructor is used to make copies for pass by value l you do not call the copy constructor l like other constructors, it has no return type l because the copy constructor properly defines pass by value for your class, it must use pass by reference in its definition

32 32 Copy Constructor l copy constructor is a special member function of a class that is implicitly called in these 3 situations: n passing object parameters by value n initializing an object variable in its declaration n returning an object as the return value of a function

33 33 ? 75 ? ? Private: size 5 arr 2000 2000 Private: size 5 arr 4000 4000 ? 75 ? ? beta someArr SomeFunc(beta); // copy-constructor // beta passed by value DEEP COPY DynArray Store ValueAt DynArray ~DynArray CopyFrom DynArray Store ValueAt DynArray ~DynArray CopyFrom

34 34 DynArray::DynArray( const DynArray& otherArr ) // Copy constructor // Implicitly called for deep copy in initializations. // POST: If room on free store THEN // new array of size otherArr.size is created // on free store && arr == its base address // && size == otherArr.size // && arr[0..size-1] == otherArr.arr[0..size-1] // ELSE error message. { int i ; size = otherArr.size ; arr = new int[size] ; // allocate memory for copy for ( i = 0; i< size ; i++ ) arr[i] = otherArr.arr[i] ; // copies array } 34

35 35 What about the assignment operator? l the default method used for assignment of class objects makes a shallow copy l if your class has a data member pointer to dynamic data, you should write a member function to create a deep copy of the dynamic data

36 36 gamma.CopyFrom(beta); ? 75 ? ? Private: size 5 arr 3000 3000 Private: size 5 arr 2000 2000 ? 75 ? ? gamma beta DEEP COPY DynArray Store ValueAt DynArray ~DynArray CopyFrom DynArray Store ValueAt DynArray ~DynArray CopyFrom

37 37 void DynArray::CopyFrom ( /* in */ DynArray otherArr ) // Creates a deep copy of otherArr. // POST: Array pointed to by arr@entry deallocated // && IF room on free store // THEN new array is created on free store // && arr == its base address // && size == otherArr.size // && arr[0..size-1] == otherArr[0..size-1] // ELSE halts with error message. { int i ; delete [ ] arr ;// delete current array size = otherArr.size ; arr = new int [size] ;// allocate new array for ( i = 0; i< size ; i++ ) // deep copy array arr[i] = otherArr.arr[i] ; } 37


Download ppt "1 // SPECIFICATION FILE (dynarray.h) // Safe integer array class allows run-time specification // of size, prevents indexes from going out of bounds, //"

Similar presentations


Ads by Google