17-DynamicAllocation

# 17-DynamicAllocation - #include &lt;iostream&gt; using...

This preview shows pages 1–2. Sign up to view the full content.

#include <iostream> using namespace std; class Point { private: float x, y; public: Point(float x1 = 0.0f, float y1 = 0.0f) { setCoordinates(x1, y1); } void setCoordinates(float x1, float y1) { x = x1; y = y1; } float getX() const { return x; } float getY() const { return y; } void print() const { cout << "(" << x << ", " << y << ")" << endl; } }; int main (void) { // Declaring objects Point p1; //call constructor const int y = 5; Point points[y]; //declare array of 5 Point objects Point *pointer; //declare pointer of type Point pointer = &points[0]; //set pointer to first element of pointer object array Point &reference = p1; //get mem addr of p1 object p1.setCoordinates(1, 2); points[3].setCoordinates(3, 4); //mutate 4th element of object array pointer->setCoordinates(5, 6); //mutate 1st element of object array cout << "p1.print() "; p1.print(); cout << "points[3].print() "; points[3].print(); cout << "pointer->print() "; pointer->print(); cout << "points[0].print() "; points[0].print(); cout << "reference.print() "; reference.print();

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 10/05/2011 for the course CS 37 taught by Professor Glenncuevas during the Summer '09 term at Irvine Valley College.

### Page1 / 3

17-DynamicAllocation - #include &lt;iostream&gt; using...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online