402.cpp
来自「C++实训教程」· C++ 代码 · 共 38 行
CPP
38 行
/*
402.cpp
Written By S.Y.Feng
demo using new and delete
*/
#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 Display()
{
cout << "x = "<< x
<< " y = "<< y << endl;
}
~Point() {cout << "Destruct obj.\n" ;}
};
int main()
{
Point *p;
p=new Point(1,3);
if(!p)
{ cout << "allocation failure\n"; return -1; }
p->Display();
delete p;
return 0;
}
/*
x = 1 y = 3
Destruct obj.
*/
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?