Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

implement library.cpp and book.cpp by using c++ and show the output. Library.h #pragma once #include #include namespace cs20a { using namespace std; class Book {

implement library.cpp and book.cpp by using c++ and show the output.

Library.h

#pragma once #include #include

namespace cs20a { using namespace std; class Book { public: Book(); Book(string author, string title, int copies = 1, int checkedOut = 0);

string getAuthor() const; string getTitle() const;

void setAuthor(string author); void setTitle(string title);

int getCopies() const; int getCheckedOut() const;

void addCopy(); void deleteCopy();

void checkOut(); void checkIn();

friend ostream& operator << (ostream& outs, const Book& book);

private: string author, title; int copies, checkedOut; }; }

Library.cpp

#include #include "Library.h"

namespace cs20a { Library::Library() {}

ostream & operator<<(ostream & outs, const Library & library) { for (int i = 0; i < library.getTotalBooks(); i++) outs << library.books[i] << endl;

return outs; }

//*** Code goes here ***// //*** Code goes here ***// //*** Code goes here ***//

}

Book.h

#pragma once #include #include

namespace cs20a { using namespace std; class Book { public: Book(); Book(string author, string title, int copies = 1, int checkedOut = 0);

string getAuthor() const; string getTitle() const;

void setAuthor(string author); void setTitle(string title);

int getCopies() const; int getCheckedOut() const;

void addCopy(); void deleteCopy();

void checkOut(); void checkIn();

friend ostream& operator << (ostream& outs, const Book& book);

private: string author, title; int copies, checkedOut; }; }

Book.cpp

#include #include "Book.h"

namespace cs20a { Book::Book() : author(), title(), copies(1), checkedOut(0) {}

Book::Book(string author, string title, int copies, int checkedOut) : author(author), title(title), copies(copies), checkedOut(checkedOut) {}

ostream & cs20a::operator<<(ostream & outs, const Book & book) { cout << book.title << ", by " << book.author << " Copies: " << book.copies << " Checked out: " << book.checkedOut; return outs; }

//*** Code goes here ***// //*** Code goes here ***// //*** Code goes here ***//

}

Librarydriver.cpp

#include #include "Library.h" #include "Book.h"

using namespace std; using namespace cs20a;

int main() { Library library;

try { cout << "--> Before Initial State." << endl;

library.addBook("Ernest Hemingway", "For Whom the Bell Tolls"); library.addBook("Ernest Hemingway", "For Whom the Bell Tolls"); library.addBook("John Steinbeck", "The Grapes of Wrath"); library.addBook("John Steinbeck", "The Grapes of Wrath"); library.addBook("John Steinbeck", "East of Eden"); library.addBook("Mark Twain", "Huckleberry Finn");

cout << library; } catch (const std::exception& e) { cout << "*** " << e.what() << " ***" << endl; } cout << "--> After Initial State." << endl << endl;

try { cout << "--> Before deleting Main Street and For Whom the Bell Tolls." << endl;

if (library.hasBook("Sinclair Lewis", "Main Street")) library.deleteBook("Sinclair Lewis", "Main Street");

library.deleteBook("Ernest Hemingway", "For Whom the Bell Tolls"); library.deleteBook("Ernest Hemingway", "For Whom the Bell Tolls");

cout << library; } catch (const std::exception& e) { cout << "*** " << e.what() << " ***" << endl; } cout << "--> After deleting Main Street and For Whom the Bell Tolls." << endl << endl;

try { cout << "--> Before checking out For Whom the Bell Tolls and The Grapes of Wrath." << endl;

if (library.hasBook("Ernest Hemingway", "For Whom the Bell Tolls")) library.checkOutBook("Ernest Hemingway", "For Whom the Bell Tolls");

if (library.hasBook("John Steinbeck", "The Grapes of Wrath")) library.checkOutBook("John Steinbeck", "The Grapes of Wrath");

cout << library; } catch (const std::exception& e) { cout << "*** " << e.what() << " ***" << endl; } cout << "--> After checking out For Whom the Bell Tolls and The Grapes of Wrath." << endl << endl;

try { cout << "--> Before checking checking in For Whom the Bell Tolls." << endl;

library.checkInBook("Ernest Hemingway", "For Whom the Bell Tolls"); library.checkInBook("Ernest Hemingway", "For Whom the Bell Tolls");

cout << library; } catch (const std::exception& e) { cout << "*** " << e.what() << " ***" << endl; } cout << "--> After checking checking in For Whom the Bell Tolls." << endl << endl;

try { cout << "--> Before checking in The Grapes of Wrath." << endl;

library.checkInBook("John Steinbeck", "The Grapes of Wrath");

cout << library; } catch (const std::exception& e) { cout << "*** " << e.what() << " ***" << endl; } cout << "--> After checking in The Grapes of Wrath." << endl << endl;

try { cout << "--> Before checking in Huckleberry Finn." << endl;

library.checkInBook("Mark Twain", "Huckleberry Finn"); cout << endl;

cout << library; } catch (const std::exception& e) { cout << "*** " << e.what() << " ***" << endl; } cout << "--> After checking in Huckleberry Finn." << endl << endl;

system("pause"); return 0; }

Dynamicarray.h

#pragma once #include namespace cs20a

{ const int INITIAL_CAPACITY = 2; const int GROWTH_FACTOR = 2; const double MINIMUM_SIZE_ALLOWED = 0.25;

template class DynamicArray { public: DynamicArray(); DynamicArray(int capacity); DynamicArray(const DynamicArray &rhs);

~DynamicArray();

void add(const T& value); void removeAt(int index); void insert(int index, const T& value); bool remove(const T& value);

int indexOf(const T& value) const; bool contains(const T& value) const;

void clear();

int getSize() const; int getCapacity() const;

bool isEmpty() const;

DynamicArray& operator =(const DynamicArray& rhs);

T& operator[](int index); const T& operator[](int index) const;

bool operator==(DynamicArray & a) const; bool operator!=(DynamicArray & a) const;

private: int size, capacity; T *elements;

void deepCopy(const DynamicArray& rhs); void setCapacity(int newCapacity); bool isCapacityAdjustmentNeeded() const; bool isIndexInRange(int index) const; };

template DynamicArray::DynamicArray() : size(0), capacity(INITIAL_CAPACITY) { elements = new T[capacity]; }

template DynamicArray::DynamicArray(int capacity) : size(0), capacity(capacity) { elements = new T[capacity]; }

template DynamicArray::DynamicArray(const DynamicArray &rhs) { deepCopy(rhs); }

template DynamicArray& DynamicArray::operator =(const DynamicArray& rhs) { if (this != &rhs) { delete[] elements; deepCopy(rhs); } return *this; }

template void DynamicArray::deepCopy(const DynamicArray& rhs) { size = rhs.size; capacity = rhs.capacity; elements = new T[capacity];

for (int i = 0; i < size; i++) elements[i] = rhs.elements[i]; }

template DynamicArray::~DynamicArray() { delete[] elements; }

template void DynamicArray::add(const T& value) { if (isCapacityAdjustmentNeeded()) setCapacity(size + 1);

T item = value; elements[size++] = item; }

template void DynamicArray::removeAt(int index) { if (!isIndexInRange(index)) throw std::out_of_range("Index out of range.");

for (int i = index; i < (size - 1); i++) elements[i] = elements[i + 1];

size--;

if (isCapacityAdjustmentNeeded()) setCapacity(size + 1); }

template bool DynamicArray::remove(const T& value) { int i = indexOf(value);

if (i >= 0) { removeAt(i); return true; } else return false; }

template void DynamicArray::insert(int index, const T& value) { if (!isIndexInRange(index)) throw std::out_of_range("Index out of range.");

if (isCapacityAdjustmentNeeded()) setCapacity(size + 1);

for (int i = size; i > index; i--) elements[i] = elements[i - 1];

elements[index] = value; size++; }

template int DynamicArray::indexOf(const T& value) const { for (int i = 0; i < size; i++) if (elements[i] == value) return i;

return -1; }

template bool DynamicArray::contains(const T& value) const { return indexOf(value) > -1; }

template int DynamicArray::getSize() const { return size; }

template int DynamicArray::getCapacity() const { return capacity; }

template bool DynamicArray::operator==(DynamicArray & rhs) const { if (this != &rhs) { if (rhs.size != size) return false;

for (int i = 0; i < size; i++) if (rhs[i] != elements[i]) return false; }

return true; }

template bool DynamicArray::operator!=(DynamicArray& rhs) const { return !(this == &rhs); }

template bool DynamicArray::isCapacityAdjustmentNeeded() const { return !((size + 1) > MINIMUM_SIZE_ALLOWED*capacity && size < capacity); }

template bool DynamicArray::isIndexInRange(int index) const { return (index >= 0 && index <= (size - 1)); }

template void DynamicArray::setCapacity(int minCapacity) { if (minCapacity < size) throw std::logic_error("Capacity must be greater than current size.");

if (minCapacity >= 0) { int limit = 1; while (limit <= minCapacity) limit *= GROWTH_FACTOR;

T *tarray = new T[limit];

for (int i = 0; i < size; i++) tarray[i] = elements[i];

delete[] elements;

elements = tarray; capacity = limit; } }

template T& DynamicArray::operator[](int index) { if (!isIndexInRange(index)) throw std::out_of_range("Index out of range.");

return elements[index]; }

template const T& DynamicArray::operator[](int index) const { if (!isIndexInRange(index)) throw std::out_of_range("Index out of range.");

return elements[index]; }

template void DynamicArray::clear() { delete[] elements;

size = 0; capacity = INITIAL_CAPACITY; elements = new T[capacity]; }

template bool DynamicArray::isEmpty() const { return (size == 0); } }

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_2

Step: 3

blur-text-image_3

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

Real Time Database Systems Architecture And Techniques

Authors: Kam-Yiu Lam ,Tei-Wei Kuo

1st Edition

1475784023, 978-1475784022

More Books

Students also viewed these Databases questions