From d80c10626712eff3b94fdd26345efaa8fc520c5e Mon Sep 17 00:00:00 2001 From: Fuwn Date: Mon, 8 Apr 2024 01:20:21 -0700 Subject: feat(week_1): wk1_bagFns1_static --- week_1/bagFns1_static/bag.h | 90 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 week_1/bagFns1_static/bag.h (limited to 'week_1/bagFns1_static/bag.h') diff --git a/week_1/bagFns1_static/bag.h b/week_1/bagFns1_static/bag.h new file mode 100644 index 0000000..64c6ef8 --- /dev/null +++ b/week_1/bagFns1_static/bag.h @@ -0,0 +1,90 @@ +// FILE: bag1.h +// CLASS PROVIDED: bag (part of the namespace main_savitch_3) +// +// TYPEDEF and MEMBER CONSTANTS for the bag class: +// typedef ____ value_type +// bag::value_type is the data type of the items in the bag. It may be any +// of the C++ built-in types (int, char, etc.), or a class with a default +// constructor, an assignment operator, and operators to +// test for equality (x == y) and non-equality (x != y). +// +// typedef ____ size_type +// bag::size_type is the data type of any variable that keeps track of how +// many items are in a bag. +// +// static const size_type CAPACITY = _____ +// bag::CAPACITY is the maximum number of items that a bag can hold. +// +// CONSTRUCTOR for the bag class: +// bag( ) +// Postcondition: The bag has been initialized as an empty bag. +// +// MODIFICATION MEMBER FUNCTIONS for the bag class: +// size_type erase(const value_type& target); +// Postcondition: All copies of target have been removed from the bag. +// The return value is the number of copies removed (which could be zero). +// +// void erase_one(const value_type& target) +// Postcondition: If target was in the bag, then one copy has been removed; +// otherwise the bag is unchanged. A true return value indicates that one +// copy was removed; false indicates that nothing was removed. +// +// void insert(const value_type& entry) +// Precondition: size( ) < CAPACITY. +// Postcondition: A new copy of entry has been added to the bag. +// +// void operator +=(const bag& addend) +// Precondition: size( ) + addend.size( ) <= CAPACITY. +// Postcondition: Each item in addend has been added to this bag. +// +// CONSTANT MEMBER FUNCTIONS for the bag class: +// size_type size( ) const +// Postcondition: The return value is the total number of items in the bag. +// +// size_type count(const value_type& target) const +// Postcondition: The return value is number of times target is in the bag. +// +// NONMEMBER FUNCTIONS for the bag class: +// bag operator +(const bag& b1, const bag& b2) +// Precondition: b1.size( ) + b2.size( ) <= bag::CAPACITY. +// Postcondition: The bag returned is the union of b1 and b2. +// +// VALUE SEMANTICS for the bag class: +// Assignments and the copy constructor may be used with bag objects. + +#ifndef MAIN_SAVITCH_BAG1_H +#define MAIN_SAVITCH_BAG1_H +#include // Provides size_t + +namespace main_savitch_3 { +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); + // CONSTANT MEMBER FUNCTIONS + size_type size() const { return used; } + size_type count(const value_type &target) const; + value_type smallest() const; + value_type average() const; + void sort() const; + bool operator==(const bag &); + +private: + value_type data[CAPACITY]; // The array to store items + size_type used; // How much of array is used +}; + +// NONMEMBER FUNCTIONS for the bag class +bag operator+(const bag &b1, const bag &b2); +} // namespace main_savitch_3 + +#endif -- cgit v1.2.3