Question

#ifndef BAG_H #define BAG_H #include <cstdlib> // Provides size_t using namespace std; class bag { public:...

#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).

Homework Answers

Answer #1

Solution:

  • Copy constructor: Copy constructor is called when a new object is created using an existing object. Consider it a copy of existing object.
  • Copy Assignment: Copy assignment is used when an already initialised object is assigned a new value using an existing object.

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.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
(C++) Suppose you want to use a linked list where the items stored in the list...
(C++) Suppose you want to use a linked list where the items stored in the list are strings from the standard library string class, how would you change the node1.h header file? Header File: #ifndef MAIN_SAVITCH_NODE1_H #define MAIN_SAVITCH_NODE1_H #include <cstdlib> // Provides size_t and NULL namespace main_savitch_5 { class node { public: // TYPEDEF typedef double value_type; // CONSTRUCTOR node(const value_type& init_data=value_type( ), node* init_link=NULL) { data_field = init_data; link_field = init_link; } // MODIFICATION MEMBER FUNCTIONS node* link( )...
Write the implementation of a non-member function Node* deleteSecond(Node* head_ptr), where Node is a class defined...
Write the implementation of a non-member function Node* deleteSecond(Node* head_ptr), where Node is a class defined on page 257. The function takes as input a pointer to the head of a linked list consisting of numbers. The function should remove the second item in the list. If the list had only one item, the function should delete that item. If the list was empty, then let the list remain empty. In all cases return the new head of the list...
Requirements The assignment is to create a dynamic array implementation of a set (defined in set.h)....
Requirements The assignment is to create a dynamic array implementation of a set (defined in set.h). Add the efficiency of each function to the documentation in the herder file. Use the test_set.cpp as your test program. _______________________________________________________________________________________________________________________________________________________ set.h file #ifndef _SET_H_ #define _SET_H_ #include <cstdlib> #include <iostream> class set { public: typedef int value_type; typedef std::size_t size_type; set(size_type initial_capacity); // postcondition: empty set with initial_capacity has been created ~set(); // postcondition: all dynamically allocated memory has been deallocated set(const set&...
C++ Class involving difference. The goal is to overload the function: void Bag::operator-=(const Bag& a_bag) //...
C++ Class involving difference. The goal is to overload the function: void Bag::operator-=(const Bag& a_bag) // The Set Difference between two sets A and B is the set that consists of the elements of A which are not elements of B. Bag bag1 = (1,2,3) and Bag bag2 = (2,4,5) then bag1-=bag2 should return 1,3,4,5. //parameter a_bag to be subtracted from this (the calling) bag //post removes all data from items_ that is also found in a_bag //Since type is...
C++ #include<iostream> #include<string> #include<fstream> #include<cstdlib> using namespace std; const int ROWS = 8; //for rows in...
C++ #include<iostream> #include<string> #include<fstream> #include<cstdlib> using namespace std; const int ROWS = 8; //for rows in airplane const int COLS = 4; void menu(); //displays options void displaySeats(char[][COLS]); void reserveSeat(char [ROWS][COLS]); int main() { int number=0; //holder variable char seatChar[ROWS][COLS]; for (int i = 0; i < ROWS; i++) { for (int j = 0; j < COLS; j++) { seatChar[i][j] = '-'; } } int choice; //input from menu bool repeat = true; //needed for switch loop while (repeat...
- implement the Stack ADT using the linked list approach. Use C++ program language #include "StackLinked.h"...
- implement the Stack ADT using the linked list approach. Use C++ program language #include "StackLinked.h" template StackLinked::StackLinked (int maxNumber) { } template StackLinked::StackLinked(const StackLinked& other) { } template StackLinked& StackLinked::operator=(const StackLinked& other) { } template StackLinked::~StackLinked() {    clear(); } template void StackLinked::push(const DataType& newDataItem) throw (logic_error) {    } template DataType StackLinked::pop() throw (logic_error) { } template void StackLinked::clear() {    StackNode* t;    while ( top != NULL)    {        t = top;       ...
Using C++, write the following program: The Point.h file declares the class and you will create...
Using C++, write the following program: The Point.h file declares the class and you will create a Point.cpp that contains the implementation and a main() that instantiates the Point class to add additional tests to cover the various overloaded operators Use the Point.h file that is found below (This class contains a point on a plane and this class is going to contain a X coordinate and Y coordinate. The class is also going to contain a member function that...
array_v.h #ifndef ARRAY_V_H #define ARRAY_V_H #include <cassert> template < typename IndexType, typename BaseData > class Array_V...
array_v.h #ifndef ARRAY_V_H #define ARRAY_V_H #include <cassert> template < typename IndexType, typename BaseData > class Array_V { public: IndexType partition(IndexType lo, IndexType hi); IndexType sort(int numvals); void qsRecursive(IndexType lo, IndexType hi); IndexType getHiIndex(); IndexType getLoIndex(); void setHiIndex(IndexType index); void setLoIndex(IndexType index); Array_V(IndexType lo, IndexType hi); //constructor Array_V(int size = 0); Array_V(const Array_V< IndexType, BaseData >& initArray); //copy constructor ~Array_V(); //destructor BaseData& operator [] (IndexType); Array_V< IndexType, BaseData >& operator = (const Array_V< IndexType, BaseData >& initArray); void assign(IndexType i, const...
Data Structures using C++ Consider the following class #ifndef LINKEDQUEUETYPE_H #define LINKEDQUEUETYPE_H #include <iostream> #include <new>...
Data Structures using C++ Consider the following class #ifndef LINKEDQUEUETYPE_H #define LINKEDQUEUETYPE_H #include <iostream> #include <new>    #include <cstdlib> #include "QueueADT.h" using namespace std; // Definition of the node template <class ItemType> struct NodeType {        ItemType info;        NodeType<ItemType> *next; }; template <class ItemType> class LinkedQueueType: public QueueADT<ItemType> { public:        // Constructor        LinkedQueueType();           // Default constructor.           // Post: An empty queue has been created. queueFront = NULL;           //       queueBack = NULL;...
My assignment: Triplet Template Class Directions: Define a template class for a generic triplet. The private...
My assignment: Triplet Template Class Directions: Define a template class for a generic triplet. The private data member for the triplet is a generic array with three elements. The triplet ADT has the following functions:  default constructor  explicit constructor: initialize the data member using parameters  three accessors (three get functions) which will return the value of each individual element of the array data member  one mutator (set function) which will assign values to the data member...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT