Question requirement

Pages: 123
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>
#include <conio.h>

using namespace std;

template< class T >
class Item{

private:

public:
	Item();
	~Item();

};



int main(){

	system( "pause" );//Pause window
	return 0;//Exit program
}


Write a template class that implements a set of items. A set is a collection of items in which no item occurs more than once. Internally, you may represent the set using the data structure of your choice (e.g. list, arrays, etc). However the class should externally support the following functions:

 Add a new item to the set. If the item is already in the set then nothing happens.
 Remove an item from the set.
 Return the number of items in the set.
 Determine if an item is a member of the set.


what does the data structure want ? list array? some example provide ? and it said that the class should externally support the following functions:

is that mean the function is not member function of class ? is external & outside 1?
what does the data structure want ? list array? some example provide ?
That is what you've been asked to do. We can't decide that for you.
The functions shall be members of the class. If you got an assignment about sets and moreover about templates you shall already know what is the array. So it is very strange that you ask such a question.
the use of the term "externally" is a bit sloppy in this context. it means public member functions (which could be called form outside (externally) the class) while protected and private member functions can only be used inside (internally) the class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
]#include <iostream>
#include <string>
#include <conio.h>

using namespace std;

template< class T >
class Item{
private:
	T object[100];
	string ItemName;
public:
	Item(){ ItemName = "" };
	~Item();

	static int count;

	void addNewItem();
	void RemoveItem();
	void numOfItem();
	void DetermineItem();

};

template< class T >
int Item<T> :: count = 0;

int main(){

	system( "pause" );//Pause window
	return 0;//Exit program
}
\

Can someone for me that
 Return a pointer to a dynamically created array containing each item in the set. The caller of this function is responsible for de-allocating the memory.
what does this mean?
Last edited on
what does this mean?
create a new array of the required size (count) and copy all items (from object) to that array and return the array. That's all.

Besides: Don't name your class Item. name it set or Set (as in the requirement proposed)

what is ItemName good for? It's not within your requirement?
if it's okay to use a statically sized array stick to that.
count is neither static nor public (it determines how many items object contains). A better name for object would be Item

The function numOfItem() should return int, count actually

It's a good idea to start implementing DetermineItem() (a better name would be FindItem() though).
The parameter is an item and it returns an int: int DetermineItem(T item); or better int DetermineItem(const T &item);
iterate over what you named object, compare each item (with ==) return -1 if that item cannot be found and the index of the item otherwise.
iterate over what you named object, compare each item (with ==) return -1 if that item cannot be found and the index of the item otherwise.
i not really get how to compare with a looping

and create a newarray with size(count) . it's in a new member function?
1
2
3
4
5
6
7
8
9
cout << "\tSet of items\n--------------------------" << endl
		<< "1.Add New Item " << endl
		<< "2.Remove an Item " << endl
		<< "3.Number of items in the set " << endl
		<< "4.Find Item " << endl
		<< "5.De-allocating memory " << endl << endl
		<< "Enter Choice : ";

	int choice = 0;


and with ItemName u said it's not needed?
for example . if we add an Record . we should enter a name for the itemName right? so how to add the record if without the ItemName?

and here is the code so far been teach by you
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
template< class T >
class Set{
private:
	T Item[100];
	int count;
public:
	Set(){ count = 0; };
	~Set();

	void addNewItem();
	void RemoveItem();
	int numOfItem();
	int FindItem(const T &item);

};

template< class T >
int Set<T> :: count = 0;
template< class T >
int Set<T> :: numofItem(){
	return count;
}
template< class T >
int Set<T> :: FindItem( const T &item ){
	
	

	return -1;
}
I don't know why you're so scared when it comes to classes. The algorithms stay the same.

i not really get how to compare with a looping
I'm pretty sure you did that multiple times...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
template< class T >
int Set<T> :: FindItem( const T &item ){
	int result = -1;

	for(int i = 0; i < count; ++i)
	{
		if(item == Item[i])
		{
			result = i;
			break;
		}
	}
	return result;
}
there's really no secret behind that.

for addNewItem(); and RemoveItem();: both require the parameter item

addNewItem(); adds the item only when FindItem() returns -1
RemoveItem(); removes the item only when FindItem() returns not -1

remove line 17/18

"De-allocating memory" isn't the right wording. It should "get all items"

so how to add the record if without the ItemName?
The item itself could be a name or a number or whatever. It's just required that it's comparable. Plus in the requirements there is no name mentioned
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <iostream>
#include <string>
#include <conio.h>

using namespace std;

template< class T >
class Set{
private:
	//Cannot be negative number , validation
	static const size_t N = 100; 
	T Item[N];
	size_t count;
public:
	Set() : count(0){};
	~Set();

	void addNewItem(const T &);
	void RemoveItem(const T &);
	size_t size();
	size_t max_size();
	int FindItem(const T &);

};

template< class T >
void Set<T> :: addNewItem( const T &item){
	int result = FindItem(item);

	//Add new item when FindItem return a value -1
	if( result == -1 ){

	}
	else
		cout << endl;//Nothing Happen
}

template< class T >
void Set<T> :: RemoveItem( const T &item ){
	int result = FindItem(item);

	if( result >= 0 && result < count ){

	}
}
	
template< class T >
size_t Set<T> :: size() const{
	return count;
}

template< class T >
size_t Set<T> :: max_size() const{
	return N;
}

template< class T >
int Set<T> :: FindItem( const T &item ){
	int result = -1;
	for( int i = 0 ; i < count ; i++ ){
		if( item == Item[i] ){
			result = i;
			break;
		}
	}
	return result;
}

int main(){
	int choice = 0;
	string title;
	system( "pause" );//Pause window

	cout << "\tSet of items\n--------------------------" << endl
		<< "1.Add New Item " << endl
		<< "2.Remove an Item " << endl
		<< "3.Number of items in the set " << endl
		<< "4.Find Item " << endl
		<< "5.Get all Items " << endl << endl
		<< "Enter Choice : ";

	cin  >> choice;

	switch( choice ){
	case 1:{
			cout << "Enter title : ";
			getline( cin , title );
			Set<string> set;
			set.addNewItem(title);
			break;
		   }
	default:
		cout << "Invalid Input ! Please re-enter ! "<< endl;
		break;
	}
	system( "pause" );
	return 0;//Exit program
}


here the process.
as @vlad told me. he said i should declare
1
2
size_t size();
	size_t max_size();


this is for which function already ? and sorry for it.
when add the item. is the Item[[100] will be increase or countwill be increase ? by the way . the name that i pass in for the title is an object //class ?] it's not a normal variable right?
You can use size_t. no problem with that. it's basically unsigned int. I'd prefer int because you can use negative values.

this is for which function already ? and sorry for it.
What do you mean?

when add the item. is the Item[[100] will be increase or count will be increase ?
count of course. it's easy, really. Didn't i show it already:
1
2
3
4
5
6
7
8
template< class T >
void Set<T> :: addNewItem( const T &item){
...
//Important! check if count is not already N
Item[count] = item;
++count;
...
}
Remove is the opposite:
1
2
3
4
5
6
7
8
template< class T >
void Set<T> :: RemoveItem( const T &item ){
...
//Important! check if count is not already 0
--count;
Item[result] = Item[count];
...
}


the name that i pass in for the title is an object //class ?] it's not a normal variable right?
A variable is a variable. Not normal in what regard?

line 88: having the Set variable there makes no sense. Remember your course project? Set is similar. string title; would rather find it's place in that case.

It's think that making a function for each case would be a good idea.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include <iostream>
#include <string>
#include <vector>
#include <iterator>
#include <conio.h>

using namespace std;

template< class T >
class Set{
private:
	//Cannot be negative number , validation
	static const size_t N = 100; 
	T Item[N];
	size_t count;
public:
	Set() : count(0){};
	~Set();

	void addNewItem(const T &);
	void RemoveItem(const T &);
	size_t size();
	size_t max_size();
	int FindItem(const T &);

	bool operator==(const Set &set ){ return count == set.count; }
	bool operator!=(const Set &set ){ return count != set.count; }

};

template< class T >
void Set<T> :: addNewItem( const T &addData){
	// check for duplicate data
	bool duplicate = false;
	for( int i = 0 ; i < N ; i++ ){
		// check if new data is similar to the data in the stack
		if( Item[i] == addData )
			duplicate = true;
	}
		
	// if there are no duplication
	if( duplicate == false ){
		if( count < N )
			stack[count++] = addData;
		cout << "Item successfull added ! " << endl;
	}
	else
		cout<<"Entered item already exist in the stack"<<endl;
}

template< class T >
void Set<T> :: RemoveItem( const T &deleteData ){
	// declare & initialize variable
	bool foundDelete = false;
	int index;

	// search in the item if the data to be deleted exist
	for( int i = 0 ; i < N ; i++ ){
		// check if it exist(found)
		if( Item[i] == deleteData ){
			foundDelete = true;
			index = i;
		}
	}

	// if it exists(found)
	if( foundDelete == true ){
		// shift the item to replace & overwrite the delete data
		for( int j = index; j < N-1 ; j++ ){
			Item[j]=Item[j+1];
		}
		count--;	// reduce the number items
		cout << "Item successfull deleted ! " << endl;
	}
	else
		cout << "Number stack not found ! " << endl;
}
	
template< class T >
size_t Set<T> :: size() const{
	return count;
}

template< class T >
size_t Set<T> :: max_size() const{
	return N;
}

int main(){
	int choice = 0;
	string title;
	system( "pause" );//Pause window

	cout << "\tSet of items\n--------------------------" << endl
		<< "1.Add New Item " << endl
		<< "2.Remove an Item " << endl
		<< "3.Number of items in the set " << endl
		<< "4.Find Item " << endl
		<< "5.Get all Items " << endl << endl
		<< "Enter Choice : ";

	cin  >> choice;

	switch( choice ){
	case 1:{
			cout << "\n\tAdd Item\n---------------------- " << endl;
			cout << "Enter title : ";
			getline( cin , title );
			Set<string> set;
			set.addNewItem(title);
			getch();//Pause window
			break;
		   }
	case 2:{
			// DELETE 
			//prompt of user input
			cout<<"\n\tDelete Stack\n-------------------------"<<endl;
			cout<<"Enter the data to delete: ";
			getline( cin , title );
			Set<string> set;
			set.RemoveItem(title);
			getch();
			break;
		   }
	case 3:{
			Set<string>set;
			vector<string>::iterator it;
			// // use the iterator to display the number item
			// DISPLAY STACK
			cout<<"\nDisplay number of item in the set"<<endl;
				// use the iterator to display the item
			while(it != is.end())
				cout<<it++<<" ";
			cout << endl;
			getch();//Pause window
			break;
		   }
		break;
	case 4:
		break;
	case 5:
		break;
	default:
		cout << "Invalid Input ! Please re-enter ! "<< endl;
		break;
	}
	system( "pause" );
	return 0;//Exit program
}


sorry and i late to post what I'm doing so far.
Here , i just did it with own concept using boolean.
for the case 3 . can i just declare
int countItem;
when add item successfull the
countItem++;
like that correct? as my question stated that Return the number of items in the set.

beside that. i need to overload the == and != operator for the object class so my template based set class can determine membership
Last edited on
Check my reply above: http://www.cplusplus.com/forum/beginner/84902/#msg457818

don't create a new Set for each case. It must remain valid for the entire program. while title could be local for each case

I don't understand why you dissolved FindItem(). But it's up to you of course. Do what you find more suitable/understandable for you.

for the case 3 . can i just declare
There's no connection from set to vector. Except you would return a vector from a member function of Set

i need to overload the == and != operator for the object class so my template based set class can determine membership
Nope, string has this operators. You just need to overload them if there're missing
string has the operator .
but my question said that
Test your class by creating different sets of different data types (e.g. strings, integers, or objects of other classes). If you add objects to your set, then you may need to overload the == and != operators for the object’s class so your template based set class can properly determine membership.
that's why i need to have the overloading operator ?
do i do correct for my overloading operator ?

so how should i declare the number of item of the set?
because at start i already set the fix sized for N as 100;
if i get the N . the result of N will always 100.
Last edited on
You need to overload those operators for each Item type (i.e. class/struct) you want to store in your set and which haven't already.

With the operators above you can now store instances of set into your set.


so how should i declare the number of item of the set?
because at start i already set the fix sized for N as 100;
if i get the N . the result of N will always 100.
count is the number of items. N is only the upper limit
1
2
bool operator==(const Set &set ){ return Item == set.Item; }
	bool operator!=(const Set &set ){ return Item != set.Item; }


isn't for each Item Type ?

and for the
Determine if an item is a member of the set.
it's just like the function that last time u teached that name
FindItem()?
isn't for each Item Type ?
No, Item == set.Item compares only the pointers of the array. (a typical pitfall for beginners)

so your template based set class can properly determine membership
In this case iit means that the compiler would complain if the operators are missing.

Talking about compiler issues. I'd suggest that you start the compiler. There're several issues...
err.
Question 1 :
what you mean for that? no any code needed ?

Question 2 :
Determine if an item is a member of the set. is mean to
FindItem() and display it only right?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
template< class T >
void Set<T> :: FindItem( const T &findItem ){
	// declare & initialize variable
	bool foundItem = false;
	int index;

	// search in the item if the data to be deleted exist
	for( int i = 0 ; i < N ; i++ ){
		// check if it exist(found)
		if( Item[i] == findItem ){
			foundItem = true;
			index = i;
		}
	}

	if( foundItem == true ){
		cout << findItem << " is a member of the set ! " << endl << endl;
	}
	else
		cout << findItem << " is not a member of the set ! " << endl << endl;
Last edited on
comparing arrays like so Item == set.Item is wrong in [almost] any case. You need a loop.

Question 2 :
Determine if an item is a member of the set. is mean to
FindItem() and display it only right?
Yes.
Last edited on
1
2
3
4
5
bool operator ==( const Set &set ){
		return ( for ( int i = 0 ; i < N ; i++ ){
			Item[i] == set.Item[i];
		}
	}


isn't correct?
always use count for your loops, really! Not N.

N is only the upper limit you have to check against when you add an item. Again: It is not use in any of your loops!

isn't correct?
No, it makes no sense. Use a variable like ok (or so). Set this variable to the result of your comparison (break the loop if not ok). return ok.
Pages: 123