Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > creating a Set in C++ using classes (need help on an exercise)

Reply
Thread Tools

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

 
 
Richard Gromstein
Guest
Posts: n/a
 
      03-10-2007
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).

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

 
Reply With Quote
 
 
 
 
kwikius
Guest
Posts: n/a
 
      03-10-2007
On 10 Mar, 05:18, "Richard Gromstein" <(E-Mail Removed)> wrote:

> 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


 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      03-10-2007
kwikius wrote:
> On 10 Mar, 05:18, "Richard Gromstein" <(E-Mail Removed)> wrote:
>
>
>>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
 
Reply With Quote
 
red floyd
Guest
Posts: n/a
 
      03-10-2007
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]

 
Reply With Quote
 
denvorontsov@gmail.com
Guest
Posts: n/a
 
      03-11-2007
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.

 
Reply With Quote
 
Richard Gromstein
Guest
Posts: n/a
 
      03-11-2007
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Using property() to extend Tkinter classes but Tkinter classesare old-style classes? Terry Reedy Python 5 11-30-2010 06:49 PM
how to link classes using pointers of their derived classes ivan.leben@gmail.com C++ 6 09-07-2006 07:00 AM
OOP casting classes (using one single variable for similar types of classes) roberts.noah@gmail.com C++ 8 01-07-2006 04:13 PM
Classes within classes David ASP .Net 2 07-22-2005 07:13 PM
Can I using reflection to get all child classes or classes undera package dynamically? Carfield Yim Java 1 05-31-2004 05:33 PM



Advertisments