NEED THIS IN C++ AND PLEASE MAKE SURE IT COMPILES BEFORE JUST THROWING A CODE OU
ID: 641294 • Letter: N
Question
NEED THIS IN C++
AND PLEASE MAKE SURE IT COMPILES BEFORE JUST THROWING A CODE OUT THERE>.....
Reread the code in Display 10.9 (Which is attached at the bottom) . Then, write a class TwoD that implements the
two-dimensional dynamic array of double s using ideas from this display in its
constructors. You should have a private member of type pointer to double to point
to the dynamic array, and two int (or unsigned int ) values that are MaxRows and
MaxCols .
You should supply a default constructor for which you are to choose a default
maximum row and column sizes and a parameterized constructor that allows the
programmer to set maximum row and column sizes.
Further, you should provide a void member function that allows setting a particular
row and column entry and a member function that returns a particular row and
column entry as a value of type double .
Remark: It is difficult or impossible (depending on the details) to overload [ ] so
it works as you would like for two-dimensional arrays. So simply use accessor and
mutator functions using ordinary function notation.
Overload the + operator as a friend function to add two two-dimensional arrays.
This function should return the TwoD object whose i th row, j th column element is
the sum of the i th row, j th column element of the left-hand operand TwoD object and
the i th row, j th column element of the right-hand operand TwoD object.
Provide a copy constructor, an overloaded operator = , and a destructor.
Declare class member functions that do not change the data as const members.
DISPLAY 10.9:
1 #include <iostream>
2 using namespace std;
3 typedef int* IntArrayPtr;
4 int main( )
5 {
6 int d1, d2;
7 cout << "Enter the row and column dimensions of the array: "
8 cin >> d1 >> d2;
9 IntArrayPtr *m = new IntArrayPtr[d1];
10 int i, j;
11 for (i = 0; i < d1; i++)
12 m[i] = new int[d2];
13 //m is now a d1-by-d2 array.
14 cout << "Enter " << d1 << " rows of "
15 << d2 << " integers each: ";
16 for (i = 0; i < d1; i++)
17 for (j = 0; j < d2; j++)
18 cin >> m[i][j];
19 cout << "Echoing the two-dimensional array: ";
20 for (i = 0; i < d1; i++)
21 {
22 for (j = 0; j < d2; j++)
23 cout << m[i][j] << " ";
24 cout << endl;
25 }
26
27 for (i = 0; i < d1; i++)
28 delete[] m[i];
29 delete[] m;
30 return 0;
31 }
Explanation / Answer
Hey i can help you with this. Contact me on hunain60 at gmail
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.