C++类型数组的构造与析构

#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<< "Parameter Constructor called"<<endl;
    }

    ~Point() { cout<<"Destructor 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() {
    Point *ptr = new Point[5];
    ptr[0].move(5, 10);    
    ptr[1].move(15, 20);
    cout << "Deleting..." << endl;
    delete[] ptr;
    return 0;
}

 

#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<< "Parameter Constructor called"<<endl;
    }

    ~Point() { cout<<"Destructor 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;
}

 

posted on 2020-03-14 17:59  ~明月几时有  阅读(684)  评论(0)    收藏  举报