#ifndef BAG_H
#define BAG_H
#include <cstdlib> // Provides size_t
using namespace std;
class bag {
public:
// TYPEDEFS and MEMBER CONSTANTS
typedef int value_type;
typedef std::size_t size_type;
static const size_type CAPACITY = 30;
// CONSTRUCTOR bag() {used = 0;}
// MODIFICATION MEMBER FUNCTIONS size_type erase(const value_type& target);
bool erase_one(const value_type& target);
void insert(const value_type& entry);
void operator +=(const bag& addend);
void sort(const bag& b);
//Sort the array in the bag object
// CONSTANT MEMBER FUNCTIONS
size_type size( ) const { return used; }
size_type count(const value_type& target) const;
private:
value_type data[CAPACITY];
// The array to store items size_type used;
// How much of array is used
};
#endif
For the bag class above, create only the copy assignment and copy constructor methods (you can assume that you don’t need to use the :: scope resolution operators).
Solution:
Copy Constructor for BAG Class:
bag(const bag &b){
used = b.used;
for(int i=0; i<(b.used); i++){
data[i] = b.data[i];
}
}
Copy Assignment for BAG Class:
bag& operator = (const bag &b){
// check for self-assignment
if(&b == this)
return *this;
used = b.used;
for(int i=0; i<(b.used); i++){
data[i] = b.data[i];
}
return *this;
}
PS: Let me know if you have any doubt.
Get Answers For Free
Most questions answered within 1 hours.