📄 402b.cpp
字号:
/*
402b.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->Display();p++;
}
//delete []p; Error!!pointer is not restored
return 0;
}
/*
x = 0 y = 0
x = 10 y = -10
x = 20 y = -20
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -