动态内存分配
#include <iostream>
using namespace std;
class Point{
public:
//默认值的构造函数
Point():x(0),y(0){
cout << "Default Constructor called." <<endl;
}
//自定义构造函数
Point(int x, int y):x(x),y(y){
cout << "Constructor called." <<endl;
}
//析构函数
~Point(){
cout << "Destrutor called" << endl;
}
int getX() const{return x;}
int getY() const {return y;}
void move(int newX, int newY){
x = newX;
y = newY;
}
private:
int x,y;
};
int main()
{
cout << "Step one:" <<endl;
Point *ptr1 = new Point; //动态内存分配,调用默认构造函数
delete ptr1; //删除对象,自动调用析构函数
cout << "Step two:" <<endl;
ptr1 = new Point(1,2);
delete ptr1;
return 0;
}