1. 程式人生 > >C++ 物件指標練習

C++ 物件指標練習

#include <iostream>
#include  <stdlib.h>
using namespace std;
class Coordinate
{

public:
	Coordinate(int x, int y)
	{
		// 設定X,Y的座標
		m_iX = x;
		m_iY = y;

	}
public:
	int m_iX;
	int m_iY;
};

int main(void)
{
	// 在堆上建立物件指標
	Coordinate *p = new Coordinate(3,5);
	// 列印座標
	cout << p->m_iX << "," << p->m_iY << endl;
	// 銷燬物件指標
	delete p;
	p = NULL;
	return 0;
	system("pause");
}

執行結果: