Need help with creating a function in a class

Hello, I am a computer science student. I am currently working on a "bag" class which is sort of a common sense answer to creating a random class with difference functions. I am attempting to create a "union" function which takes two bags ie: bag1 and bag2, adds all the items in both bags and creates a new bag ie: "bag3". For some reason I keep coming up with problems instead of solutions. Maybe it's the fact I just got done with 2 days of calculus. I don't know. My code is below. Both a main(source) and header file. Any help would be appreciated. Thanks in advance.

Header

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
#ifndef BAG_H
#define BAG_H

const int BAG_CAPACITY = 20;

template <typename T>
class Bag {
private:
	int count; // Number of items in the Bag
	T items[BAG_CAPACITY]; // Items in the Bag

	// Find index of a given item
	int findItem (const T& anEntry) const;
public:
	// Initialize the bag
	Bag () { count = 0; }

	// Get number of items in the bag
	int getCurrentSize () const { return count; }

	// Get an item from the bag using a zero-based index
	//	If index is invalid, returns a default item of type T
	T getItem (int index) const
		{ return index >= 0 && index < count ? items[index] : T(); }

	// Determine if bag is empty
	bool isEmpty () const { return count == 0; }

	// Add an item to the bag
	bool add (const T& newEntry);

	// Determine number of times item appears in the bag
	int getFrequencyOf (const T& anEntry) const;

	// Determine if the bag contains the given item
	bool contains (const T& anEntry) const
		{ return getFrequencyOf(anEntry) > 0; }

	// Clear all items from the bag
	void clear () { count = 0; }

	// Remove an item from the bag
	bool remove (const T& anEntry);
};

template <typename T>
bool Bag<T>::add (const T& newEntry) {
	if (count == BAG_CAPACITY)
		return false;
	items[count++] = newEntry;
	return true;
}

template <typename T>
int Bag<T>::getFrequencyOf (const T& anEntry) const {
	int freq = 0;
	for (int i = 0; i < count; i++)
		if (items[i] == anEntry)
			freq++;
	return freq;
}

template <typename T>
int Bag<T>::findItem (const T& anEntry) const {
	for (int i = 0; i < count; i++)
		if (items[i] == anEntry)
			return i;
	return -1;
}

template <typename T>
bool Bag<T>::remove (const T& anEntry) {
	if (!contains(anEntry))
		return false;
	for (int i = findItem(anEntry) + 1; i < count; i++)
		items[i-1] = items[i];
	count--;
	return true;
}

template <typename T>
void Bag<T>::union (const T&

#endif 


Main(source)
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
#include <fstream>
#include <iostream>
#include <string>
using namespace std;

#include "bag.h"

void add_item (Bag<string>& bag);
void init_bag (Bag<string>& bag, const string& fname);
void help ();
void remove_item (Bag<string>& bag);
void save_bag (const Bag<string>& bag, const string& fname);
void show_bag_detail (const Bag<string>& bag);
void show_bag_summary (const Bag<string>& bag);

int main () {

	// Declare and initialize bag.
	Bag<string> bag1, bag2;
	init_bag(bag1, "bag1.txt");
	init_bag(bag2, "bag2.txt");

	// Command loop...
	cout << endl;
	help();
	string command;
	do {
		cout << endl;
		cout << "bag1: "; show_bag_summary(bag1);
		cout << "bag2: "; show_bag_summary(bag2);
		cout << endl;
		cout << "Command: ";
		getline(cin, command);
		if (command == "add1") add_item(bag1);
		else if (command == "add2") add_item(bag2);
		else if (command == "remove1") remove_item(bag1);
		else if (command == "remove2") remove_item(bag2);
		else if (command == "detail1") show_bag_detail(bag1);
		else if (command == "detail2") show_bag_detail(bag2);
		else if (command == "diff12") ;// show_detail(bag1.difference(bag2))
		else if (command == "diff21") ;// show_detail(bag1.difference(bag2))
		else if (command == "intersect") ;// show_detail(bag1.intersection(bag2))
		else if (command == "union") ;// show_detail(bag1.union(bag2))
		else if (command != "exit")
			help();

	} while (command != "exit");

	// Save bag contents
	save_bag(bag1, "bag1.txt");
	save_bag(bag2, "bag2.txt");

//	system("pause");
	return 0;
}

void add_item (Bag<string>& bag){
	string item;
	cout << "Item to add: ";
	getline(cin, item);
	if (bag.add(item))
		cout << "Item added." << endl;
	else
		cout << "Failed to add item." << endl;
}

void help () {
	cout << "Valid commands: addN removeN detailN (N = 1 or 2)" << endl;
	cout << "                diff12 diff21 intersect union" << endl;
}

void init_bag (Bag<string>& bag, const string& fname) {

	// Open file of items. Exit if file not found.
	ifstream f(fname.c_str());
	if (!f) return;

	// Get all items from file and add to bag.
	string item;
	while (getline(f, item))
		bag.add(item);

	// Close file
	f.close();
}

void remove_item (Bag<string>& bag){
	string item;
	cout << "Item to remove: ";
	getline(cin, item);
//	if (bag.remove(item))
//		cout << "Item was removed." << endl;
//	else
//		cout << "Item was not found." << endl;
	cout << (bag.remove(item) ? "Item was removed." : "Item was not found.") << endl;
}

void save_bag (const Bag<string>& bag, const string& fname) {

	// Open file for saving bag contents.
	ofstream f(fname.c_str());
	if (!f) {
		cerr << "ERROR: Could not open mybag.txt for output." << endl;
		return;
	}

	// Save bag items
	for (int i = 0; i < bag.getCurrentSize(); i++)
		f << bag.getItem(i) << endl;

	// Close file
	f.close();
}

void show_bag_detail (const Bag<string>& bag) {
	show_bag_summary(bag);
	for (int i = 0; i < bag.getCurrentSize(); i++)
		cout << " - " << bag.getItem(i) << endl;
}

void show_bag_summary (const Bag<string>& bag) {
	if (bag.isEmpty())
		cout << "The bag is empty." << endl;
	else {
		int size = bag.getCurrentSize();
		if (size > 1)
			cout << "There are " << size << " items in the bag." << endl;
		else
			cout << "There is 1 item in the bag." << endl;
	}
}
Please provide more info on what you problem is. In any event, your union function should look something like:

1
2
3
4
5
6
7
8
9
10

template <typename T>
Bag<T> Bag<T>::union (const Bag<T>& rhs) const
{
    Bag<T> bag3;
    //... Loop over items in *this and add them to bag3
    //... Loop over items in rhs and add them to bag3
    return bag3;
}
Topic archived. No new replies allowed.