//----------------------------------------------------------------- // // arrayso1.cpp // // Adapts arrayso to use dynamic memory allocation. Note that we first // allocate the memory for the array using new and at the end of the // program delete the memory with delete // // written by: Elizabeth Sklar, modified by Simon Parsons // modified : 25th April 2009 // //----------------------------------------------------------------- #include using namespace std; class Point { private: int x, y; public: Point() { } Point( int x0, int y0 ) : x(x0), y(y0) { } void set( int x0, int y0 ) { x = x0; y = y0; } int getX() { return x; } int getY() { return y; } void print() const { cout << "(" << x << "," << y << ") "; } }; int main() { Point *triagain = new Point[3]; // Dynamic memory allocation assert( triagain != 0 ); triagain[0].set( 0,0 ); triagain[1].set( 0,3 ); triagain[2].set( 3,0 ); cout << "tri-ing again: "; for ( int i=0; i<3; i++ ) { triagain[i].print(); } cout << endl; delete[] triagain; // Delete memory }