this is a homework task im completly lost in?

we need to implement this header file below

i like to think im pretty good at arrays and classes.... but the new additon of dynamic arrays has got me really confused... any help would be greatly appreciated

#include <iostream>
using namespace std;

class Collector {
public:
  // make a collector for things
  Collector(); /// The constructor should allocate (with operator new) the initial array of strings
  ~Collector(); // Make sure that the destructor cleans up all of the storage allocated by this object
  Collector(Collector &other); // Copy constructor should perform a deep copy of other

  // collect a thing of type string
  // the string is added to the array of things
  // you MUST make use of the 'next' private member
  // you should check there is available space in the array to add this new string
  // use grow() if there is not enough space
  void collect(string name);

  // return the current size of the collection (NOT the maximum size of the collection)
  int size();

  // get element i of the collection
  // checks that i is in bounds of the non null portion of the collection and returns "" if not
  string get(int i);

  // print all non null elements of the collection with a space between each element
  void print();
protected: // local helper functions go here
  void grow();
  // grow the collection by INITSIZE eg. current max size is 20, this function should grow it to 30
  //  - called if the things array doesn't have room for any extra things

private:

  static const int INITSIZE = 10;  // initial size of the array and amount we grow by each time
  string *things;           // pointer to our array of things
  int arraysize;            // current maximum size of the array
  int next;                 // location of the next free (available) element in the array

};

Recommended Answers

All 4 Replies

collector:: collector()
{
    int* a = NULL;  
    int n;         

    a = new int[n];
}

?

About this (bad) interface:
1. Bad copy constructor signature. Avoid non-const copy constructor argumen(s). Change to common:

Collector(const Collector&);

2. You shall overload an assignment operator or make it private (unmovable objects) for such classes with dynamic storage allocation; Add:

Collector& operator=(const Collector&);

Otherwise you declare a bomb: one morning you will assign this class object and your program will die. Default assignment operator is not suitable for such classes.
3. Redefine collect member signature to:

void collect(const string&);

It's much more effective interface.
4. Declare this class destructor as virtual:

virtual ~Collector();

Now you have more effective and (for the most part) correct class with desired functionalty.

Forget this awful constructor as soon as possible ;)
Read this: http://www.daniweb.com/forums/announcement8-2.html
and this:http://www.daniweb.com/forums/announcement8-3.html

To be continued ;)...

we have to implement this header file tho
and have no idea at all where to begin!

we have to implement this header file tho
and have no idea at all where to begin!

Well, I don't want to be an accessory to a crime against C++...
Good luck!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.