#include "stdafx.h"#include <iostream>#include <stdlib.h>#include "Coordinate.h"using namespace std;/*****************************************//*对象数组*要求:* 定义Coordinate类* 数据成员分别是m_iX,m_iY;* 分别通过栈和堆实例化长度为3的对象数组* 给数组中元素赋值* 遍历两个数组******************************************/int main(void){ Coordinate coor[3]; coor[0].m_iX = 3; coor[0].m_iY = 5; for (int i = 0; i < 3;i++) { cout <<"coor_X="<<coor[i].m_iX << endl; cout<<"coor_Y="<< coor[i].m_iY << endl; } Coordinate *p = new Coordinate[3]; p->m_iX = 7; p[0].m_iY = 9; p++;//p+=1;p=p+1; p->m_iX = 11; p[0].m_iY = 13; p[1].m_iX=15; p++; p->m_iY = 17; for (int j = 0; j < 3;j++) { cout <<"p_X="<< p->m_iX << endl; cout <<"p_Y="<<p->m_iY << endl; p--; } p++; delete[]p; system("pause"); return 0;}//代码:Line.h#include <iostream>#include <stdlib.h>#include "Coordinate.h"using namespace std;class Line{public: Line(int x1,int y1,int x2,int y2); ~Line(); void setA(int x,int y); void setB(int x,int y); void printInfo();private:Coordinate m_coorA;Coordinate m_coorB;};//Line.cpp#include <iostream>#include <stdlib.h>#include "Coordinate.h"#include "Line.h"using namespace std;Line::Line(int x1,int y1,int x2,int y2):m_cooA(x1,y1),m_coorB(x2,y2)//初始化列表{ cout << "Line()" << endl;}Line::~Line(){ cout << "~Line()" << endl;}void Line::setA(int x, int y) { m_coorA.setX(x); m_coorB.setY(y);}void Line::setB(int x, int y){ m_coorA.setX(x); m_coorB.setY(y);}void Line::printInfo(){ cout <<"("<<m_coorA.getX()<<","<<m_coorA.getY()<<")"<< endl; cout <<"("<<m_coorB.getX()<<","<<m_coorB.getY()<<")"<< endl;}#include <iostream>#include <stdlib.h>using namespace std;class Coordinate{public: Coordinate(); ~Coordinate(); void setX(int x); int getX(); void setY(int y); int getY();public: int m_iX; int m_iY;};#include "stdafx.h"#include <iostream>#include <stdlib.h>#include "Coordinate.h"using namespace std;Coordinate::Coordinate(){ cout <<"Coordinate() " <<m_iX<<","<<m_iY<< endl;}Coordinate::~Coordinate(){ cout << "~Coordinate() "<<m_iX<<","<<m_iY<< endl;}void Coordinate::setX(int x){ m_iX = x;}int Coordinate::getX(){ return m_iX;}void Coordinate::setY(int y){ m_iY = y;}int Coordinate::getY(){ return m_iY;}
添加回答
举报
0/150
提交
取消