I need help solving this question from the practice final exam given to us to prepare for the final in C++
#include <iostream>
using namespace std;
/*
The following is code for an OrderedCollection container and its
related iterator.
The container has a capacity determined by a constructor
parameter.
The container does not grow. Code that adds elements to the
container ensures that the capacity of
the container is never exceeded. An attempt to add an item to a
full container is ignored by the add() methods.
Requirement
The Bankaccount class currently only has a "balance"
property.
Modify the code so that BankAccouts have a string property "owner"
as well.
Modify how BankAccount objects print to, say, "Lou: $100" instead
of just "$100".
The Bankaccount class currently implement == and !=
operators that are based on "identity".
That is, the actual addresses of objects are compared. We want to
implement
these operators as "equality" rather than "identity"
operators.
Modify the operators so that A == B is true if and only if
bankaccounts A and B have equivalent owner strings and have the
equal balances.
That is, we aren't seeing if A and B refer to the same object
memory but rather
that the objects are "equal" because their owner strings and
balances are equal.
(Also modify the != operator).
Modify the OrderedCollection class so that it behaves
like an OrderedSet. The difference is that
a set would not be allowed to contain duplicates (based on your new
== operator of items stored in the
set). Here the add methods need to be changed so that items will
not be added if
an equivalent item is already in the set. (Note for this exercise
you don't actually have to change
the name of the class to OrderedSet, though that would be
ideal).
Modify the container so that it grows if it gets
full.
If its internal array gets full a new one should be allocated that
is bigger, items copied over,
and the old one properly deteted.
*/
template <class T> class OrderedIterator; //forward declaration
template <class T>
class OrderedCollection {
// =================
friend class OrderedIterator<T>;
const int capacity; //maximum size of the container
int size; //number of actual elements in the container
T **buffer; //pointer to memory for holding elements
public:
OrderedCollection(int aCapacity = 100);
OrderedCollection(const OrderedCollection<T> & c);
~OrderedCollection();
typedef OrderedIterator<T> iterator;
int getSize() const {return size;} //answer number of
elements
bool isFull()const {return size == capacity;} //answer if full
OrderedCollection<T> & addLast( T & element);
//add to end
OrderedCollection<T> & addFirst( T & element); //add
to front
T & removeLast();//remove last element
T & removeFirst(); //remove first element
OrderedCollection<T> & remove(const T & element);
//remove all items == element
iterator begin(void); //provide an start iterator
iterator end(void); //provide an end iterator
void print(){
cout << "Collection\n";
cout << "==========\n";
for(int i=0; i<size; i++) cout <<
*buffer[i];
}
};
template <class T>
class OrderedIterator { //The iterator class
// ===============
int index; //location in the container being
iterated
OrderedCollection<T> & s; //container being
iterated
public:
OrderedIterator(OrderedCollection<T> & set,
int position = 0);
OrderedIterator<T> & operator++(int); //to
advance to next element
T & operator*(); //to get element
bool operator==(const OrderedIterator<T> &
iter);
bool operator!=(const OrderedIterator<T> &
iter);
};
//Constructor Implementations
template <class T>
OrderedCollection<T>::OrderedCollection(int aCapacity)
: capacity(aCapacity),size(0), buffer(new T*[aCapacity]) {}
template <class T>
OrderedCollection<T>::OrderedCollection(const
OrderedCollection<T> & c)
: capacity(c.capacity), size(c.size), buffer(new T*[c.capacity])
{
for(int i=0; i< capacity; i++) buffer[i] =
c.buffer[i];
}
template <class T>
OrderedCollection<T>::~OrderedCollection() {delete [] buffer;
}
//Method Implementations
template <class T>
OrderedCollection<T> &
OrderedCollection<T>::addLast(T & element){
if(size < capacity) buffer[size++] = &element; return
*this;
}
template <class T>
OrderedCollection<T> &
OrderedCollection<T>::addFirst(T & element){
if(size < capacity){
size++;
for(int i=size-1; i>0; i--) buffer[i] =
buffer[i-1];
buffer[0] = &element;
}
return *this;
}
template <class T>
T & OrderedCollection<T>::removeLast(){
return * buffer[size--];
}
template <class T>
T & OrderedCollection<T>::removeFirst(){
T* temp;
size--;
temp = buffer[0];
for(int i=0; i<size; i++) buffer[i] =
buffer[i+1];
return *temp;
}
template <class T>
OrderedCollection<T> &
OrderedCollection<T>::remove(const T & item){
//remove all elements which are == item (assume T implements ==
oper)
int newIndex = 0;
for(int i=0; i<size; i++){
if(*buffer[i] != item)
buffer[newIndex++] = buffer[i];
}
size = newIndex;
return *this;
}
template <class T>
typename OrderedCollection<T>::iterator
OrderedCollection<T>::begin(void) { //provide an start
iterator
return OrderedIterator<T>(*this);
}
template <class T>
typename OrderedCollection<T>::iterator
OrderedCollection<T>::end(void) { //provide an end
iterator
return OrderedIterator<T>(*this, size);
}
template <class T>
OrderedIterator<T>::OrderedIterator(OrderedCollection<T>
& set, int position)
: index(position), s(set) {}
template <class T>
OrderedIterator<T> &
OrderedIterator<T>::operator++(int) {
index++; return *this;
}
template <class T>
T & OrderedIterator<T>::operator*() {
return *(s.buffer[index]);
}
template <class T>
bool OrderedIterator<T>::operator==(const
OrderedIterator<T> & iter) {
return (&s == &(iter.s)) && (index ==
iter.index);
}
template <class T>
bool OrderedIterator<T>::operator!=(const
OrderedIterator<T> & iter) {
return !(*this == iter);
}
class BankAccount{
double balance;
public:
BankAccount(double anAmount):balance(anAmount) {}
bool operator== (const BankAccount & b) {return
&b == this;}
bool operator!= (const BankAccount & b) {return
&b != this;}
void printOn(ostream & o) const { o << "$"
<< balance << "\n"; }
};
ostream & operator<<(ostream & o, const BankAccount
& b){
b.printOn(o);
return o;
}
//Example of how the collection and iterator could be used
int main() {
BankAccount b1(100.0), b2(50.0), b3(200.0), b4(150.0);
OrderedCollection<BankAccount> accounts;
accounts.addLast(b1).addLast(b2).addLast(b3).addLast(b4).addLast(b2);
for(OrderedCollection<BankAccount>::iterator itr =
accounts.begin();
itr !=
accounts.end(); itr++)
cout << *itr;
accounts.print();
accounts.remove(b2);
accounts.print();
return 0;
}//end main
Code:
template <class T> class OrderedIterator; //forward declaration
template <class T>
class OrderedCollection {
// =================
friend class OrderedIterator<T>;
const int capacity; //maximum size of the
container
int size; //number of actual elements in the
container
T **buffer; //pointer to memory for holding
elements
public:
OrderedCollection(int aCapacity = 100);
OrderedCollection(const OrderedCollection<T>
& c);
~OrderedCollection();
typedef OrderedIterator<T> iterator;
int getSize() const { return size; } //answer
number of elements
bool isFull()const { return size == capacity; }
//answer if full
OrderedCollection<T> & addLast(T &
element); //add to end
OrderedCollection<T> & addFirst(T &
element); //add to front
T & removeLast();//remove last element
T & removeFirst(); //remove first element
OrderedCollection<T> & remove(const T &
element); //remove all items == element
iterator begin(void); //provide an start
iterator
iterator end(void); //provide an end iterator
void print() {
cout << "Collection\n";
cout << "==========\n";
for (int i = 0; i < size; i++)
cout << *buffer[i];
}
};
template <class T>
class OrderedIterator { //The iterator class
// ===============
int index; //location in the container being
iterated
OrderedCollection<T> & s; //container being
iterated
public:
OrderedIterator(OrderedCollection<T> & set,
int position = 0);
OrderedIterator<T> & operator++(int); //to
advance to next element
T & operator*(); //to get element
bool operator==(const OrderedIterator<T> &
iter);
bool operator!=(const OrderedIterator<T> &
iter);
};
//Constructor Implementations
template <class T>
OrderedCollection<T>::OrderedCollection(int aCapacity)
: capacity(aCapacity), size(0), buffer(new
T*[aCapacity]) {}
template <class T>
OrderedCollection<T>::OrderedCollection(const
OrderedCollection<T> & c)
: capacity(c.capacity), size(c.size), buffer(new
T*[c.capacity]) {
for (int i = 0; i < capacity; i++) buffer[i] =
c.buffer[i];
}
template <class T>
OrderedCollection<T>::~OrderedCollection() { delete[] buffer;
}
//Method Implementations
template <class T>
OrderedCollection<T> &
OrderedCollection<T>::addLast(T & element) {
//add only if the entry is not present
//Changed : checking for the duplicate element to
mimic the set behaviour
bool bEntryFound = false;
for (int i = 0; i < size; i++)
{
if (*buffer[i] == element)
{
bEntryFound =
true;
break;
}
}
if(!bEntryFound)
{
//Changed : Add only if it is not
already present
if (size < capacity)
buffer[size++] =
&element;
else
{
//Changed : if
exceeds the capacity reallocate
T** tempBuffer =
new T*[capacity * 2];
for (int i = 0;i
< size;i++)
{
tempBuffer[i] = buffer[i];
}
delete[]
buffer;
buffer =
tempBuffer;
}
}
return *this;
}
template <class T>
OrderedCollection<T> &
OrderedCollection<T>::addFirst(T & element) {
if (size < capacity) {
size++;
for (int i = size - 1; i > 0;
i--) buffer[i] = buffer[i - 1];
buffer[0] = &element;
}
return *this;
}
template <class T>
T & OrderedCollection<T>::removeLast() {
return *buffer[size--];
}
template <class T>
T & OrderedCollection<T>::removeFirst() {
T* temp;
size--;
temp = buffer[0];
for (int i = 0; i < size; i++) buffer[i] = buffer[i
+ 1];
return *temp;
}
template <class T>
OrderedCollection<T> &
OrderedCollection<T>::remove(const T & item) {
//remove all elements which are == item (assume T
implements == oper)
int newIndex = 0;
for (int i = 0; i < size; i++) {
if (*buffer[i] != item)
buffer[newIndex++] = buffer[i];
}
size = newIndex;
return *this;
}
template <class T>
typename OrderedCollection<T>::iterator
OrderedCollection<T>::begin(void) { //provide an start
iterator
return OrderedIterator<T>(*this);
}
template <class T>
typename OrderedCollection<T>::iterator
OrderedCollection<T>::end(void) { //provide an end
iterator
return OrderedIterator<T>(*this, size);
}
template <class T>
OrderedIterator<T>::OrderedIterator(OrderedCollection<T>
& set, int position)
: index(position), s(set) {}
template <class T>
OrderedIterator<T> &
OrderedIterator<T>::operator++(int) {
index++; return *this;
}
template <class T>
T & OrderedIterator<T>::operator*() {
return *(s.buffer[index]);
}
template <class T>
bool OrderedIterator<T>::operator==(const
OrderedIterator<T> & iter) {
return (&s == &(iter.s)) && (index ==
iter.index);
}
template <class T>
bool OrderedIterator<T>::operator!=(const
OrderedIterator<T> & iter) {
return !(*this == iter);
}
class BankAccount {
double balance;
//Change : added the owner field
string _owner;
public:
//changed : initializing owner field
BankAccount(double anAmount,string owner)
:balance(anAmount),_owner(owner) {}
bool operator== (const BankAccount & b)
{
//Changed : added both balance and
owner for equality checking
return (this->balance ==
b.balance && this->_owner == b._owner);
}
bool operator!= (const BankAccount & b)
{
//Changed : added both the balance
and owner for inequality checking
return (this->balance !=
b.balance || this->_owner != b._owner);
}
void printOn(ostream & o) const
{
//Changed : added owner and balance
for printing
o <<_owner<< ": $"
<< balance << "\n";
}
};
ostream & operator<<(ostream & o, const BankAccount
& b) {
b.printOn(o);
return o;
}
//Example of how the collection and iterator could be used
int main() {
//Changed added the name into the constructor
parameter
BankAccount b1(100.0,"Lou"), b2(100.0,""),
b3(200.0,"Mark"), b4(150.0,"Waugh");
OrderedCollection<BankAccount> accounts;
accounts.addLast(b1).addLast(b2).addLast(b3).addLast(b4).addLast(b2);
for (OrderedCollection<BankAccount>::iterator
itr = accounts.begin();
itr != accounts.end(); itr++)
cout << *itr;
accounts.print();
accounts.remove(b2);
accounts.print();
return 0;
}//end main
OUTPUT:
Microsoft Visual Studio Debug Console Lou: $100 Housen: $50 Mark: $200 Waugh: $150 Collection Lou: $100 Housen: $50 Mark: $200 Waugh: $150 Collection Lou: $100 Mark: $200 Waugh: $150
I need help solving this question from the practice final exam given to us to prepare...
Below is the given code of implementation: #include <string> #include <iostream> #include <list> #include <cassert> using namespace std; class List; class Iterator; class Node { public: /* Constructs a node with a given data value. @param s the data to store in this node */ Node(string s); /* Destructor */ ~Node() {} private: string data; Node* previous; Node* next; friend class List; friend class Iterator; }; class List { public: /** Constructs an empty list. */ List(); /* Destructor. Deletes...
A library maintains a collection of books. Books can be added to and deleted from and checked out and checked in to this collection. Title and author name identify a book. Each book object maintains a count of the number of copies available and the number of copies checked out. The number of copies must always be greater than or equal to zero. If the number of copies for a book goes to zero, it must be deleted from the...
I have the following c++ data structures assignment: Copy Constructors, Destructors, and Assignment Operators An understanding of how to implement copy constructors, destructors, and assignment operators is essential when working with data structures using dynamic memory allocation. Failure to implement these methods correctly can and probably will result in memory leaks. In this project, you are provided with a working implementation of a doubly-linked list in which the copy constructor, destructor, and assignment operator methods are not complete. To complete...
In this assignment, you will implement a sort method on singly-linked and doubly-linked lists. Implement the following sort member function on a singly-linked list: void sort(bool(*comp)(const T &, const T &) = defaultCompare); Implement the following sort member function on a doubly-linked list: void sort(bool(*comp)(const T &, const T &) = defaultCompare); The sort(…) methods take as a parameter a comparator function, having a default assignment of defaultCompare, a static function defined as follows: template <typename T> static bool defaultCompare(const...
Hi, I hope I can get some help with the following exercise in C++( CPP): 1.Write an additional method called push_back(int) that will add an integer to the end of the list. You can modify the provided code. 2.Modify the Node class and LinkedList class so that you can access your parent node (double linked-list). #include #include using namespace std; typedef int Type; enum Boolean { False = 0, True }; class Item { friend class SLList; public: Type getVal()...
(The SortedLinkedList class template) Complete the SortedLinkedList class template which is a doubly linked list and is implemented with a header node and a tail node. // SortedLinkedList.h // SortedLinkedList.h // A collection of data are stored in the list by ascending order #ifndef SORTEDLIST_H #define SORTEDLIST_H using namespace std; template <typename T> class SortedList { private: // The basic single linked list node type. // Nested inside of SortedList. struct NodeType { T data; NodeType* next; NodeType* prev; NodeType(const...
Please show me how to overload the operators << and >> #ifndef LINK_LIST #define LINK_LIST #include <iostream> using namespace std; template <typename T> struct Int_Node { T value; Int_Node<T> *pre, *next; }; template <typename T> class Link_List { template <typename U> friend ostream &operator<<(ostream &, const Link_List<U> &);// print all integers in the list template <typename U> friend istream &operator>>(istream &, Link_List<U> &);// input a value at the back of the list, like insert_node(val);...
I need help with those two functions c++ #ifndef TRIPLE_H #define TRIPLE_H #include <iostream> #include <string> using namespace std; class Triple { private: int a, b, c; public: Triple(); // all elements have value 0 Triple(int k); // all elements have value k Triple(int x, int y, int z); // specifies all three elements Triple(string s); // string representation is "(a,b,c)" string toString(); // create a string representation of the vector void fromString(string s); // change the vector to equal...
I need help with the code below. It is a C program, NOT C++. It can only include '.h' libraries. I believe the program is in C++, but it must be a C program. Please help. // // main.c // float_stack_class_c_9_29 // // /* Given the API for a (fixed size), floating point stack class, write the code to create a stack class (in C). */ #include #include #include #include header file to read and print the output on console...
C++ assignment help! The instructions are below, i included the main driver, i just need help with calling the functions in the main function This assignment will access your skills using C++ strings and dynamic arrays. After completing this assignment you will be able to do the following: (1) allocate memory dynamically, (2) implement a default constructor, (3) insert and remove an item from an unsorted dynamic array of strings, (4) use the string class member functions, (5) implement a...