Bag - UAH CPE 212 Fundamentals of Software Engineering...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
UAH CPE 212 Fundamentals of Software Engineering Agenda Class 6 Container Classes Container Classes The Bag Key Concepts
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
UAH CPE 212 This Time Container Classes What are they? Structures which hold things BUT also Abstract Data Types / User Defined Types Required Operations Put stuff in Take stuff out (Find stuff) Data Structures are Containers Some impose specific order on the data Implementation Notes typedef versus templates size_t vs unsigned int Static Member constants / enum Our Example - The Bag Constructors Destructor Assignment
Background image of page 2
UAH CPE 212 Bag Class Declaration #ifndef BAG1_H #define BAG1_H #include <stdlib.h> // Provides size_t class Bag { public: // TYPEDEF and MEMBER CONSTANTS enum { CAPACITY = 30 }; // Or: static const size_t CAPACITY = 30; typedef int Item; // CONSTRUCTOR Bag( ) { used = 0; } // MODIFICATION MEMBER FUNCTIONS void insert(const Item& entry); void remove(const Item& target); void operator +=(const Bag& addend); // CONSTANT MEMBER FUNCTIONS size_t size( ) const { return used; } size_t occurrences(const Item& target) const; private: Item data[CAPACITY]; // The array to store items size_t used; // How much of array is used }; // NONMEMBER FUNCTIONS for the Bag class Bag operator +(const Bag& b1, const Bag& b2); #endif
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
UAH CPE 212 Bag Class Definition 1 // FILE: bag1.cxx // CLASS IMPLEMENTED: Bag (see bag1.h for documentation) // INVARIANT for the Bag ADT: // 1. The number of items in the Bag is in the member //variable used; // 2. For an empty Bag, we do not care what is stored in any of data; for a // non-empty Bag the items in the Bag are stored in //data[0] through // data[used-1], and we don't care what's in the rest //of data. #include <assert.h>
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 14

Bag - UAH CPE 212 Fundamentals of Software Engineering...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online