Question

In: Computer Science

C++ existing code #include "ArrayBag.hpp" #include <iostream> /****************************************************** Public Methods *****************************************************/ /* Default Constructor */ template...

C++

existing code

#include "ArrayBag.hpp"
#include <iostream>

/****************************************************** Public Methods *****************************************************/

/* Default Constructor */
template <typename ItemType>
ArrayBag<ItemType>::ArrayBag() : item_count_(0) { // initializer list

} // end default constructor

template <typename ItemType>
int ArrayBag<ItemType>::getCurrentSize() const {

return item_count_;
}

template <typename ItemType>
bool ArrayBag<ItemType>::isEmpty() const {

return item_count_ == 0;
}

template <typename ItemType>
bool ArrayBag<ItemType>::add(const ItemType &new_entry) {
  
bool has_room_to_add = (item_count_ < DEFAULT_CAPACITY);

if (has_room_to_add) {
items_[item_count_] = new_entry;
item_count_++;
} // end if

return has_room_to_add;
} // end add

template <typename ItemType>
bool ArrayBag<ItemType>::remove(const ItemType &an_entry) {

int located_index = getIndexOf(an_entry);
bool can_remove_item = !isEmpty() && (located_index > -1);

if (can_remove_item) {
item_count_--;
items_[located_index] = items_[item_count_]; // copy
// last item in place of item to be removed
} // end if

return can_remove_item;
} // end remove

template <typename ItemType>
void ArrayBag<ItemType>::clear() {

item_count_ = 0;
}

template <typename ItemType>
bool ArrayBag<ItemType>::contains(const ItemType &an_entry) const {

return getIndexOf(an_entry) > -1;
} // end contains

template <typename ItemType>
int ArrayBag<ItemType>::getFrequencyOf(const ItemType &an_entry) const {
  
int frequency = 0;
int current_index = 0; // array index currently
// being inspected
while (current_index < item_count_) {
  
if (items_[current_index] == an_entry) {
frequency++;
} // end if
current_index++; // increment to next entry
} // end while

return frequency;
} //end getFrequencyOf

template <typename ItemType>
void ArrayBag<ItemType>::operator+=(const ArrayBag<ItemType> &a_bag) {


}

template <typename ItemType>
void ArrayBag<ItemType>::operator-=(const ArrayBag<ItemType> &a_bag) {


}

template <typename ItemType>
void ArrayBag<ItemType>::operator/=(const ArrayBag<ItemType> &a_bag) {


}

template <typename ItemType>
bool ArrayBag<ItemType>::operator==(const ArrayBag<ItemType> &a_bag) {

}

template <typename ItemType>
bool ArrayBag<ItemType>::operator!=(const ArrayBag<ItemType> &a_bag) {

}

/***************************************************** Private Methods *****************************************************/

template <typename ItemType>
int ArrayBag<ItemType>::getIndexOf(const ItemType &target) const {

bool found = false;
int result = -1;
int search_index = 0;

while (!found && (search_index < item_count_)) { // if the bag is empty, item count is zero, so loop is skipped

if (items_[search_index] == target) {
found = true;
result = search_index;
}

else {

search_index++;
} // end if
} // end while

return result;
} // end getIndexOf

Define the following operator overloads in ArrayBag.hpp and implement them accordingly in ArrayBag.cpp.

/** 
   Implements Set Union.  The union of two sets A and B is the set of 
   elements, which are in A, in B, or in both A and B.
   @param a_bag to be combined with the contents of this (the calling) bag
   @post adds as many items from a_bag as space allows lhs += rhs, 
   the left hand side (the calling side) of the operator will be modified. 
**/
void operator+=(const ArrayBag &a_bag);

/** 
   Implements Set Difference
   The (set) difference between two sets A and B is the set that
   consists of the elements in A which are not elements of B.
   @param a_bag to be subtracted from this (the calling) bag
   @post removes all the data from items_ that is also found in a_bag
      lhs -= rhs, the left hand side (the calling side) of the operator 
      will be modified, remove elements from lhs that are also elements 
      of the rhs (a_bag). 
**/
void operator-=(const ArrayBag &a_bag);

/** 
   Implements Set Intersection
   The intersection of two sets A and B is the set that
   consists of the elements that are in both A and B.
   @param a_bag to be intersected with this (the calling) bag
   @post items_ no longer contains elements not found in a_bag
      lhs /= rhs, the left hand side (the calling side) of 
      the operator will be modified, remove elements from 
      lhs that are NOT elements of the rhs (a_bag).
**/
void operator/=(const ArrayBag &a_bag);

/**
   Implements Equal Comparison
   Two ArrayBags are equal to each other if they contain the same elements
   the order of the elements do not matter.
   @param a_bag to be compared with this (the calling) bag
   @return true if the two bags contain the same elements, false otherwise
   Note that in order for two bags to be equal the must be of equal sizes.
**/
bool operator==(const ArrayBag &a_bag);

/**
   Implements Not Equal Comparison Opposite of the == operator, if two bags 
      have at least one element different they are not equal
   @param a_bag to be compared with this (the calling) bag
   @return true if two bags have a differing element, false if they are equal
      In this case we can be sure that two arrays are not equal if they have 
                                                              different sizes.
**/
bool operator!=(const ArrayBag &a_bag);

Solutions

Expert Solution

#include "ArrayBag.hpp"
#include <iostream>

/****************************************************** Public Methods *****************************************************/

/* Default Constructor */
template <typename ItemType>
ArrayBag<ItemType>::ArrayBag() : item_count_(0) { // initializer list

} // end default constructor

template <typename ItemType>
int ArrayBag<ItemType>::getCurrentSize() const {

return item_count_;
}

template <typename ItemType>
bool ArrayBag<ItemType>::isEmpty() const {

return item_count_ == 0;
}

template <typename ItemType>
bool ArrayBag<ItemType>::add(const ItemType &new_entry) {
  
bool has_room_to_add = (item_count_ < DEFAULT_CAPACITY);

if (has_room_to_add) {
items_[item_count_] = new_entry;
item_count_++;
} // end if

return has_room_to_add;
} // end add

template <typename ItemType>
bool ArrayBag<ItemType>::remove(const ItemType &an_entry) {

int located_index = getIndexOf(an_entry);
bool can_remove_item = !isEmpty() && (located_index > -1);

if (can_remove_item) {
item_count_--;
items_[located_index] = items_[item_count_]; // copy
// last item in place of item to be removed
} // end if

return can_remove_item;
} // end remove

template <typename ItemType>
void ArrayBag<ItemType>::clear() {

item_count_ = 0;
}

template <typename ItemType>
bool ArrayBag<ItemType>::contains(const ItemType &an_entry) const {

return getIndexOf(an_entry) > -1;
} // end contains

template <typename ItemType>
int ArrayBag<ItemType>::getFrequencyOf(const ItemType &an_entry) const {
  
int frequency = 0;
int current_index = 0; // array index currently
// being inspected
while (current_index < item_count_) {
  
if (items_[current_index] == an_entry) {
frequency++;
} // end if
current_index++; // increment to next entry
} // end while

return frequency;
} //end getFrequencyOf

template <typename ItemType>
void ArrayBag<ItemType>::operator+=(const ArrayBag<ItemType> &a_bag) {


}

template <typename ItemType>
void ArrayBag<ItemType>::operator-=(const ArrayBag<ItemType> &a_bag) {


}

template <typename ItemType>
void ArrayBag<ItemType>::operator/=(const ArrayBag<ItemType> &a_bag) {


}

template <typename ItemType>
bool ArrayBag<ItemType>::operator==(const ArrayBag<ItemType> &a_bag) {

}

template <typename ItemType>
bool ArrayBag<ItemType>::operator!=(const ArrayBag<ItemType> &a_bag) {

}

/***************************************************** Private Methods *****************************************************/

template <typename ItemType>
int ArrayBag<ItemType>::getIndexOf(const ItemType &target) const {

bool found = false;
int result = -1;
int search_index = 0;

while (!found && (search_index < item_count_)) { // if the bag is empty, item count is zero, so loop is skipped

if (items_[search_index] == target) {
found = true;
result = search_index;
}

else {

search_index++;
} // end if
} // end while

return result;
} // end getIndexOf

Define the following operator overloads in ArrayBag.hpp and implement them accordingly in ArrayBag.cpp.

/** 
   Implements Set Union.  The union of two sets A and B is the set of 
   elements, which are in A, in B, or in both A and B.
   @param a_bag to be combined with the contents of this (the calling) bag
   @post adds as many items from a_bag as space allows lhs += rhs, 
   the left hand side (the calling side) of the operator will be modified. 
**/
void operator+=(const ArrayBag &a_bag);

/** 
   Implements Set Difference
   The (set) difference between two sets A and B is the set that
   consists of the elements in A which are not elements of B.
   @param a_bag to be subtracted from this (the calling) bag
   @post removes all the data from items_ that is also found in a_bag
      lhs -= rhs, the left hand side (the calling side) of the operator 
      will be modified, remove elements from lhs that are also elements 
      of the rhs (a_bag). 
**/
void operator-=(const ArrayBag &a_bag);

/** 
   Implements Set Intersection
   The intersection of two sets A and B is the set that
   consists of the elements that are in both A and B.
   @param a_bag to be intersected with this (the calling) bag
   @post items_ no longer contains elements not found in a_bag
      lhs /= rhs, the left hand side (the calling side) of 
      the operator will be modified, remove elements from 
      lhs that are NOT elements of the rhs (a_bag).
**/
void operator/=(const ArrayBag &a_bag);

/**
   Implements Equal Comparison
   Two ArrayBags are equal to each other if they contain the same elements
   the order of the elements do not matter.
   @param a_bag to be compared with this (the calling) bag
   @return true if the two bags contain the same elements, false otherwise
   Note that in order for two bags to be equal the must be of equal sizes.
**/
bool operator==(const ArrayBag &a_bag);

/**
   Implements Not Equal Comparison Opposite of the == operator, if two bags 
      have at least one element different they are not equal
   @param a_bag to be compared with this (the calling) bag
   @return true if two bags have a differing element, false if they are equal
      In this case we can be sure that two arrays are not equal if they have 
                                                              different sizes.
**/
bool operator!=(const ArrayBag &a_bag);

What to do can you explain brief ?


Related Solutions

#include <iostream> #include <string> #include <vector> using namespace std; class Song{ public: Song(); //default constructor Song(string...
#include <iostream> #include <string> #include <vector> using namespace std; class Song{ public: Song(); //default constructor Song(string t, string a, double d); //parametrized constructor string getTitle()const; // return title string getAuthor()const; // return author double getDurationMin() const; // return duration in minutes double getDurationSec() const; // return song's duration in seconds void setTitle(string t); //set title to t void setAuthor(string a); //set author to a void setDurationMin(double d); //set durationMin to d private: string title; //title of the song string author;...
C++ code Why my code is not compiling? :( #include <iostream> #include <iomanip> #include <string> using...
C++ code Why my code is not compiling? :( #include <iostream> #include <iomanip> #include <string> using namespace std; const int CWIDTH = 26; int main() {    int choice;    double convertFoC, converCtoF;    double starting, endvalue, incrementvalue;    const int CWIDTH = 13;    do {       cin >> choice;    switch (choice)    {        cin >> starting;    if (starting == 28){       cout << "Invalid range. Try again.";    }    while (!(cin >> starting)){       string  garbage;       cin.clear();       getline(cin, garbage);       cout << "Invalid data Type, must be a number....
C++ CODE ONLY Using the following code. #include <iostream> #include <string> #include <climits> #include <algorithm> using...
C++ CODE ONLY Using the following code. #include <iostream> #include <string> #include <climits> #include <algorithm> using namespace std; // M x N matrix #define M 5 #define N 5 // Naive recursive function to find the minimum cost to reach // cell (m, n) from cell (0, 0) int findMinCost(int cost[M][N], int m, int n) {    // base case    if (n == 0 || m == 0)        return INT_MAX;    // if we're at first cell...
Complete the following TODO: parts of the code in C++ #include <iostream> #include <string> #include <limits>...
Complete the following TODO: parts of the code in C++ #include <iostream> #include <string> #include <limits> #include <vector> using namespace std; // // CLASS: NODE // class Node{ public: int value = 0; // our node holds an integer Node *next = nullptr; // our node has a pointer to the next Node Node(int i){ // contructor for our Node class value = i; // store a copy of argument "i" in "value" next = nullptr; // be sure next...
A C++ question: I want to indent the code of this C++ program: #include<iostream> #include<cstring> using...
A C++ question: I want to indent the code of this C++ program: #include<iostream> #include<cstring> using namespace std; int lastIndexOf(char *s, char target) { int n=strlen(s); for(int i=n-1;i>=0;i--) { if(s[i]==target) { return i; } } return -1; } void reverse(char *s) { int n=strlen(s); int i=0,j=n-1; while(i<=j) { char temp=s[i]; s[i]=s[j]; s[j]=temp; i++; j--; } return; } int replace(char *s, char target, char replacementChar) { int len=strlen(s); int total=0; for(int i=0;i<len;i++) { if(s[i]==target) { s[i]=replacementChar; total+=1; } } return total;...
C++ finish the AVL Tree code: #include "AVLNode.h" #include "AVLTree.h" #include <iostream> #include <string> using namespace...
C++ finish the AVL Tree code: #include "AVLNode.h" #include "AVLTree.h" #include <iostream> #include <string> using namespace std; AVLTree::AVLTree() { root = NULL; } AVLTree::~AVLTree() { delete root; root = NULL; } // insert finds a position for x in the tree and places it there, rebalancing // as necessary. void AVLTree::insert(const string& x) { // YOUR IMPLEMENTATION GOES HERE } // remove finds x's position in the tree and removes it, rebalancing as // necessary. void AVLTree::remove(const string& x) {...
Hi, i need flowchart for this code (C++) please, THANX #include <iostream> #include <thread> #include <unistd.h>...
Hi, i need flowchart for this code (C++) please, THANX #include <iostream> #include <thread> #include <unistd.h> #include <semaphore.h> #include <pthread.h> using namespace std; #define NRO 6 // Número de coches //Puente declarado con matriz y valor entero void Puente(string array, int value); // Variable global int Norte = 1; int Sur = 1; sem_t mutex1; //Coche al norte void* NorteC(void* arg){ sem_wait(&mutex1); string array = "En el lado Norte "; // Norte cout<<array<<"del puente, el coche #"<<Norte<<" puede cruzar el...
Complete the C++ code #include <iostream> #include <stdlib.h> #include <time.h> using namespace std; struct Cell {...
Complete the C++ code #include <iostream> #include <stdlib.h> #include <time.h> using namespace std; struct Cell { int val; Cell *next; }; int main() { int MAX = 10; Cell *c = NULL; Cell *HEAD = NULL; srand (time(NULL)); for (int i=0; i<MAX; i++) { // Use dynamic memory allocation to create a new Cell then initialize the // cell value (val) to rand(). Set the next pointer to the HEAD and // then update HEAD. } print_cells(HEAD); }
Please comments this C++ code and show screenshots of the outputs main.cpp #include<iostream> #include<vector> #include<string> #include"BST.h"...
Please comments this C++ code and show screenshots of the outputs main.cpp #include<iostream> #include<vector> #include<string> #include"BST.h" #include"BST.cpp" using namespace std; std::vector<std::string> tokenize(char line[]) {    std::vector<std::string> tok;        std::string word = "";        for (int i = 0; i < strlen(line); i++)        {            if (i == strlen(line) - 1)            {                word += line[i];                tok.push_back(word);                return tok;       ...
For C++ Consider the following code defining classes for assets and office equipment: #include<string> #include<iostream> using...
For C++ Consider the following code defining classes for assets and office equipment: #include<string> #include<iostream> using namespace std; // class definition for asset class Asset{ protected: int value; // value of asset in cents public: Asset(int value); // constructor int get_value(); // get the value }; Asset::Asset(int val){ // implementation of constructor value=val; } int Asset::get_value(){ // implementation of get_value return value; } // abstract class for office equipment class OfficeEquipment:public Asset{ public: OfficeEquipment(int val); // constructor virtual void use_item()...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT