Exercises in this lecture   Go to the notes, in which this exercise belongs -- Keyboard shortcut: 'u'   Alphabetic index   Course home   

Exercise solution:
Use of constructors with object on the free store


Here is my version of the program:

#include <iostream>
#include "point.h"

using namespace std;

void f(){

  Point *p = new Point(),
        *q = new Point(),
        *r = new Point(11.0, 12.0),
        *s = new Point(*p),                      
        *t = new Point(3.0),           
        *u = new Point(*t),
        *ap[10]; 

  for(int i = 0; i < 10; i++)
     ap[i] = new Point();

  *q = *r;

  cout << "Point p: " << *p << endl;   // (0,7)
  cout << "Point q: " << *q << endl;   // (11,12)
  cout << "Point r: " << *r << endl;   // (11,12)
  cout << "Point s: " << *s << endl;   // (1,9)
  cout << "Point t: " << *t << endl;   // (3,20)
  cout << "Point u: " << *u << endl;   // (4,22)

  for(int i = 0; i < 10; i++)           // (0,7) ten times
      cout << "ap[" << i << 
     "]: " << *(ap[i]) << endl;  

  delete p; delete q; delete r; delete s; delete t; delete u;
  for(int i = 0; i < 10; i++) delete ap[i];

}

int main(){
  f();
}