1

How can one create a Set object that stores its elements on the heap in a dynamic array of int values? In particular, how does one create an empty set with these specifications?


//  Morally Right (M) 2014 1-ling. All morals preserved.

class Set 
{

public:

    Set();
    Set( int element );
    Set(const Set& s );
    ~Set();

    bool contains( unsigned int element );
    unsigned int getSize();

    int operator[]( unsigned int index ) const;
    Set& operator=( const Set s );
    Set operator+=( const Set s ) const;
    Set operator-=( const Set s ) const;
    Set operator*=( const Set s ) const;

    friend ostream& operator<<( ostream& o , const Set& s );
    friend istream& operator>>( istream& i , const Set& s );

private:

    int *array = new int[size]; // IS THIS THE PATH TO SUCCESS?
    int* elements();
    unsigned int size();
    void copy( const Set& s );
    void resize( unsigned int new_size );

};
trincot
  • 317,000
  • 35
  • 244
  • 286
  • Is `Set` a user-defined class or a name for some kind of container? – David G Jan 29 '14 at 23:42
  • If you go to UCLA, please don't copy this code. Do it yourself. I don't want the professor thinking I ripped this off if you turn in your work with the exact same structure. –  Jan 29 '14 at 23:46
  • What exactly is the problem with the code you have now? – David G Jan 29 '14 at 23:55
  • Am I looking for this: http://stackoverflow.com/questions/4029870/c-how-to-create-a-dynamic-array-of-integers –  Jan 29 '14 at 23:56
  • @0x499602D2, well, I don't know how to create a dynamic array of `int` values. –  Jan 30 '14 at 00:07
  • But the question you linked shows exactly how to do that. Using a `new` keyword. – jaho Jan 30 '14 at 00:09
  • 1
    Yes. Dynamic means that the size can be provided at runtime. However the main part of this assignment is that you will need to create, copy and delete dynamic arrays in each of the functions you mentioned. Also you shouldn't do it in your header file, but in your implementation file, generally. – jaho Jan 30 '14 at 00:20
  • http://meta.stackexchange.com/questions/12527/do-i-have-to-worry-about-copyright-issues-for-code-posted-on-stack-overflow – Karoly Horvath Jan 30 '14 at 00:20

1 Answers1

0

Normally I would recommend that you use std::vector<T>, but since your assignment tells you to use raw C-style arrays, I will have to reiterate my answer thusly:

If you're holding a resource (such as dynamic memory) you will have to implement the RAII idiom so that the resource is deallocated when encapsulating structure goes out of scope. For this you will need a destructor so that you can appropriately call delete/delete[]. Doing otherwise causes a memory leak if you're using dynamic memory and it's not deallocated.

But to allocate the resource, you will need a constructor that calls new/new[]. And to implement copying, you will need a copy-constructor that copies the resource from the other object to your object.


Now that I've laid out the instructions, you can start. Note that you already have the appropriate default-constructor, copy-constructor, and destructor to perform the aforementioned activities. To create a pointer to the memory, you can simply use int* element and assign it to memory.

For more information, see this post.

Community
  • 1
  • 1
David G
  • 94,763
  • 41
  • 167
  • 253
  • If he's allowed to used STL why not just use std::set? – jaho Jan 30 '14 at 00:01
  • I can't. Instructions: Your supervisor wanted you to make some design changes to your Set class. The biggest change is make your `Set` objects store their elements not in a vector of `int` values, but on the heap in a dynamic array of `int` values. As a consequence, you will need to redefine the copy constructor, assignment operator `=`, and destructor in order to prevent shallow copies and memory leaks. –  Jan 30 '14 at 00:03
  • I just love how the professor is inculcating the minds of tomorrow with the expectation that everyone will be working under a "supervisor." –  Jan 30 '14 at 00:04
  • @user3250884 Give me a few while I update my answer. – David G Jan 30 '14 at 00:06
  • saying that C-style arrays are bad practice is a bit harsh. there are plenty of valid use cases. – Karoly Horvath Jan 30 '14 at 00:24
  • @KarolyHorvath Sure I know, but it's best to discourage use of explicit `new` in favor of standard containers. – David G Jan 30 '14 at 00:25
  • What does it mean to have an empty set in this instance? –  Jan 30 '14 at 00:30
  • @user3250884 An empty set means that in the default-constructor you initialize `element` to `nullptr` in the member-initializer list. – David G Jan 30 '14 at 00:31
  • For the `Set( int element )` constructor I set `size=1`, correct? –  Jan 30 '14 at 00:34
  • @user3250884 I'm assuming `int element` is the value you want to add to the array, right? If so, then yes, the size should be equal to one. – David G Jan 30 '14 at 00:36
  • `Set::Set( int element ) { size = 1; array[0] = { element }; }` –  Jan 30 '14 at 00:50
  • @0x499602D2, should elements = element? –  Jan 30 '14 at 00:58
  • @user3250884 [let us continue this discussion in chat](http://chat.stackoverflow.com/rooms/46386/discussion-between-0x499602d2-and-user3250884) – David G Jan 30 '14 at 01:04
  • I just realized I made a mistake. The constructor that takes an integer should be the size of the array. Meaning `S::S(int elements) : array(new int[elements]), size(elements) {}`. Sorry! – David G Jan 30 '14 at 13:49