/* Absolute C++ Page 472 Problem numbers 1 and 3 1. Reread the code in Display 10.9. Write a class "TwoD" that implements the two dimensional dynamic array of doubles using the ideas from this display in its constructor. You should have a private member of type pointer to double to point to the dynamic array and two int values that are MaxRows and MaxColumns. You should supply a default constructor for which you are to choose a default maximum row and column size and a parameterized constructor that allows the programmer to set max row and column sizes. You should provide a bool member function that allows setting a particular row and column entry. This should return true if the operation was successful and false if the operation was unsuccessful. Check to see that the max row and column sizes have not been exceeded. You should provide a member function that returns a particular row and column entry of type double. Do NOT overload the index operator "[]"! Overload the + operator as a friend function to add two dimensional arrays. This should return a "TwoD" object whose ith row and jth column is the sum of the ith row and jth column of the right and left hand operands. Provide a copy constructor, an overloaded = operator and a destructor. Remember, these must be member functions. Declare class member functions that do not change the data to be const members. */ // Declare TwoD class // private: double* to dynamic array // two int values, MaxRows, MaxCols // public: Default constructor (user chooses max rows, max columns) // Parameterized constructor (programmer picks it) // double GetValue (returns row, column) // bool PointerValue (set a particular row/column entry. True if successful. // Mkae sure max rows, columns not exceeded.) // double PutValue (insert value) // Overload + as friend to add 2d arrays. // Copy constructor, overloaded = operator, destructor. Must be MEMBER FUNCTIONS // Decllare class member functions that do not cfhange the data to be const members #include using namespace std; typedef double* dPtr; //Declaration for pointer to a double. Address of // cell values. /* Class: TwoD * Date: 03/08/07 * Class description * -------------------- * This is the TwoD class. It is a two-dimensional array. It contains a default * constructor, a parameterized constructor, a value retrieval, a position in * the two dimensional array, an overloaded addition operator, and the big three. */ class TwoD { public: TwoD(); // Default constructor: User input TwoD(int MaxRows, int MaxCols); // Parametirzed constructor, programmer input double GetValue(int Row, int Col); // Retrieve a value. bool PointerValue(int Row, int Col, int MaxRows, int MaxCols, double newValue); // PointerValue is the "position" in the array. friend const TwoD operator+ (TwoD& arrayA, TwoD& arrayB); // Overloaded + operator TwoD(const TwoD& object); // copy constructor TwoD& operator =(const TwoD& rightSide); // Assignment operator ~TwoD(); // Destructor private: dPtr *ArrayPosition; int MaxRows, MaxCols; }; /* Function Name: Main * Class: None * Date: 03/08/07 * Function description * -------------------- * This is the main function. It takes user input and sends it to the other * functions of the program. */ int main() { TwoD arrayA, arrayB, arrayC; // Declaring the three TwoD objects containing // pointers to dynamic arrays. int userRows, userCols; cout << "How many rows will be in your arrays?" << endl; cin >> userRows; cout << endl << "How many columns will be in your arrays?" << endl; cin >> userCols; arrayA.TwoD(userRows, userCols); // Creating the arrays. return 0; } /* Function Name: TwoD::TwoD() * Class: None * Date: 03/08/07 * Function description * -------------------- * This is the default constructor. By default, each two dimensional array has * three rows and four columns. */ TwoD::TwoD() // Default constuctor { MaxRows = 2; // Maximum 3 rows MaxCols = 3; // Maximum 4 columns. dPtr *ArrayPosition = new dPtr[MaxRows]; // Pointer to a dynamic array of doubles. for(int i = 0; i < MaxRows+1; i++) { ArrayPosition[i] = new double[MaxCols]; // Fill the row up til max columns } } /* Function Name: TwoD::TwoD(int setRows, int setCols) * Class: None * Date: 03/08/07 * Function description * -------------------- * This section allows the user to set the number of rows and columns in an * instance of TwoD. */ TwoD::TwoD(int setRows, int setCols) // Parameterized Constructor. { MaxRows = setRows; MaxCols = setCols; dPtr *ArrayPosition = new dPtr[setRows]; // dynamically allocated array of double of setRows length for(int i = 0; i < MaxRows+1; i++) { ArrayPosition[i] = new double[MaxCols]; // Fill the row up til max columns } } /* Function Name: double TwoD::GetValue(int Row, int Col) * Class: None * Date: 03/08/07 * Function description * -------------------- * This section retrieves a value from the array for the user. */ double TwoD::GetValue(int Row, int Col) // Return the cell value. { double returnValue; returnValue = ArrayPosition[Row][Col]; return returnValue; } /* Function Name: bool TwoD::PointerValue(int Row, int Col, int MaxRows, int MaxCols, double newValue) * Class: None * Date: 03/08/07 * Function description * -------------------- * This section targets a cell in the array and changes it. If it works, it returns true. If not, * it returns false. */ bool TwoD::PointerValue(int Row, int Col, int MaxRows, int MaxCols, double newValue) // Put a value int { if (Row > MaxRows || Col > MaxCols) { if (Row > MaxRows) cout << endl << "You have entered an invalid row number."; if (Col > MaxCols) cout << endl << "You have entered an invalid column number."; return false; } ArrayPosition[Row][Col] = newValue; return true; } /* Function Name: const TwoD operator+ (TwoD& arrayA, TwoD& arrayB) * Class: None * Date: 03/08/07 * Function description * -------------------- * This is an overloaded addition operator, so that two instances of TwoD can be added * together. It will return a third array. */ const TwoD operator+ (TwoD& arrayA, TwoD& arrayB) // Overloaded + operator { TwoD sumArray; int Row = 0; int Col = 0; for (int i = 0; i < arrayA.MaxRows; i++) { for (int j = 0; j < arrayA.MaxCols; j++) { sumArray.ArrayPosition[Row][Col] = arrayA.ArrayPosition[Row][Col] + arrayB.ArrayPosition[Row][Col]; } } return sumArray; } /* Function Name: TwoD::TwoD(const TwoD &object) * Class: None * Date: 03/08/07 * Function description * -------------------- * This is a copy constructor. */ TwoD::TwoD(const TwoD &object) // Copy constructor { MaxRows = object.MaxRows; MaxCols = object.MaxCols; dPtr *ArrayPosition = new dPtr[MaxRows]; // dynamically allocated array of double of setRows length for(int i = 0; i < MaxRows+1; i++) { ArrayPosition[i] = new double[MaxCols]; // Fill the row up til max columns } for ( int i = 0; i < MaxRows + 1; i++ ) { for ( int j = 0; j < MaxCols; j++ ) { ArrayPosition[i][j] = object.ArrayPosition[i][j]; } } } /* Function Name: TwoD::~TwoD() * Class: None * Date: 03/08/07 * Function description * -------------------- * This is a destructor. */ TwoD::~TwoD() // Destructor { for(int i = 0; i < MaxRows; i++) delete[] ArrayPosition[i]; delete[] ArrayPosition; } TwoD& TwoD::operator =(const TwoD& rightSide) // Overloaded assignment operator { if (MaxRows != rightSide.MaxRows && MaxCols != rightSide.MaxCols) { for(int i = 0; i < MaxRows; i++) delete[] ArrayPosition[i]; delete[] ArrayPosition; dPtr *ArrayPosition = new dPtr[MaxRows]; // dynamically allocated array of double of setRows length for( int i = 0; i < MaxRows+1; i++) { ArrayPosition[i] = new double[MaxCols]; // Fill the row up til max columns } } MaxRows = rightSide.MaxRows; MaxCols = rightSide.MaxCols; for(int i = 0; i < MaxRows+1; i++) { ArrayPosition[i] = new double[MaxCols]; // Fill the row up til max columns } for ( int i = 0; i < MaxRows + 1; i++ ) { for ( int j = 0; j < MaxCols; j++ ) { ArrayPosition[i][j] = rightSide.ArrayPosition[i][j]; } } return *this; }