/*
 * File:    allocation.cpp
 * Author:  Michael H. Goldwasser
 * Date:    04 January 2005
 *
 * This program simulates six different blocks of code in conjunction
 * with a homework assignment.  The point is to explore several
 * subtleties in the allocation and subsequent assignment of objects
 * (in this case, STL vector's).
 *
 * This can be compiled as:
 *   g++ -o allocation allocation.cpp
 */

#include <iostream>
#include <vector>
using namespace std;    


int main() {
 

  for (int problem=1; problem<=6; problem++) try {

    cout << "---- starting problem " << problem << "-----" << endl;

    vector<int> a;
    vector<int> b(100);
    b.at(30) = 2222;
    vector<int> c(200);
    c.at(30) = 3333;
    vector<int> d(b);
    vector<int> *e = new vector<int>(100);
    vector<int> *f = new vector<int>(b);


    switch (problem) {

    case 1:	// problem (i)

      cout << b.at(30) << endl;
      cout << c.at(30) << endl;
      c = b;
      cout << b.at(30) << endl;
      cout << c.at(30) << endl;
      break;

    case 2:	// problem (ii)

      cout << b.at(30) << endl;
      cout << c.at(30) << endl;
      b = c;
      cout << b.at(30) << endl;
      cout << c.at(30) << endl;
      break;


    case 3:	// problem (iii)

      cout << a.at(30) << endl;
      a = b;
      cout << a.at(30) << endl;


    case 4:	// problem (iv)

      cout << b.at(30) << endl;
      cout << d.at(30) << endl;
      b.at(30) = 5555;
      cout << b.at(30) << endl;
      cout << d.at(30) << endl;
      break;


    case 5:	// problem (v)

      e->at(30) = 6666;
      cout << b.at(30) << endl;
      cout << e->at(30) << endl;
      cout << f->at(30) << endl;
      f = e;
      f->at(30) = 7777;
      cout << b.at(30) << endl;
      cout << e->at(30) << endl;
      cout << f->at(30) << endl;
      break;

    case 6:	// problem (vi)

      e->at(30) = 8888;
      cout << b.at(30) << endl;
      cout << e->at(30) << endl;
      cout << f->at(30) << endl;
      *f = *e;
      f->at(30) = 9999;
      cout << b.at(30) << endl;
      cout << e->at(30) << endl;
      cout << f->at(30) << endl;
      break;

    }

  } catch (...) {
    cout << "Unexpected exception occurred.\n";
  }

}
