• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 442
  • Last Modified:

Help me with some ideas

I wrote this program about an integer set and the source code is below.  My friend challenged me to use templates e.g. ints floats and strings.  I am pasting my program below.

Could you please give any suggestions what to do 'cos i am a little confused...
And for those who think that i am trying to have my homework done.. i repeat, this is for my own good.  I only need suggestions.. please do help.

Thank you.

/***************************************************************************
***** This program will create a class called IntegerSet and each object *****
***** of class can hold integers in the range 0 through 100.             *****      
*****************************************************************************/



 #include <iostream.h>
  class IntegerSet
{
 
   private:
    int a[101];
 
   public:
    IntegerSet();
    IntegerSet(int b, int c, int d, int e, int f);
 
    void unionOfIntegerSets(IntegerSet a1, IntegerSet a2);
    void intersectionOfIntegerSets(IntegerSet a1, IntegerSet a2);
    void insertElement(int k);
    void deleteElement(int k);
    void setPrint();
    bool isEqualTo(IntegerSet a1);
    int SetElement(int i);
};


      IntegerSet::IntegerSet()
{
      int i;
      for (i = 0; i < 101; i++)
 {
    a[i]=0;
 }
}

    IntegerSet::IntegerSet(int b, int c, int d, int e, int f)
{
    int i=0;
    while(i < 101)
 {
    a[i]=0;
    i=i+1;
 }

    a[b]=1;
    a[c]=1;
    a[d]=1;
    a[e]=1;
    a[f]=1;
}

    int IntegerSet::SetElement(int i)
{
    return a[i];
}

    void IntegerSet::unionOfIntegerSets(IntegerSet a1, IntegerSet a2)
{
    int i=0;
    while(i < 101)
 {
    if (a1.SetElement(i)==1)
  {
    a[i]=1;
      }
    else
  {
    if (a2.SetElement(i)==1)
   {
    a[i]=1;
   }
  }
    i=i+1;
 }
}


    void IntegerSet::insertElement(int k)
{
    if(a[k]==0)
 {
    a[k]=1;
 }       
}


    void IntegerSet::deleteElement(int m)
{
    if(a[m]==1)
 {
    a[m]=0;
 }
}
                                                      
    void IntegerSet::intersectionOfIntegerSets(IntegerSet a1, IntegerSet a2)
{
    int i=0;
    while(i < 101)
 {
    if(a1.SetElement(i)==1 && a2.SetElement(i)==1)
  {
    a[i]=1;
  }
    i=i+1;
 }
}


    void IntegerSet::setPrint()
{
    int i=0;
    bool empty=true;

    while(i < 101)
 {
    if(a[i]==1)
  {
    cout << i << " ";
    empty=false;
  }
    i=i+1;
 }

    if(empty)
 {
    cout << "---";
 }
    cout << "\n";
}


    bool IntegerSet::isEqualTo(IntegerSet a1)
{
    int i=0;
    while(i < 101)
 {
    if(a[i] != a1.SetElement(i))
  {
    return false;
  }
    i=i+1;
 }
    return true;
}

    void main()
{
    IntegerSet a(80, 50, 100, 12, 15), b(67, 50, 15, 80, 43), c, un, intrsctn;

    cout << "a: ";
    a.setPrint();
    cout << "b: ";
    b.setPrint();

    if(a.isEqualTo(b))
 {
    cout << "These sets are equal\n";
 }
    else
 {
    cout << "These sets are unequal\n";
 }

    cout << "Their union:\n";
    un.unionOfIntegerSets(a, b);
    un.setPrint();

    cout << "Their intersection:\n";
    intrsctn.intersectionOfIntegerSets(a, b);
    intrsctn.setPrint();

    cout << "c: ";
    c.setPrint();

    a.deleteElement(12);
    a.deleteElement(100);
    cout << "Delete 12 and 100 from a1. Now\na:";
    a.setPrint();

    a.insertElement(67);
    a.insertElement(43);
    cout << "Insert 43 and 67 into a1. Now\na:";
    a.setPrint();

    if(a.isEqualTo(b))
 {
    cout << "a equals b\n";
 }
}
0
ash2902
Asked:
ash2902
1 Solution
 
SlartiCommented:
1. Change the top of the class declaration to:
template <class T> class Set
{  // continue normally

2. Inside the class declaration, change all occurences of int to T.
e.g. T a[101];

3. Change all member function definitions so that they start with 'template <class T>', for example:
template <class T> Set<T>::Set()
{ // continue function normally, replacing all int's with T's ...

4. To create an object use the following:
Set<int> set1; // create a Set of int's.
Set<float> set2; // create a Set of float's.

If you want more details, add more points...

0
 
ash2902Author Commented:
Adjusted points to 15
0
 
VEngineerCommented:
If you want to parameterize the set using templates as suggested by slarti, you should probably choose a different implementation instead of a static array of 100.  What happens if you make a set of float or char?  The array implementation doesn't quite work right.

If you want to create a custom set class (not using the STL) that can hold an unrestricted amount of any type of object and also provide functionality such as union, intersect, difference, equality, subset, size, etc... let me know and I'll be glad to help out.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
alexoCommented:
The STL comes with a set container.  Use it instead of reinventing the wheel.
0
 
VEngineerCommented:
I agree with Alexo, but if you are learning C++, it never hurts to make one of your own for practice sake.
0
 
ash2902Author Commented:
i am still having problems...
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now