My assignment is to basically design a set class that creates a "set" of any data type and is able to perform basic functions. It needs to use an array to store it's elements and operate like a C++ set, meaning no duplicates. I need to be able to add an element to the set, remove an element from the set, check if the set is full or empty, display the set, input elements to the set (via keyboard or file), and also get the union and intersection of two sets.

I have to overload the << operator to display the set, overload the >> operator to input (keyboard or file) to the set, overload the + operator to get the union, and overload the * operator to get the intersection.

I've created the header file and believe I have set it up correctly for my class. And am now working on the Set.cpp file to define the methods and such. Now I'm not sure what to do and am confused about using an array to store the elements of the set. I would love to be able to just add/remove elements from the set and check if it's full/empty (I don't have a driver yet, so I can't test it yet). But I'm just completely confused with using the array and actually setting up a "set". I'm not asking anyone to write code for me, just need help on where to go from here.

Set.h

#if ! defined (SET_H)
#define SET_H

#include <iostream>

using namespace std;

const int MAX_LENGTH = 5;

template<class T>
class Set
{
private:
    T data[MAX_LENGTH];
    int length;
public:
    Set ();
    void Add(const T& item);
    void Remove(const T& item);
    bool isFull() const;
    bool isEmpty() const;
    void operator>>(const Set& T) const; //Input Set
    void operator<<(const Set& T) const; //Display Set
	void operator+(const Set& T) const; //Union
    void operator*(const Set& T) const; //Intersection
	
};

#endif  // end SET_H

Set.cpp

#include "Set.h"
#include <iostream>

using namespace std;

template<class T>
Set<T>::Set ()
{
    length = 0;
}

template<class T>
bool Set<T>::isEmpty() const
{
    return (length == 0);
}

template<class T>
bool Set<T>::isFull() const
{
    return (length == MAX_LENGTH);
}

template<class T>
void Set<T>::Add(const T& item)
{
    data[length] = item;
    length++;
}

template<class T>
void Set<T>::Remove(const T& item)
{
    int index = 0;
    
    while (index < length && item != data[index])
        index++;
    if (index < length)
    {
        data[index] = data[length - 1];
        length--;
    }
}

//Input
template<class T>
void Set::operator>>(const Set& T) const
{
    return 0;
}

//Display
template<class T>
void Set::operator<<(const Set& T) const
{
    return 0;
}

//Union
template<class T>
void Set::operator+(const Set& T) const
{
    return 0;
}

//Intersection
template<class T>
void Set::operator*(const Set& T) const
{
    return 0;
}

I changed up my code a little bit, added some things and am now getting the following error when I try to compile:


Set.cpp:57: error: expected constructor, destructor, or type conversion before ‘Set’
Set.cpp:74: error: expected constructor, destructor, or type conversion before ‘Set’
Set.cpp:86: error: expected constructor, destructor, or type conversion before ‘Set’
Set.cpp:93: error: expected constructor, destructor, or type conversion before ‘Set’


Here is my updated code, any help would be greatly appreciated.

Set.h

#if ! defined (SET_H)
#define SET_H

#include <iostream>

using namespace std;

const int MAX_LENGTH = 5;

template<class T>
class Set
{
private:
    T data[MAX_LENGTH];
    int length;
public:
    Set ();
    void Add(const T& item);
    void Remove(const T& item);
    bool isFull() const;
    bool isEmpty() const;
    Set operator>>(const Set& T); //Input Set
    Set operator<<(const Set& T); //Display Set
	Set operator+(const Set& T); //Union
    Set operator*(const Set& T); //Intersection
	
};

#endif  // end SET_H

Set.cpp

#include "Set.h"
#include <iostream>

using namespace std;

template<class T>
Set<T>::Set ()
{
    length = 0;
}

template<class T>
bool Set<T>::isEmpty() const
{
    return (length == 0);
}

template<class T>
bool Set<T>::isFull() const
{
    return (length == MAX_LENGTH);
}

template<class T>
void Set<T>::Add(const T& item)
{
    data[length] = item;
    length++;
}

template<class T>
void Set<T>::Remove(const T& item)
{
    int index = 0;
    
    while (index < length && item != data[index])
        index++;
    if (index < length)
    {
        data[index] = data[length - 1];
        length--;
    }
}

//Input
template<class T>
Set Set::operator>>(const Set& T)
{
    for(int i = 0; i < MAX_LENGTH; i++)
    {
        T s;
        cin >> s;
        data[i] = s;
        length++;
        if(length == MAX_LENGTH)
        {
            break;
        }
    }
}

//Display
template<class T>
Set Set::operator<<(const Set& T)
{
    cout << "{";
    for(int i = 0; i < MAX_LENGTH; i++)
    {
        cout << data[i] << ", ";
    }
    cout << "}";
}

//Union
template<class T>
Set Set::operator+(const Set& T)
{
    return 0;
}

//Intersection
template<class T>
Set Set::operator*(const Set& T)
{
    return 0;
}
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.