/******************************* NEWDELET.CPP ********************************** Example showing the use of new and delete.                       1998-09-15 *
*                                                                   Agner Fog *
*                                                                             *
* This example shows different uses of new and delete.                        *
* An object of class vector is created in main with the statement:            *
* v = new vector;                                                             *
* where v is a pointer to the new object. An object created with new has to   *
* be deleted with delete when you don't need it any more. This is done at the *
* end of main with the statement                                              *
* delete v;                                                                   *
*                                                                             *
* new can also be used for creating an array. This is useful when the size of *
* the array is not known in advance.                                          *
* The member function vector::ReadList creates an array with n elements in    *
* the statement:                                                              *
* list = new float[n];                                                        *
* where list is a pointer to the new array. This array is deleted in the      *
* destructor vector::~vector() using this statement:                          *
* delete[] list;                                                              *
* Note, that some compilers require an empty square bracket after delete when *
* deleting an array.                                                          *
* The member function vector::squares() also creates an array:                *
* float * squarelist = new float[length];                                     *
* There are two reasons for creating this array with new: The first reason is *
* that we need not know the size of the array in advance. The other reason is *
* that the function cannot return a pointer to an array that is declared      *
* inside the function because such an array would be destroyed when the       *
* function returns.                                                           *
* The array created in vector::squares() is deleted in main:                  *
* delete[] sq;                                                                *
*                                                                             *
* Remember that an object cannot be deleted more than once. You will get an   *
* error if you attempt to delete an object that has already been deleted.     *
* You also get an error if you attempt to delete an object that has been      *
* created in any other way than with new.                                     *
******************************************************************************/

#include <iostream.h>
#include <conio.h>

class vector {
   public:
   vector ();
   ~vector();
   void ReadList(int n);
   float sum();
   float * squares();
   private:
   float * list;
   int length;
};

// constructor
vector::vector () {
   length = 0;
   list = NULL;}

// destructor
vector::~vector () {
   if (length) {
      // delete array created by member function ReadList:
      delete[] list;}}

// member function to read a list of n numbers
void vector::ReadList(int n) {
   // make sure value of n is reasonable:
   if (n < 1 || n > 1000) return;
   // delete any previous array:
   if (length) {
      delete[] list;}
   // create a new array of size n:
   list = new float[n];
   length = n;
   // read into new array:
   for (int i = 0; i < n; i++) {
      cout << "\nEnter value number " << (i+1) << ": ";
      cin >> list[i];}}

// member function to calculate the sum of the numbers
float vector::sum() {
   float s = 0;
   for (int i = 0; i < length; i++) s += list[i];
   return s;}

// member function to return an array containing the squares
float * vector::squares() {
   if (!length) return NULL;
   // make dynamic array for squares:
   float * squarelist = new float[length];
   // calculate squares:
   for (int i=0; i<length; i++) {
      squarelist[i] = list[i] * list[i];}
   // return array of squares:
   return squarelist;}


void main() {
   vector * v; float * sq;
   // make new object
   v = new vector;
   // call member function ReadList
   v->ReadList(5);
   // call member function sum
   cout << "\n\nSum = " << v->sum();
   // call member function squares
   sq = v->squares();
   if (sq != NULL) {
      // output contents of array sq
      cout << "\n\nSquares =";
      for (int i=0; i<5; i++) cout << " " << sq[i];
      // delete dynamic array created by function squares
      delete[] sq;}
   // delete object created by main
   delete v;
   // wait for user to press key
   getch();}

