1, demo.cpp as follows:
Iterating through the elements in the array, the so-called traversal is the printing of elements in an array,yiban#include<iostream> #include <stdlib.h> #include "Coordinate.h" Using namespace std;int main (void) {coordinate coor[3];//defines a coordinate, the way the stack accesses the coor[0].m_ix=3; coor[0].m_iy=5; coordinate *p=new coordinate[3];//instantiates an object in a heap p->m_ix=7;//a pointer to the array element the first p[0].m_iy=9;//is indexed p++;// Points to the second element p=p+1;p->m_ix=11;//the second element p[0].m_iy=13;p[1].m_ix=15;//accesses the third element p++;//p points to the third element p->m_iy=17;for (int i=0;i<3;i++)//Traversal of the first array {cout<< "coor_x" <<coor[i].m_iX<<endl;cout<< "Coor_y" < <coor[i].m_iy<<endl;} for (int j=0;j<3;j++) {cout<<p->m_ix<<endl;cout<<p->m_iy<<endl;p--;// The first loop points to the third element, because the previous P has pointed to the third element, so p--let her point back to}p++;d elete []p;//added []. It is because all objects of the array should be pins, or only one destructor is executed. There will be a memory leak for so long. As can be seen from the running result, three destructors were executed. P=null;system ("pause"); return 0;} 2, Coordinate.cpp as follows: #include <iostream> #include "Coordinate.h" using namespace std; Coordinate::coordinate () {cout<< "Coordinate "<<ENDL;} Coordinate::~coordinate () {cout<< "~coordinate" <<ENDL;}
3.
Coordinate.h such as: #include <iostream>using namespace Std;class coordinate {public:coordinate (); ~coordinate (); Public:int m_ix;int M_iy;};
The results of the operation are as follows:
650) this.width=650; "src=" Https://s5.51cto.com/wyfs02/M02/A4/BA/wKioL1mwogyyGtcjAAAmW0CT-FE749.png "title=" Untitled. png "alt=" Wkiol1mwogyygtcjaaamw0ct-fe749.png "/>
2.
650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M00/06/0A/wKiom1mwpjWCBn5iAABTMnfQnXY770.png "title=" Untitled. png "alt=" Wkiom1mwpjwcbn5iaabtmnfqnxy770.png "/>
C + + sub-file definition, array element traversal