对象的动态创建与销毁!

#include 
using namespace std;
class Point
{
private:
    int x;
    int y;
public:    
    Point(int a = 0,int b = 0)
    {
        x = a;
        y = b;
    }
    void setXY (int u,int v)
    {
        x = u;
        y = v;
    }
    void display()
    {
        cout <<" ( " << x <<" , " << y <<" ) "<< endl;
    }
};

int main()
{
    int num,i,x,y;
    Point *p,*q;
    cout << "请输入点point的个数:"<< endl;
    cin >> num;
    p = q = new Point[num];
    for ( i = 0;i < num;i++)
    {
        cout << "请输入x;"<<endl;
        cin >> x;
        cout << "请输入y;"<<endl;
        cin >> y;
        q->setXY(x,y);
        q++;
    }
    q = p;
    cout << "一共有" << num << "个点" << endl; 
    for (i = 0;i < num;i++)
    {
        q ->display();
        q++;
    }                         
    delete p;    
    return 0;
}

 

转载于:https://www.cnblogs.com/666fengmo666/p/7804816.html

你可能感兴趣的:(对象的动态创建与销毁!)