By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,851 Members | 2,104 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,851 IT Pros & Developers. It's quick & easy.

creating a Set in C++ using classes (need help on an exercise)

P: n/a
Hello, I have an exercise that I need to finish very soon and I really
need help understanding what to do and how exactly to do it. I am
working on reading the chapter right now and working on it myself, but
I have a feeling I won't get far. Please help me complete the
exercise. I am posting what needs to be done and will be updating
with pieces of code that I come up with. Thank you all for your
attention.
-----------------------------------------
// Exercise text

Implement a Set class, where a set is an unordered collection of zero
or more elements with no duplicates. For this exercise, the elements
should be ints. The public interface consists of methods to

- Create s Set.
- Add a new element to a Set.
- Remove an elemnt from a Set.
- Enumerate the elements in the Set.
- Compute the intersection of two Sets S1 and S2, that is, the set of
elemtns that belong to both S1 and S2.
- Compute the union of two Sets S1 and S2, that is, the set of
elements that belong to S1 or S2 or both.
- Compute the difference of two Sets S1 and S2, that is, the set of
elements that belong to S1 but not to S2.
------------------------------------------
// Instructor't notes

implement with a linked list, using classes

intent: to write classes correctly
Set.h has class declarations (including inlines)
Set.cpp has method definitions for the class
main.cpp is the "driver" (i.e. uses the Set class)
if l is a linked list, is x already on the list?
bool search(x,l) /* search for x on list l*/
if l is null return false
if l->info == x return true
return search(x, l->next)

assume you store the items in order
how to do union of two sets?

create a copy constructor which copies the set -- if you dont, any
copies will destroy the original
when they are destroyed.

of course, your destructor must delete any dynamically
allocated storage

destructor():
if l == null return;
destructor(l->next);
delete(l);

your destructor will call delete on each dynamically
created element. when you creat a copy, if its a
shallow copy, the originals dynamically allocated
parts will be destroyed. (you implicity make copies
when you do call by value, return by value,
initialization copies (i.e. when the copy constructor
is called) the copies are destroyed when they go out
of scope).

-------------------------------------------------------------------

Mar 10 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On 10 Mar, 05:18, "Richard Gromstein" <rgr...@gmail.comwrote:
I have a feeling I won't get far. Please help me complete the
exercise. I am posting what needs to be done and will be updating
with pieces of code that I come up with. Thank you all for your
attention.
<...>

Break it down. Your instructor told you where to start. Ignore the set
part and complete the first part...
// Instructor't notes

implement with a linked list, using classes.
So try to make a linked list...
If and when you get that to work then you will have partially
completed the task (and so may get some marks) and also may be able to
move on.

regards
Andy Little
Mar 10 '07 #2

P: n/a
kwikius wrote:
On 10 Mar, 05:18, "Richard Gromstein" <rgr...@gmail.comwrote:

>>I have a feeling I won't get far. Please help me complete the
exercise. I am posting what needs to be done and will be updating
with pieces of code that I come up with. Thank you all for your
attention.


<...>

Break it down. Your instructor told you where to start. Ignore the set
part and complete the first part...

>>// Instructor't notes

implement with a linked list, using classes.


So try to make a linked list...
If and when you get that to work then you will have partially
completed the task (and so may get some marks) and also may be able to
move on.

regards
Andy Little

Good advice, to which I would add, don't underestimate the time it will
take you to get a linked list working, if you have never done it before
(unless of course you are a programming genius). Sounds like you've left
it a bit late already. Don't be afraid to ask for an extension.

john
Mar 10 '07 #3

P: n/a
Richard Gromstein wrote:
Hello, I have an exercise that I need to finish very soon and I really
need help understanding what to do and how exactly to do it. I am
working on reading the chapter right now and working on it myself, but
I have a feeling I won't get far. Please help me complete the
exercise. I am posting what needs to be done and will be updating
with pieces of code that I come up with. Thank you all for your
attention.
Show us what you already have. We won't do your homework for you.
[problem statement redacted]
Mar 10 '07 #4

P: n/a
Working on a linked list creation. Digging through old C notes and
reading online tutorials... I'll post what I have when I get
something solid.

Mar 11 '07 #5

P: n/a
Working on a linked list creation. Digging through old C notes and
reading online tutorials... I'll post what I have when I get
something solid.

Mar 11 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.