Be sure to address the question marks in the comments in all files and state the
ID: 3889192 • Letter: B
Question
Be sure to address the question marks in the comments in all files and state the preconditions and post conditions for each function. If any functions are not fully tested add a test to the test program. In either code comments or in your report, state where each function is tested. All three files are provided, bag2.h, bag2.cpp
// FILE: bag2.h
// CLASS PROVIDED: ?
//
// TYPEDEF and MEMBER CONSTANTS for the bag class:
// ?
//
//
// CONSTRUCTORs for the bag class:
// ?
// Postcondition:
//
// MODIFICATION MEMBER FUNCTIONS for the bag class:
// ?
//
// CONSTANT MEMBER FUNCTIONS for the bag class:
// ?
//
// NONMEMBER FUNCTIONS for the bag class:
// ?
//
#ifndef BAG_H
#define BAG_H
#include <cstdlib> // Provides size_t
class bag
{
public:
typedef int value_type;
typedef std::size_t size_type;
static const size_type CAPACITY = 30;
bag( ) { used = 0; }
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);
size_type size( ) const {
return used; }
size_type count(const value_type& target) const;
private:
value_type data[CAPACITY];
size_type used;
};
// NONMEMBER FUNCTIONS for the bag class
bag operator +(const bag& b1, const bag& b2);
#endif
// FILE: bag2.cpp
// CLASS IMPLEMENTED: bag
//
//
#include <algorithm> // Provides ??
#include <cassert> // Provides ??
#include "bag2.h"
using namespace std;
const bag::size_type bag::CAPACITY;
// ?
bag::size_type bag::erase(const value_type& target)
{
size_type index = 0;
size_type many_removed = 0;
while (index < used)
{
if (data[index] == target)
{
--used;
data[index] = data[used];
++many_removed;
}
else
++index;
}
return many_removed;
}
// ?
bool bag::erase_one(const value_type& target)
{
size_type index; //?
index = 0;
while ((index < used) && (data[index] != target))
++index;
if (index == used)
return false; // ?
// ?
--used;
data[index] = data[used];
return true;
}
//?
void bag::insert(const value_type& entry)
// Library facilities used: ?
{
assert(size( ) < CAPACITY);
data[used] = entry;
++used;
}
//?
void bag::operator +=(const bag& addend)
// Library facilities used: ?
{
assert(size( ) + addend.size( ) <= CAPACITY);
copy(addend.data, addend.data + addend.used, data + used);
used += addend.used;
}
//?
bag::size_type bag::count(const value_type& target) const
{
size_type answer;
size_type i;
answer = 0;
for (i = 0; i < used; ++i)
if (target == data[i])
++answer;
return answer;
}
//?
bag operator +(const bag& b1, const bag& b2)
// Library facilities used: ?
{
bag answer;
assert(b1.size( ) + b2.size( ) <= bag::CAPACITY);
answer += b1;
answer += b2;
return answer;
}
// FILE: bag_test.cpp
// This is a small demonstration program showing how the bag class is used.
#include <iostream> // Provides ?
#include <cstdlib> // Provides ?
#include "bag2.h" // ?
#include "bag2.cpp" // ?
using namespace std; //?
// ?
void get_ages(bag& ages);
void check_ages(bag& ages);
//?
int main( )
{
bag ages;
get_ages(ages);
check_ages(ages);
cout << "May your family live long and prosper." << endl;
return EXIT_SUCCESS;
}
// ?
void get_ages(bag& ages)
{
int user_input;
cout << "Type the ages in your family." << endl;
cout << "Type a negative number when you are done:" << endl;
cin >> user_input;
while (user_input >= 0)
{
if (ages.size( ) < ages.CAPACITY)
ages.insert(user_input);
else
cout << "I have run out of room and canít add that age." << endl;
cin >> user_input;
}
}
//?
void check_ages(bag& ages)
{
int user_input;
cout << "Type those ages again. Press return after each age:" << endl;
while (ages.size( ) > 0)
{
cin >> user_input;
if (ages.erase_one(user_input))
cout << "Yes, I've got that age and will remove it." << endl;
else
cout << "No, that age does not occur!" << endl;
}
}
Explanation / Answer
/ /CLASS PROVIDED: BAG
//
// TYPEDEF and MEMBER CONSTANTS for the bag class:
// Type defs are value_type and size_type
//Member constants is CAPACITY
//
// CONSTRUCTORs for the bag class:
// bag( ) { used = 0; }
// Postcondition:
//
// MODIFICATION MEMBER FUNCTIONS for the bag class:
// 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);
//
// CONSTANT MEMBER FUNCTIONS for the bag class:
// size_type size( ) const;
//size_type count(const value_type& target) const;
//
// NONMEMBER FUNCTIONS for the bag class:
// bag operator +(const bag& b1, const bag& b2);
//
#ifndef BAG_H
#define BAG_H
#include <cstdlib> // Provides size_t
class bag
{
public:
typedef int value_type;
typedef std::size_t size_type;
static const size_type CAPACITY = 30;
bag( ) { used = 0; }
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);
size_type size( ) const {
return used; }
size_type count(const value_type& target) const;
private:
value_type data[CAPACITY];
size_type used;
};
// NONMEMBER FUNCTIONS for the bag class
bag operator +(const bag& b1, const bag& b2);
#endif
// FILE: bag2.cpp
// CLASS IMPLEMENTED: bag
//
//
#include <algorithm> // The header <algorithm> defines a collection of functions especially designed to be used on //ranges of elements.
//A range is any sequence of objects that can be accessed through iterators or pointers, such as an array or an //instance of some of the STL(Standard template librairy) containers.
#include <cassert> // This header was originally in the C standard library as <assert.h>.
//This header is part of the error handling library.In the program assert() function used.
#include "bag2.h"
using namespace std;
const bag::size_type bag::CAPACITY; // defines the capacity of bag as 30 that is constant
//precondition is index value should be less then used and bag should be atleast once used
// that is age value should be entered in bag it must be greater than 0
bag::size_type bag::erase(const value_type& target)
{
size_type index = 0;
size_type many_removed = 0;
while (index < used)
{
if (data[index] == target)
{
--used;
data[index] = data[used];
++many_removed;
}
else
++index;
}
return many_removed;
}
//precondition is index value should be less then used and bag should be atleast once used
// that is age value should be entered in bag it must be greater than 0
bool bag::erase_one(const value_type& target)
{
size_type index; //iterates through bag value
index = 0;
while ((index < used) && (data[index] != target))
++index;
if (index == used)
return false; // does not remove the age
// removes the age from bag
--used;
data[index] = data[used];
return true;
}
//there should be user input and current size of the bag should be less than capacity of a bag then only data is inserted
void bag::insert(const value_type& entry)
// Library facilities used: #include<cassert.h>
{
assert(size( ) < CAPACITY);
data[used] = entry;
++used;
}
//operator overloading of addiion assignment and contents are added to bag
void bag::operator +=(const bag& addend)
// Library facilities used: #include<cassert.h>
{
assert(size( ) + addend.size( ) <= CAPACITY);
copy(addend.data, addend.data + addend.used, data + used);
used += addend.used;
}
//calculate the size of the bag return target value precondition is bag should at least once used
bag::size_type bag::count(const value_type& target) const
{
size_type answer;
size_type i;
answer = 0;
for (i = 0; i < used; ++i)
if (target == data[i])
++answer;
return answer;
}
//+ operator overloaded and adds the contents of two Bag and precondition bag should contain data
bag operator +(const bag& b1, const bag& b2)
// Library facilities used: #include<cassert.h>
{
bag answer;
assert(b1.size( ) + b2.size( ) <= bag::CAPACITY);
answer += b1;
answer += b2;
return answer;
}
// FILE: bag_test.cpp
// This is a small demonstration program showing how the bag class is used.
#include <iostream> // Provides Standard Input / Output Streams Library. Header that defines the standard input/output //stream objects:
#include <cstdlib> // Provides This header defines several general purpose functions, including dynamic memory //management, random number generation, communication with the environment, integer arithmetics, //searching, sorting and converting.
#include "bag2.h" //provides Class declaration of bag and and necessary function and constant values declared such as //CAPACITY
#include "bag2.cpp" // implementaion of class bag and member functions
using namespace std; //he built in C++ library routines are kept in the standard namespace. That includes stuff like //cout, cin, string, vector, map, etc. Because these tools are used so commonly, it's //popular to add "using namespace std" at the top of your source code so that you won't have to //type the std:: prefix constantly.
// function declaration
void get_ages(bag& ages);
void check_ages(bag& ages);
//main function
int main( )
{
bag ages;
get_ages(ages);
check_ages(ages);
cout << "May your family live long and prosper." << endl;
return EXIT_SUCCESS;
}
// gets the ages and inserts in the bag
void get_ages(bag& ages)
{
int user_input;
cout << "Type the ages in your family." << endl;
cout << "Type a negative number when you are done:" << endl;
cin >> user_input;
while (user_input >= 0)
{
if (ages.size( ) < ages.CAPACITY)
ages.insert(user_input);
else
cout << "I have run out of room and canít add that age." << endl;
cin >> user_input;
}
}
//erases the ages
void check_ages(bag& ages)
{
int user_input;
cout << "Type those ages again. Press return after each age:" << endl;
while (ages.size( ) > 0)
{
cin >> user_input;
if (ages.erase_one(user_input))
cout << "Yes, I've got that age and will remove it." << endl;
else
cout << "No, that age does not occur!" << endl;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.