Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /***************************************************************
- Author: Dr. Daniel Spiegel, Updated by: Trisha Badlu
- Creation Date: 21 March 2017
- Due Date: 31 March 2017
- Assignment: #3
- Filename: Array.cpp
- Course: CSC136 - 020
- Professor Name: Dr. Spiegel
- Purpose: Define member functions for Array class.
- ***************************************************************/
- // File: Array.cpp
- // Member function definitions for class Array
- #include <iostream>
- #include <iomanip>
- #include <stdlib.h>
- #include <assert.h>
- #include "Array.h"
- #include "SortSearch.h"
- using namespace std;
- // Initialize static data member at file scope
- int Array::arrayCount = 0; // no objects yet
- // Default constructor for class Array (default size 10)
- Array::Array(int arraySize){
- setEltsInUse(0);
- setCapacity(( arraySize > 0 ? arraySize : 10 ));
- ptr = new int[getCapacity()]; // create space for array
- assert( ptr != 0 ); // terminate if memory not allocated
- ++arrayCount; // count one more object
- for ( int index = 0; index < getCapacity(); index++ )
- ptr[ index ] = 0; // initialize array
- }
- // Copy constructor for class Array
- // must receive a reference to prevent infinite recursion
- Array::Array(const Array &init){
- setEltsInUse(init.getEltsInUse());
- setCapacity(init.getCapacity());
- ptr = new int[getCapacity()]; // create space for array
- assert( ptr != 0 ); // terminate if memory not allocated
- ++arrayCount; // count one more object
- for ( int index = 0; index < getEltsInUse(); index++ )
- ptr[ index ] = init.ptr[ index ]; // copy init into object
- }
- // Destructor for class Array
- Array::~Array(){
- delete [] ptr; // reclaim space for array
- --arrayCount; // one fewer objects
- }
- // Set the Array's size
- void Array::setCapacity(int elts){
- capacity=elts;
- }
- // Get the size of the array
- int Array::getCapacity() const{
- return capacity;
- }
- // Get the amount of elements in use
- int Array::getEltsInUse() const{
- return numElts;
- }
- // Set the amount of elements in use
- void Array::setEltsInUse(int eltsInUse){
- numElts = eltsInUse;
- }
- // Sorts the elements in the array
- Array &Array::sort(){
- selSort(ptr, getEltsInUse());
- }
- // Overloaded assignment operator
- // const return avoids: ( a1 = a2 ) = a3
- const Array &Array::operator=( const Array &right ){
- if ( &right != this ) { // check for self-assignment
- // for arrays of different sizes, deallocate original
- // left side array, then allocate new left side array.
- if ( getCapacity() != right.getCapacity() ) {
- delete [] ptr; // reclaim space
- setCapacity(right.getCapacity()); // resize this object
- setEltsInUse(right.getEltsInUse());
- ptr = new int[getCapacity()]; // create space for array copy
- assert( ptr != 0 ); // terminate if not allocated
- }
- for ( int index = 0; index < getEltsInUse(); index++ )
- ptr[ index ] = right[ index ]; // copy array into object
- }
- return *this; // enables x = y = z;
- }
- // Determine if two arrays are equal and
- // return true, otherwise return false.
- bool Array::operator==(const Array &right) const{
- if ( getEltsInUse() != right.getEltsInUse() )
- return false; // arrays of different sizes
- for ( int index = 0; index < getEltsInUse(); index++ )
- if ( ptr[ index ] != right[ index ] )
- return false; // arrays are not equal
- return true; // arrays are equal
- }
- // Determine if two arrays are not equal and
- // return true, otherwise return false (uses operator==).
- bool Array::operator!=(const Array &right) const{
- return ! ( *this == right );
- }
- // Added a specified number to the array and incrementing numElts
- // Creates new ptr if ptr is already full, also increasing capacity
- const Array &Array::operator+=(const int right){
- if(getEltsInUse() == getCapacity()){
- setCapacity(getCapacity()+1);
- int *tempArray = new int[getCapacity()];
- for(int index = 0; index < getCapacity() - 1; index++)
- tempArray[index] = ptr[index];
- tempArray[getCapacity()-1] = right;
- delete [] ptr;
- ptr = tempArray;
- }
- else
- ptr[getEltsInUse()] = right;
- setEltsInUse(getEltsInUse()+1);
- return *this;
- }
- // Overloaded subscript operator for non-const Arrays
- // reference return creates an lvalue
- int &Array::operator[](int subscript){
- // check for subscript out of range error
- assert( 0 <= subscript && subscript < getEltsInUse() );
- return ptr[subscript]; // reference return
- }
- // Overloaded subscript operator for const Arrays
- // const reference return creates an rvalue
- const int &Array::operator[](int subscript) const{
- // check for subscript out of range error
- assert( 0 <= subscript && subscript < getEltsInUse() );
- return ptr[ subscript ]; // const reference return
- }
- // Return the number of Array objects instantiated
- // static functions cannot be const
- int Array::getArrayCount(){
- return arrayCount;
- }
- // Overloaded output operator for class Array
- ostream &operator<<(ostream &output, const Array &right){
- int index;
- //output << '\t';
- for ( index = 0; index < right.getEltsInUse(); index++ ) {
- output << setw( 12 ) << right[ index ];
- if ( ( index + 1 ) % 4 == 0 ) // 4 numbers per row of output
- output << endl;
- }
- if ( index % 4 != 0 )
- output << endl;
- return output; // enables cout << x << y;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement