Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The files taht are important to the question in the photo are, abstract _ bag.hpp #ifndef _ ABSTRACT _ BAG _ HPP _ #define _

The files taht are important to the question in the photo are,
abstract_bag.hpp
#ifndef _ABSTRACT_BAG_HPP_
#define _ABSTRACT_BAG_HPP_
#include
//edit
#endif
bag_simple.hpp
#ifndef STUDENT_BAG_HPP
#define STUDENT_BAG_HPP
#include "abstract_bag.hpp"
template class Bag{
public:
Bag();
~Bag();
std::size_t getCurrentSize() const;
bool isEmpty() const;
bool add(const T& entry);
bool remove(const T& entry);
void clear();
std::size_t getFrequencyOf(const T& entry) const;
bool contains(const T& entry) const;
static const std::size_t MAXSIZE =100;
private:
// implementation using fixed automatic storage
std::size_t size;
T data[MAXSIZE];
};
#include "bag_simple.tpp"
#endif
bag_simple.tpp
#include "bag_simple.hpp"
#include
// implementation using fixed automatic storage
template Bag::Bag(): size(0){}
template Bag::~Bag(){}
template std::size_t Bag::getCurrentSize() const { return size; }
template bool Bag::isEmpty() const { return (size ==0); }
template bool Bag::add(const T& entry){
if(size >= MAXSIZE){
return false;
}
data[size]= entry;
++size;
return true;
}
template bool Bag::remove(const T& entry){
if(size ==0) return false;
std::size_t idx =0;
for(std::size_t i =0; i size; ++i){
if(data[i]== entry) break;
++idx;
}
if(idx == size) return false;
--size;
for(std::size_t i = idx; i size; ++i){
data[i]= data[i+1];
}
return true;
}
template void Bag::clear(){ size =0; }
template std::size_t Bag::getFrequencyOf(const T& entry) const {
std::size_t freq =0;
for(std::size_t i =0; i size; ++i){
if(data[i]== entry)++freq;
}
return freq;
}
template bool Bag::contains(const T& entry) const {
return (getFrequencyOf(entry)!=0);
}
bag_test.cpp
#define CATCH_CONFIG_MAIN
#include "catch.hpp"
#include "bag_simple.hpp"
// force template expansion
template class Bag;
TEST_CASE("Test Type", "[Bag]"){
REQUIRE((std::is_base_of AbstractBag,Bag >::value)==1);
}
TEST_CASE("Test Construction/Destruction","[Bag]"){
Bag b;
REQUIRE(b.getCurrentSize()==0);
REQUIRE(b.isEmpty());
}
TEST_CASE("Test add", "[Bag]"){
Bag b;
REQUIRE(b.getCurrentSize()==0);
REQUIRE(b.isEmpty());
for(int i =0; i Bag::MAXSIZE; ++i){
REQUIRE(b.add(i));
REQUIRE(b.getCurrentSize()== i+1);
REQUIRE(!b.isEmpty());
}
REQUIRE(!b.add(0));
}
TEST_CASE("Test remove", "[Bag]"){
Bag b;
REQUIRE(b.getCurrentSize()==0);
REQUIRE(b.isEmpty());
for(int i =0; i Bag::MAXSIZE; ++i){
REQUIRE(b.add(i));
REQUIRE(b.getCurrentSize()== i+1);
REQUIRE(!b.isEmpty());
}
REQUIRE(!b.remove(-1));
for(int i =0; i Bag::MAXSIZE; ++i){
REQUIRE(b.remove(i));
}
REQUIRE(!b.remove(0));
}
TEST_CASE("Test clear", "[Bag]"){
Bag b;
REQUIRE(b.getCurrentSize()==0);
REQUIRE(b.isEmpty());
b.clear();
REQUIRE(b.getCurrentSize()==0);
REQUIRE(b.isEmpty());
for(int i =0; i Bag::MAXSIZE; ++i){
REQUIRE(b.add(i));
REQUIRE(b.getCurrentSize()== i+1);
REQUIRE(!b.isEmpty());
}
b.clear();
REQUIRE(b.getCurrentSize()==0);
REQUIRE(b.isEmpty());
}
TEST_CASE("Test frequency", "[Bag]"){
{
Bag b;
REQUIRE(b.getCurrentSize()==0);
REQUIRE(b.isEmpty());
REQUIRE(b.getFrequencyOf(0)==0);
for(int i =0; i Bag::MAXSIZE; ++i){
REQUIRE(b.add(i));
REQUIRE(b.getCurrentSize()== i+1);
REQUIRE(!b.isEmpty());
}
for(int i =0; i Bag::MAXSIZE; ++i){
REQUIRE(b.getFrequencyOf(i)==1);
}
}
{
Bag b;
REQUIRE(b.getCurrentSize()==0);
REQUIRE(b.isEmpty());
for(int i =0; i Bag::MAXSIZE; ++i){
REQUIRE(b.add(-1));
REQUIRE(b.getCurrentSize()== i+1);
REQUIRE(!b.isEmpty());
}
std::size_t EXPECTED = Bag::MAXSIZE;
REQUIRE(b.getFrequencyOf(-1)== EXPECTED);
REQUIRE(b.getFrequencyOf(0)==0);
}
}
TEST_CASE("Test contains", "[Bag]"){
{
Bag b;
REQUIRE(b.getCurrentSize()==0);
REQUIRE(b.isEmpty());
REQUIRE(!b.contains(0));
for(int i =0; i Bag::MAXSIZE; ++i){
REQUIRE(b.add(i));
REQUIRE(b.getCurrentSize()== i+1);
REQUIRE(!b.isEmpty());
}
for(int i =0; i Bag::MAXSIZE; ++i){
REQUIRE(b.contains(i));
}
REQUIRE(!b.contains(-1));
}
{
Bag b;
REQUIRE(b.getCurrentSize()==0);
RE
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Case Studies In Business Data Bases

Authors: James Bradley

1st Edition

0030141346, 978-0030141348

Students also viewed these Databases questions

Question

Address an envelope properly.

Answered: 1 week ago

Question

Discuss guidelines for ethical business communication.

Answered: 1 week ago