402a.cpp

来自「C++实训教程」· C++ 代码 · 共 47 行

CPP
47
字号
/*
	402a.cpp
	Written By S.Y.Feng
	demo using new and delete of array 
*/
#include <iostream.h>

class Point // define Point class
{
	int x;   // x and y are private by default
	int y;
  public:
   Point(){ x = 0; y = 0 ;}
   Point(int a,int b=0)  { x = a; y= b; }
   void Set  (int a,int b=0)  { x = a; y= b; }
	void Display()
	{
		cout << "x = "<< x
		     << " y = "<< y	<< endl;
	}
    ~Point() {cout << "Destruct obj.\n" ;}
};

int main()
{
	Point *p;int i;
	const int SZ = 3;
	p=new Point[SZ];
	if(!p)
	{  cout << "allocation failure\n";	return -1;  }
	for (i=0;i<SZ;i++)
		p[i].Set(i*10,-i*10);
	for (i=0;i<SZ;i++)
		p[i].Display();
	delete []p;
	return 0;
}
/*
x = 0 y = 0
x = 10 y = -10
x = 20 y = -20
Destruct obj.
Destruct obj.
Destruct obj.

*/

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?