Lab 9. Constructors, Vectors, and Boxes

Lab 9. Constructors, Vectors, and Boxes.

How to submit your code

Each program (the source code .cpp file) should be submitted through Blackboard (Course Materials > Lab).

You can submit all your programs at the end of the lab session in one submission. This way, we can hopefully avoid the situation when you are quickly writing your program, immediately uploading it to Blackboard, but then, say 10 minutes later, realizing that there is a bug in it.

Basically, submit when you are sure that it will be your final version.

Each program should start with a comment that contains your name and a short program description, for example:

  Author: Your Name 
  Description: Lab 1. Task 1. Hello world

You can submit incomplete programs, but their “incomplete” status must be clearly mentioned in the comment to the program. In this case, also briefly describe what is implemented, and what is not.

Vector datatype

vector<T> is a template class that works essentially like an array, but with many improvements.


Accessing the elements of a vector

The elements of a vector are accessed the same way as the elements of a simple array:

vector<char> v(5);
v[0] = 'A';
v[4] = 'E';

Some member functions:

To read more about the class vector<T>, please refer to this article (in particular, read until the section Iterators).

Boxes, and boxes inside the boxes, and …

cardboard boxes

A box is a container that may contain other boxes. And there may be many levels of nested boxes inside …

There is only one condition: all boxes placed inside another box must be either all arranged horizontally, or all arranged vertically. Schematically, it is shown in the figure below.

How can we implement such a class Box?

Box is a very simple class that has two private member variables:

enum Direction { HORIZONTAL, VERTICAL };

class Box {
  Direction dir;
  std::vector<Box> children;




In your opinion, why do you think we could not use an array for storing internal boxes?

In this lab, we will be writing constructors for this class that make particular arrangements of boxes.

Printing out a box

You are provided with a function print for printing a box and all other boxes it contains.

An empty box (with no children) would look like:

| |

A box with two empty boxes arranged horizontally would be

|| || ||

and so on.

>> Download the source code <<

The width and the height of a box

This is not a part of the assignment, but it is worth mentioning that the printing function works using the public methods of the class Box that compute the width and the height of a box. This computation is done recursively:

The base case is the empty box, it has width = 3 (in characters), and height = 3.

To compute the width of a bigger box, assuming that the child boxes are placed horizontally, the total width is the sum the widths of the children +2 for margins. Similarly, the height is the max of the heights of the children +2 for margins.
(And of course, if the child boxes are arranged in the vertical direction, then the total width is the max, and the total height is the sum).

Another example of a complex arrangement of nested boxes:


Preliminary task (don’t submit).

Take the source code (boxes.cpp). Run the program, make a few boxes using two available constructors and print them out.

Task 1. Simple box. Constructor Box(Direction d, int num).

Add a constructor Box(Direction d, int num) that creates a box with num empty boxes arranged in the direction specified by the parameter d.

To test your implementation, write a main function similar to

int main() {
    Box b1(HORIZONTAL, 0);
    Box b2(HORIZONTAL, 1);
    Box b3(VERTICAL, 3);
    Box b4(HORIZONTAL, 5);


Task 2. Rectangular box. Constructor Box(int width, int height).

Add a constructor Box(int width, int height) that creates a box with height-many narrow boxes arranged vertically. And each of those child boxes should contain width-many empty boxes arranged horizontally.

For example, Box b(4,3) should create a box like this:

Write a main function to test the program. Test that the constructor works for various values of width and height greater or equal to 1. (You may assume that these two parameters cannot be smaller than 1.)

Task 3. Box from a vector of integers. Constructor Box(vector<int>).

Add another constructor Box(vector<int> v) that creates a box where the internal boxes are arranged horizontally, and each internal box itself contains v[i]-many children. As a result, we obtain something similar to a bar chart with the bars going down.

When the vector contains { 1, 4, 2, 0, 3, 1 }, the box should look like:

Write a main function to verify that the constructor works correctly.

Task 4. Fibonacci boxes. Constructor Box(int).

As we know, Fibonacci numbers are defined recursively as

F(0) = 1,
F(1) = 1,
F(n) = F(n-1) + F(n-2) for all n > 1.

Similarly, we can define Fibonacci boxes, FB(n):
When the paramenter n is equal to 0 or 1, it is an empty box. Otherwise, when n > 1, a Fibonacci box FB(n) is a made of two smaller boxes FB(n-1) and FB(n-2) composed horizontally:

Add (or edit) a constructor Box(int n) that generates such Fibonacci boxes. Also write a main function that prints out the Fibonacci boxes for n = 0, 1, 2, 3, 4, and 5.