Question requirement

Pages: 123
okay. i changed all of it .
sorry . this is because for my concept i thought loop until the end of the limit only.
1
2
3
4
5
6
7
8
9
10
11
bool operator ==( const Set &set ){
		bool match;
		for( int i = 0 ; i < count ; i++ ){
			if( Item[i] == set.Item[i] )
				match = true;
			else
				match = false;
				break;
		}
		return match;
	}

isn't correct?

and for your last time explaing , do u said 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. is to get all items only?
but i ask my friend he told me have to create a pointer that point to the dynamic array of set element? mind provide some example for me??
isn't correct?
not quite:
1
2
3
4
5
6
7
8
9
10
11
12
bool operator ==( const Set &set ){
		bool match;
		for( int i = 0 ; i < count ; i++ ){
			if( Item[i] == set.Item[i] )
				match = true;
			else { // Note
				match = false;
				break;
			} // Note: this braces ties the break to else
		}
		return match;
	}
One more thing: count and set.count must be equal in order to start the loop.

provide some example for me??
This:
1
2
3
4
5
6
7
T *GetAllItems() const{
		T *result = new T[count];
		for( int i = 0 ; i < count ; i++ ){
			result[i] = Item[i];
		}
		return result;
	}
Last edited on
bool operator!=(const Set &set ){ return Item != set.Item; }
correct implement ? or need to be same to the operator overloading for ==?

and the T *getAllitems() after i compile.
it's don't have any display result for my question requirement ?
because when i display , it out the things of pointer address .

and question said that . The caller of this function is responsible for de-allocating the memory.
isnt after call the function have to add a
delete getallItems();?
correct implement ? or need to be same to the operator overloading for ==?
You need to understand that this way you compare the pointers to the arrays. So yes, it the same like == just that the result is negated (!)

because when i display , it out the things of pointer address .
As like comparing you get the pointer to the array. And like comparing you need a loop to display.

isnt after call the function have to add a
delete getallItems();?
No, another call to getallItems() will lead to another array.
You need to store the result in a variable (the second reason beside the loop for display) and delete[] the result of getallItems() each time you called that function
1
2
3
4
5
6
7
8
9
10
11
template< class T >
T* Set<T> :: GetAllItems() const {
	T *result = new T[count];
	for( int i = 0 ; i < count ; i++ ){
		result[i] = Item[i];
	}
	for( int i = 0 ; i < count ; i++ ){
		cout << result[i] << endl;
	}
	return result;
}


so do you mean in main i should declare a variable to show the result?
when i declare in my main with
string getAll = set.getAllItems)_
it give me error instead. but that is string what?
why i cannot return with a string value?

and why the address will still show out?
and the delete also should declare inside the main not the function? because when i put the delete result; it should be return NULL value already .
and when i insert delete result[] it give me an error instead

and why i cant test my set with class object d?
1
2
class firstclass{};
class secondclass{};


in main
1
2
3
4
5
firstclass firstobject;
secondclass secondobject;

set.addNewItem(firstobject);
set.addNewItem(secondobject);

it fail
Last edited on
You've hard time to grasp that dynamic array thing, now don't you?

it give me error instead. but that is string what?
Note that a pointer to the string is returned (T*). It points to the first element of the array which has exactly count element.

This is how you access it
1
2
3
string *getAll = set.getAllItems() // Note the *
...
delete[] getAll; // Note the position of [] 

why i cannot return with a string value?
Because it's an array with possibly more than one [string].

and why the address will still show out?
It's an array! you have to use a loop to show the elements of that array

and why i cant test my set with class object d?
Within firstclass, secondclass, or whatever class you have to implement the operators:
1
2
bool operator==(...)
bool operator!=(...)

(Since you're using only the == operation whithin Set<T> you just have to override the operator==)

This is meant when stating:
then you may need to overload the == and != operators for the object’s class so your template based set class can properly determine membership.
1
2
3
4
5
6
bool operator ==( const Set &rhs ){
		return ( count == rhs.count && equal( Item, Item + count, rhs.Item ) );
	}
	bool operator!=(const Set &set ){ 
		return ( count != rhs.count && equal( Item, Item + count, rhs.Item ) );
	}


so how should pass the class object to my set?
mind to show since it's just a short code?
Set<string> set;
because you told me all using the same set
then class is not a string, and it cannot do likeSet<class> set;
Your operator!= is wrong. Here's a little trick:
1
2
3
	bool operator!=(const Set &set ){ 
		return !operator==(rhs ); // Note the ! in front of operator
	}


so how should pass the class object to my set?
1
2
3
4
5
6
7
8
9
class firstclass
{
  bool operator==(const firstclass &) const { }
};
...
Set<firstclass> set;
firstclass firstobject;

set.addNewItem(firstobject);
i get all error
1
2
(144) : see declaration of 'firstclass::operator ==
 see declaration of 'firstclass' 


here is the declaration
1
2
3
4
5
6
7
class firstclass{
	bool operator==(const firstclass &) const { }
};

Set<firstclass> theset;
				firstclass firstobject;
theset.addNewItem(firstobject);
ok, make the operator== public
same
error C4716: 'firstclass::operator==' : must return a value
well then:
1
2
3
4
5
class firstclass
{
public:
  bool operator==(const firstclass &) const { return true; }
};
for testing
can be compile. but when i try to getAllItems.
the firstobject doesn't show out .
and the number of item still remain 0
show your actual code. It can't be too much
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#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){};

	void addNewItem(const T &);
	void RemoveItem(const T &);
	void FindItem(const T &);
	T *GetAllItems() const;
	size_t size() const;
	size_t max_size() const;
	
	bool operator ==( const Set &rhs ){
		return ( count == rhs.count && equal( Item, Item + count, rhs.Item ) );
	}
	bool operator!=(const Set &rhs ){ 
		 return ( !( *this == rhs ) );
	}
};

template< class T >
void Set<T> :: addNewItem( const T &addData){
	// check for duplicate data
	bool duplicate = false;
	for( int i = 0 ; i < count ; 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 )
			Item[count++] = addData;
		cout << "Item successfull added ! " << endl << endl;
	}
	else
		cout << "Entered item already exist in the set" << endl << 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 < count ; i++ ){
		// check if it exist(found)
		if( Item[i] == deleteData ){
			foundDelete = true;
			index = i;
		}
	}

	// if it exists(found)
	if( foundDelete == true ){
		cout << "\nItem Found in Set " << endl
			<< "1.Delete Item " << endl
			<< "2.Cancel " << endl << endl
			<< "Enter Choice : ";
		int choice = 0;
		cin  >> choice ;
		do{
			switch( choice ){
			case 1:
				// 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 << endl;
				break;
			case 2:
				cout << "Process been cancel by user ! " << endl << endl;
				break;
			default:
				cout << "Invalid Input ! Please re-enter ! " << endl << endl;
			}
		}while( choice != 2 );
	}
	else
		cout << "Item not found ! " << endl << endl;
}

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 < count ; 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;
}

template< class T >
T* Set<T> :: GetAllItems() const {
	T *result = new T[count];
	for( int i = 0 ; i < count ; i++ ){
		result[i] = Item[i];
	}
	for( int i = 0 ; i < count ; i++ ){
		cout << result[i] << endl;
	}
	return result;
}
	
template< class T >
size_t Set<T> :: size()const{
	return count;
}

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

class firstclass
{
public:
  bool operator==(const firstclass &) const { return true; }
};
class secondclass{
public:
  bool operator==(const secondclass &) const { return true; }
};

int main(){
	int choice = 0;
	string title;
	Set<string>set;
	
	do{
		cout << "\tSet of items\n--------------------------" << endl
			 << "0.Exit Program "				<< 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 0:
			cout << "Program Been Terminated by User ! " << endl;
			getch();
			break;
		case 1:{
				int choice1 = 0;
				cout << "\n\tAdd Record\n---------------------" << endl
					<< "1.Add Item with String Type " << endl
					<< "2.Add Item with Object Type " << endl
					<< "3.Add Item with integer Type " << endl << endl
					<< "Enter Choice : ";
				cin  >> choice1;
				switch( choice1 ){
				case 1:{
						cin.ignore();
						string title = "";
						cout << "\n\tAdd Item [String Type]\n------------------------------- " << endl;
						cout << "Enter title : ";
						getline( cin , title );
						set.addNewItem(title);
						cout << "String Type Item Added Successfull " << endl;
						getch();
						break;
					   }
				case 2:{ 
						int countObject = 0;
						char Y = ' ';
						do{
							cout << "Do you want to create data type of object ? [Y/N] : ";
							cin  >> Y;
							if( Y == 'Y' || Y == 'y' ){
								countObject++;
								if( countObject == 1){
									Set<firstclass> theset;
									firstclass firstobject;
									theset.addNewItem(firstobject);
									cout << "Object type added successfull " << endl;
								}
								else if( countObject == 2 ){
									Set<secondclass> theset1;
									secondclass secondobject;
									theset1.addNewItem(secondobject);
									cout << "Object type added successfull " << endl;
								}
								else{
									cout << "User only defined two classes ! Cannot add class type anymore ! " << endl << endl;
								}
							}
							else if( Y =='N' || Y == 'n' )
								cout << "Task been cancelled by user ! " << endl << endl;
						}while( Y =='Y' || Y == 'y' );
						break;
					   }
				case 3:{
						Set<int> Setint;
						int integertype = 0;
						cout << "\n\tAdd Item [Integer Type]\n--------------------------" << endl
							<< "Enter number : ";
						cin  >> integertype;
						while( cin.fail() ){
							cin.clear();
							cin.ignore(100,'\n');
							cout << "Invalid input ! Only integer type allow ! " << endl << endl;
							cout << "Enter number : ";
							cin  >> integertype;
						}
						cout << "Integer Type Item Added Successfull " << endl;
						Setint.addNewItem(integertype);
						getch();//Pause window
						break;
					   }
				}
				getch();//Pause window
				break;
			   }
		case 2:{
				// DELETE 
				//prompt of user input
				cin.ignore();
				string title = "";
				cout << "\n\tDelete Item\n-------------------------" << endl;
				cout << "Enter the data to delete : ";
				getline( cin , title );
				set.RemoveItem(title);
				getch();
				break;
			   }
		case 3:{
				cout << "Number of item in the set : " << set.size() << endl << endl;
				getch();//Pause window
				break;
			   }
			break;
		case 4:{
				cin.ignore();
				string title = "";
				cout<<"\n\tFind Item\n-----------------------"<<endl;
				cout << "Enter Item to find : ";
				getline( cin , title );
				cout << endl;
				set.FindItem( title );
				getch();
				break;
			   }
		case 5:{
				string *getAll = set.GetAllItems();
				getch();
				delete []getAll;
				break;
			   }
		default:
			cout << "Invalid Input ! Please re-enter ! "<< endl;
			break;
		}
	}while( choice != 0 );
	system( "pause" );
	return 0;//Exit program
}



i changed my code , it's work and can add now.
but when i add with object or int type . my getAllItems() and number of set did'nt increase . how come?
for me i will think that because im using different set . should i do + ? mind help?
Last edited on
for me i will think that because im using different set .
Yes it's a different set and it's local to case 1:. you can't access theset or theset1 outside case 1:.

Furthermore you cannot use firstclass or secondclass with cout. The compiler would give you an error if you'd use the according set with GetAllItems()

[I'd suggest using a function for each case to keep the overview.]

But, isn't the whole requirement fullfilled already?


[Btw: I don't mind to help you with singly/doubly linked lists too...]
i think so . it's all done already . i think left the getAllItems() only.
so mind to help me that u said use a each case? provide some example so i can have a direction to do? hmm.
what i mean is this:
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
template< class T >
void AddNewItem(Set<T> &set)
{
				int choice1 = 0;
				cout << "\n\tAdd Record\n---------------------" << endl
					<< "1.Add Item with String Type " << endl
					<< "2.Add Item with Object Type " << endl
					<< "3.Add Item with integer Type " << endl << endl
					<< "Enter Choice : ";
				cin  >> choice1;
				switch( choice1 ){
				case 1:{
...
}

...

int main(){
	int choice = 0;
	string title;
	Set<string>set;
	
	do{
		cout << "\tSet of items\n--------------------------" << endl
			 << "0.Exit Program "				<< 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 0:
			cout << "Program Been Terminated by User ! " << endl;
			getch();
			break;
		case 1:{
				AddNewItem(set);
				break;
			   }
...
If'd do this for each case with more then let's say 3 lines of code you'd have a lot of small function (which might even be reusable) instead of one monster function.

how you place the { does it make hard to see when a block starts and ends.

i think left the getAllItems() only.
is there an issue left?
nop . only facing the problem for getClass Items
So do you need to do anything. Any questions left?
Pages: 123