Sorry for reposting my program because I couldn't modify my last post
My program compiles but doesn't run,
It would be great if you could help
Here is the screen shot of debugging
//DynBag.cpp : Implementation File
#include <stdexcept>
#include <iostream>
#include "DynBag.h"
main :
Code:
int main ()
{
Bag b(2), b1(3), b2(2), b3(4);
b.insert("Lidiecita");
b.insert("Brendita");
b.insert("Francisquito");
b.insert("Lidiecita");
b1.insert("Mayi");
b1.insert("Eimy");
b += b;
b.print(cout);
b3 = b + b1;
b3.print(cout);
b2 = b1;
b2.print(cout);
Bag b4 = b3;
cout << b4;
}
DynBag.h
Code:
#ifndef DYNBAG_H
#define DYNBAG_H
#include <string>
using namespace std;
typedef string ItemType;
class Bag
{
public:
Bag (int InitialCapacity);
Bag(const Bag& other);
~Bag();
void insert(const ItemType& entry);
void erase_one(const ItemType& target) throw (domain_error);
int erase(const ItemType& target);
Bag& operator=(const Bag& other);
Bag& operator+=(const Bag& addend);
void reserve(int new_capacity);
int size() const;
int count_duplicates (const ItemType& target) const;
void print (ostream& out) const;
private:
ItemType *contents;
int NumItems;
int capacity;
};
Bag operator+ (const Bag& b1, const Bag& b2);
ostream& operator<<(ostream&, const Bag& b1);
#endif
DynBag.cpp
Code:
#include <stdexcept>
#include <iostream>
#include "DynBag.h"
Bag::Bag(int InitialCapacity) : NumItems(0)
{
capacity = InitialCapacity;
contents = new ItemType[capacity];
}
Bag::Bag(const Bag& other) : NumItems(other.NumItems)
{
capacity = other.capacity;
contents = new ItemType[capacity];
for (int i=0; i < NumItems; ++i)
contents[i] = other.contents[i];
}
Bag::~Bag() {}
int Bag::size () const
{
return NumItems;
}
int Bag::count_duplicates (const ItemType& target) const
{
int answer = 0;
for (int i = 0; i < NumItems; ++i)
if (target == contents[i])
++answer;
return answer;
}
void Bag::insert(const ItemType & entry)
{
if (NumItems >= capacity)
reserve(capacity*2);
contents[NumItems] = entry;
++NumItems;
}
void Bag::erase_one(const ItemType & target) throw(domain_error)
{
int i, j;
for (i = 0; i < NumItems; ++i)
if (target == contents[i]) break;
if (i == NumItems) throw domain_error("Target Not Found");
if (i < NumItems-1)
for (j = i; j < NumItems-1; ++j)
contents[j] = contents[j+1];
--NumItems;
}
int Bag::erase(const ItemType & target)
{
int count = count_duplicates(target);
int removed = 0;
for ( ; removed < count; ++removed)
erase_one(target);
return(removed);
}
Bag& Bag::operator+=(const Bag& addend)
{
int i;
for(i=0; i<addend.size(); i++)
{
insert(addend.contents[i]);
}
return *this;
}
Bag& Bag::operator =(const Bag& other)
{
if (&other == this) return *this;
delete[] contents;
NumItems = other.NumItems;
capacity = other.capacity;
contents = new ItemType[capacity];
for (int i=0; i < NumItems; ++i)
contents[i] = other.contents[i];
return *this;
}
void Bag::reserve (int new_capacity)
{
if (new_capacity == capacity) return;
if (new_capacity < NumItems) new_capacity = NumItems;
ItemType *new_contents = new ItemType[new_capacity];
for (int i=0; i < capacity; ++i)
new_contents[i] = contents[i];
delete[] contents;
contents = new_contents;
capacity = new_capacity;
}
void Bag::print(ostream& out) const
{
for (int i = 0; i < NumItems; i++)
out << contents[i] <<"\t";
out <<endl;
}
Bag operator+(const Bag &b1, const Bag &b2)
{
Bag b3(b1.size()+b2.size());
b3 += b1;
b3 += b2;
return b3;
}
ostream& operator<<(ostream& o, const Bag& b1)
{
o << "[b1 contents here]";
return o;
}