Mega Code Archive
A set class for characters
/*
Quote from: C++: A Beginner's Guide, Second Edition
# Publisher: McGraw-Hill Osborne Media; 2 edition (December 3, 2003)
# Language: English
# ISBN-10: 0072232153
# ISBN-13: 978-0072232158
*/
#include
using namespace std;
const int MaxSize = 100;
class Set {
int len; // number of members
char members[MaxSize]; // this array holds the set
/* The find() function is private because it
is not used outside the Set class. */
int find(char ch); // find an element
public:
// Construct a null set.
Set() { len = 0; }
// Return the number of elements in the set.
int getLength() { return len; }
void showset(); // display the set
bool isMember(char ch); // check for membership
Set operator +(char ch); // add an element
Set operator -(char ch); // remove an element
Set operator +(Set ob2); // set union
Set operator -(Set ob2); // set difference
};
/* Return the index of the element
specified by ch, or -1 if not found. */
int Set::find(char ch) {
int i;
for(i=0; i < len; i++)
if(members[i] == ch) return i;
return -1;
}
// Show the set.
void Set::showset() {
cout << "{ ";
for(int i=0; i