// This ensures that this file is included at most once // in any program, as there may be multiple include // statements. #pragma once #include #include #include "Pair.hpp" /////////////////////// // Class declaration // /////////////////////// class Array; /////////////////////////// // Function declarations // /////////////////////////// // This example extends the previous Array project by // introducing operator overloading. // // Most of the functions and the Array class itself are the // same as in the previous project. Refer to that project // for a discussion of dynamic memory allocation, pointers, // initialization, clean-up, resetting entries, and the // other array operations. // // The focus of this project is operator overloading. C++ // allows us to define the behavior of many operators when // they are applied to objects of a class such as Array. // // In this example, we overload the << operator so that // instead of writing // // std::cout << to_string( array ); // // we can write // // std::cout << array; // // instead. The expression is translated into a call to // // operator<<( std::cout, array ) // // by the compiler. // // Operator overloading does not create new operators or // change their precedence or associativity. It defines what // an existing C++ operator does when applied to objects of // the specified types. ////////////////////////////////////// // Initialization and modification // ////////////////////////////////////// // Initialize 'array' with the given capacity. // - Dynamically allocate an array of 'capacity' entries. // - Initialize each entry to 'initial_value'. // - If no initial value is supplied, initialize each // entry to 0.0. void array_init( Array &array, std::size_t const capacity, double const initial_value = 0.0 ); // Clean up 'array'. // - Deallocate the dynamically allocated array. // - Reset the member variables so that the object no // longer refers to the deallocated array. void array_clean_up( Array &array ); // Reset every entry in 'array' to 'new_value'. // - If no new value is supplied, reset each entry to 0.0. void array_reset( Array &array, double const new_value = 0.0 ); ////////////////////// // Array operations // ////////////////////// // Calculate the average of the entries in 'array'. double average( Array const &array ); // Determine whether the entries in 'array' are sorted // from least to greatest. // - If the array is sorted, return its capacity. // - Otherwise, return the index of the first entry that // is less than the entry immediately before it. std::size_t is_sorted( Array const &array ); // Return the indices of the maximum and minimum entries // in 'array'. // - 'first_' is the index of the maximum entry. // - 'second_' is the index of the minimum entry. Pair maxmin( Array const &array ); /////////////////////////// // Output and conversion // /////////////////////////// // Return a comma-separated string containing the entries // in 'array'. // // This is not an overloaded operator and is unchanged in // principle from the previous project. std::string to_string( Array const &array ); // Insert a string representation of 'array' into the // output stream. // // Overloading operator<< allows us to write // // std::cout << array; // // The first argument is the output stream and the second // argument is the Array object to be inserted. // // The output stream is returned by reference so that // insertions can be chained: // // std::cout << array1 << array2 << std::endl; // // This is the same general mechanism used by the standard // output operations discussed previously. std::ostream &operator<<( std::ostream &out, Array const &array ); ////////////////////// // Class definition // ////////////////////// // Store the capacity of a dynamically allocated array // together with a pointer to its first entry. // // This class is unchanged from the previous Array project. // The important change in this project is that operator<< // has been overloaded so that an Array object can be // inserted directly into an output stream. class Array { public: std::size_t capacity_; double *array_; };