C + + sub-file definition, array element traversal

Source: Internet
Author: User

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 "&LT;&LT;ENDL;} Coordinate::~coordinate () {cout<< "~coordinate" &LT;&LT;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

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.