Homework #2:   Classes and Vectors in C++

Assigned: Wednesday, Sept. 27
Due: Monday, Oct. 9   by midnight

Contents:


Overview

Topic: Intro to C++

Related Reading:

Please make sure you adhere to the policies on academic integrity, as defined in the course general information page.


Problems to be Submitted (25 points)

  1.   (10 points)

    Implement two classes, Person and Family, based on the provided interfaces (.h files):

    The two interface files (.h files) provide the requisite information about what each of the methods should do.

    Hint 1:   Take a look at CoordND.cpp for help in creating an array of the desired size, and creating the corresponding destructor.

    Hint 2:   The two methods, getParents() and getChildren(), in the Family class will require you to loop through all members of the family (i.e. the Person array, f) and find those whose "role" match the corresponding string(s) used to denote parents and kids. For this, you may want to use string's compare() method. See this link:

  2.   (5 points)

    For this problem, assume that C++'s vector class has a default capacity (initial size) of 2, and that whenever the vector's size increases, it's size is doubled.

    Given the following sequence of vector operations, show a diagram of the array contents and size after each operation executes, not counting the declaration (the first line). So, you should have eight diagrams when you're done.

        vector<int> nlist;
    
        nlist.push_back (5);
        nlist.push_back (12);
        nlist.insert (nlist.begin()+1, 4);
        nlist.push_back (1);
        nlist[0] = 2 * nlist[2];
        nlist.erase (nlist.begin()+2);
        nlist.push_back (3);
        nlist.insert (nlist.begin(), 7);
    

    In case you want to run this code, here's the actual .cpp file:   vec.cpp

  3.   (5 points)

    Consider the following block of code as a starting point:

        #include <iostream>
        #include <vector>
        using namespace std;
    
        vector<int> a;
        vector<int> b(100, 2222);
        vector<int> c(200);
        c.at(30) = 3333;
        vector<int> d(b);
        vector<int> *e = new vector<int>(100, 4444);
        vector<int> *f = new vector<int>(b);
    

    For each part of this problem, assume the given lines of code immediately follow the initial block of code given above (however, consider each of the following problems as independent; that is, the code given for part 'a' should not effect part 'b')

    Your goal is to explain the behavior of each code fragment. You are not simply to describe the generated output but must also explain why such output is generated.

    In fact, to help you out, here is a program allocation.cpp which will simulate this entire problem for you. Simply compile and execute the code and the main routine will perform a loop, independently simulating each of the six problem parts.

    1.     cout << b.at(30) << endl;
          cout << c[30] << endl;
          cout << d.at(30) << endl;
          c = b;
          d = *e;
          cout << b[30] << endl;
          cout << c.at(30) << endl;
          cout << d[30] << endl;
          cout << e->at(30) << endl;
      
    2.     cout << b[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;
      
    3.     cout << a.at(30) << endl;
          a = b;
          cout << a[30] << endl;
      
    4.     cout << b[30] << endl;
          cout << e->at(30) << endl;
          cout << f->at(30) << endl;
          f = &b;
          f->at(30) = 5555;
          cout << b.at(30) << endl;
          cout << e->at(30) << endl;
          cout << f->at(30) << endl;
          *f = *e;
          f->at(30) = 9999;
          cout << b[30] << endl;
          cout << e->at(30) << endl;
          cout << f->at(30) << endl;
      
    5.     cout << b.size() << endl;
          cout << &b[150] << endl;
          cout << b[150] << endl;
          b[150] = 9999;
          cout << b[150] << endl;
          cout << b.size() << endl;
          cout << &c[46] << endl;
          cout << c[46] << endl;
      
  4.   (5 points)

    Let's revisit the Family class that you implemented for the first problem of this assignment. By using a pointer-based array implementation, we had to set the size of the family upon creation of the family, and couldn't change that size later (at least, not without changing the interface).

    In contrast, if we move to a vector-based implementation, that would facilitate support for a growing family, while also simplifying the class interface.

    Using the new Family class interface, Family.vec.h, re-implement that Family class using a vector-based implementation.