ECE 150 Fundamentals of Programming An array class

  • Slides: 18
Download presentation
ECE 150 Fundamentals of Programming An array class: constructor and destructor Prof. Hiren Patel,

ECE 150 Fundamentals of Programming An array class: constructor and destructor Prof. Hiren Patel, Ph. D. Douglas Wilhelm Harder, M. Math. LEL hdpatel@uwaterloo. ca dwharder@uwaterloo. ca © © 2018 by by Douglas Wilhelm Harder and Hiren Patel.

Memberfunctions Outline • In this lesson, we will: – Describe member functions – Discuss

Memberfunctions Outline • In this lesson, we will: – Describe member functions – Discuss their usage – Explain why this is necessary and useful

Memberfunctions Arrays in classes • We can only declare an array in a class

Memberfunctions Arrays in classes • We can only declare an array in a class if the size is fixed: class Massive_object; class Massive_object { public: Massive_object( double x, double y, double z, double vx, double vy, double vz, double mass ); double position[3]; // m double velocity[3]; // m/s double mass() const; void mass( double new_mass ); private: double mass_; positive }; // kg – must be strictly

Memberfunctions Variable sized local arrays • Recall that a local array can be variable

Memberfunctions Variable sized local arrays • Recall that a local array can be variable sized, defined at run time: int main() { unsigned int n; std: : cout << "Enter a capacity: "; std: : cin >> n; double array[n]; for ( std: : size_t k{0}; k < n; ++k ) { array[k] = k + 1. 0; Sufficient memory is allocated on the } stack at run time return 0; } – remember, the stack can continue to grow in one direction

Memberfunctions Variable sizes not allowed for arrays • However, you cannot declare a member

Memberfunctions Variable sizes not allowed for arrays • However, you cannot declare a member variable to be an array where the capacity is not known at compile time: class Dynamic_array { public: std: : size_t capacity; double data[capacity]; }; This is simply not allowed… – the compiler must know how much memory an instance of the class occupies at compile time – the memory for a bool, int, double and float is known at compile time • The justification for this requirement is straight-forward: – The compiler must know how much memory to allocate and copy every time an instance of a class is, for example, copied or passed as an argument

Memberfunctions Variable sized arrays in classes • The only solution is to use dynamically

Memberfunctions Variable sized arrays in classes • The only solution is to use dynamically allocated arrays: class Array { public: Array( std: : size_t capacity, double default = 0. 0 ); double const &at( std: : size_t k ) const; double &at( std: : size_t k ); protected: std: : size_t capacity_; double *a_data_; };

Memberfunctions Variable sized arrays in classes • The constructor would allocate memory: Array: :

Memberfunctions Variable sized arrays in classes • The constructor would allocate memory: Array: : Array( std: : size_t capacity, double default ): capacity_{capacity}, a_data_{new double[capacity_]} { for ( std: : size_t k{0}; k < capacity_; ++k ) { a_data_[k] = default; } Use the member variable, not the parameter } class Array { public: Array( std: : size_t capacity, double default = 0. 0 ); double at( std: : size_t k ) const; void at( std: : size_t k, double value ); protected: std: : size_t capacity_; double *a_data_; }; Initialization must be in the same order

Memberfunctions double const &at( std: : size_t k ) const • This function simply

Memberfunctions double const &at( std: : size_t k ) const • This function simply returns a reference to a_data_[k] – The returned reference is declared const – It cannot be modified by the user double const &Array: : at( std: : size_t k ) const { if ( k >= capacity_ ) { throw std: : out_of_range( "Accessing entry " + std: : to_string( k ) + ", but the array capacity is only " + std: : to_string( capacity_ ) ); } return a_data_[k]; }

Memberfunctions double &at( std: : size_t k ) • This returns a reference to

Memberfunctions double &at( std: : size_t k ) • This returns a reference to a_data_[k] – That entry can be assigned to by the user double &Array: : at( std: : size_t k ) { if ( k >= capacity_ ) { throw std: : out_of_range( "Accessing entry " + std: : to_string( k ) + ", but the array capacity is only " + std: : to_string( capacity_ ) ); } return a_data_[k]; }

Memberfunctions Using this class • This function calculates an average of n values void

Memberfunctions Using this class • This function calculates an average of n values void average( std: : size_t n ) Array tmp_array{n}; // Create an array of capacity 'n' for ( std: : size_t k{0}; k < tmp_array. capacity(); ++k ) { std: : cin >> tmp_array. at( k ); } double sum{0. 0}; for ( std: : size_t k{0}; k < tmp_array. capacity(); ++k ) { sum += tmp_array. at( k ); } return sum/tmp_array. capacity(); 1

Memberfunctions Using this class • Suppose we execute this code: void average( std: :

Memberfunctions Using this class • Suppose we execute this code: void average( std: : size_t n ) Array tmp_array{n}; // Create an array of capacity 'n' for ( std: : size_t k{0}; k < tmp_array. capacity(); ++k ) { std: : cin >> tmp_array. at( k ); 0 x 35 a 8 } average{0. 0}; for ( std: : size_t k{0}; k < tmp_array. capacity(); ++k ) { average += tmp_array. at( k ); 4. 5 2. 3 } capacity } _ a_data_ } tmp_arra y 1

Memberfunctions Using this class • The memory for the array pointer and the capacity

Memberfunctions Using this class • The memory for the array pointer and the capacity is deallocated • The dynamically allocated memory remains allocated – We have lost access to the address 1

Memberfunctions Variable sized arrays in classes • When the object goes out of scope,

Memberfunctions Variable sized arrays in classes • When the object goes out of scope, it is necessary to deallocate the associated memory: class Array { public: Array( std: : size_t capacity, double default = 0. 0 ); ~Array(); double at( std: : size_t k ) const; void at( std: : size_t k, double value ); protected: std: : size_t capacity_; double *a_data_; }; 1

Memberfunctions Variable sized arrays in classes • The destructor merely has to deallocate the

Memberfunctions Variable sized arrays in classes • The destructor merely has to deallocate the memory: class Array { public: Array( std: : size_t capacity, double default = 0. 0 ); ~Array(); double at( std: : size_t k ) const; void at( std: : size_t k, double value ); protected: std: : size_t capacity_; double *a_data_; }; 1

Memberfunctions Summary • Following this lesson, you now – Understand how member functions are

Memberfunctions Summary • Following this lesson, you now – Understand how member functions are defined – Understand they are called on the – There is a lot of time spent swapping, which can be reduced significantly – The run time is still approximately the same as selection sort 1

Memberfunctions References [1] No references? 1

Memberfunctions References [1] No references? 1

Memberfunctions Colophon These slides were prepared using the Georgia typeface. Mathematical equations use Times

Memberfunctions Colophon These slides were prepared using the Georgia typeface. Mathematical equations use Times New Roman, and source code is presented using Consolas. The photographs of lilacs in bloom appearing on the title slide and accenting the top of each other slide were taken at the Royal Botanical Gardens on May 27, 2018 by Douglas Wilhelm Harder. Please see https: //www. rbg. ca/ for more information. 1

Memberfunctions Disclaimer These slides are provided for the ECE 150 Fundamentals of Programming course

Memberfunctions Disclaimer These slides are provided for the ECE 150 Fundamentals of Programming course taught at the University of Waterloo. The material in it reflects the authors’ best judgment in light of the information available to them at the time of preparation. Any reliance on these course slides by any party for any other purpose are the responsibility of such parties. The authors accept no responsibility for damages, if any, suffered by any party as a result of decisions made or actions based on these course slides for any other purpose than that for which it was intended. 1