Beijing University C + + programming Coursera course Fourth week in question 3

Source: Internet
Author: User

Questions -31 point Possible (graded)
Total time limit:
1000ms
Memory Limit:
65536kB
Describe

Write a two-dimensional array class Array2, so that the following program output is:

0,1,2,3,

4,5,6,7,

8,9,10,11,

Next

0,1,2,3,

4,5,6,7,

8,9,10,11,

Program:

#include <iostream> #include <cstring>using namespace std;
Add your code here
int main () {    Array2 a (3,4);    int i,j;    for (  i = 0;i < 3; ++i) for        (  j = 0; J < 4; j + +)            A[i][j] = i * 4 + j;    for (  i = 0;i < 3; ++i) {        for (  j = 0; J < 4; J + +) {            cout << A (i,j) << ",";        }        cout << Endl;    }    cout << "Next" << Endl;    Array2 b;     b = A;    for (  i = 0;i < 3; ++i) {        for (  j = 0; J < 4; J + +) {            cout << b[i][j] << ",";        }        cout << Endl;    }    return 0;}
Input
No
Output
0,1,2,3,
4,5,6,7,
8,9,10,11,
Next
0,1,2,3,
4,5,6,7,
8,9,10,11,
Sample input
No
Sample output
0,1,2,3,4,5,6,7,8,9,10,11,next0,1,2,3,4,5,6,7,8,9,10,11,

Answer:
#include <iostream> #include <cstring>using namespace std;class array2{int *ptr;    int row;//number of rows int col;//column Count public:array2 (int a,int b): Row (a), col (b) {ptr=new int[row]; for (int i = 0; i < row; i++) {Ptr[i] = reinterpret_cast<int> (new Int[col]);//convert int* to int}} ARR  Ay2 () {ptr=null;row=0;col=0;}    ~array2 () {for (int i = 0; i < row; i++) {delete []reinterpret_cast<int*> (ptr[i]);  } Delete []ptr;  } int* operator[] (int a) {return reinterpret_cast<int*> (Ptr[a]);    } array2& operator = (const Array2 &k) {if (ptr==k.ptr) return *this;      if (Ptr!=null) {for (int i = 0; i < row; i++) {delete []reinterpret_cast<int*> (ptr[i]);    } Delete []ptr;      } if (k.ptr==null) {ptr=null;      row=0;      col=0;    return *this; }
Ptr=new Int[k.row];     Row=k.row; Col=k.col;
for (int i = 0; i < row; i++) {Ptr[i] = reinterpret_cast<int> (new Int[col]); }
for (int i = 0; i < row, i++) for (int j = 0; J < Col; J + +) {(reinterpret_cast<int*> (ptr[i])) [J]= (re     Interpret_cast<int*> (K.ptr[i])) [j];}     cout<< "Done" <<endl;   return (*this);   } int& operator () (int a,int b) {return (*this) [a][b]; }};   int main () {Array2 A (3,4);   int i,j;   for (i = 0;i < 3; ++i) for (j = 0; J < 4; j + +) A[i][j] = i * 4 + j;     for (i = 0;i < 3; ++i) {for (j = 0; J < 4; J + +) {cout << A (i,j) << ",";   } cout << Endl;   } cout << "Next" << Endl;   Array2 b;   b = A;     for (i = 0;i < 3; ++i) {for (j = 0; J < 4; J + +) {cout << b[i][j] << ",";   } cout << Endl; } return 0;} The core difficulty is how the double [] operator is overloaded, thought is the first [] place address, and then the second [] can use the default addressing operation, so that only the first [] will be overloaded. In the implementation process, note the use of reinterpret_cast, or C + + type conversion.




Peking University C + + programming Coursera course Fourth Week 3

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.