Question
Below are the code for my workshop I just need a little help to get right output. w3p2.cpp // Workshop 3 - Templates // Created
Below are the code for my workshop I just need a little help to get right output.
w3p2.cpp
// Workshop 3 - Templates // Created by: Jeevan Pant // Date created: 2023/07/22
// Dont make any changes to this file
#include
int cout = 0; int endl = 0;
int main(int argc, char** argv) { std::cout << "Command Line arguments: "; std::cout << "--------------------------------- "; for (int i = 0; i < argc; i++) std::cout << std::setw(3) << i + 1 << ": " << *(argv + i) << ' '; std::cout << "--------------------------------- ";
// int, double, and Book data testing templated classes unsigned numBooks = 6; int ints[]{ 25, 30, 20, 15, 25, 5, 35 }; double doubles[]{ 7.8, 9.10, 3.4, 1.2, 2.3, 6.7 }; sdds::Book books[]{ sdds::Book("Software Architecture for Busy Developers", 7, 174), sdds::Book("Low-Code Application Development with Appian",16,462), sdds::Book("Natural Language Processing with TensorFlow", 11, 514), sdds::Book("Learning DevOps",17,560), sdds::Book("SQL for Data Analytics",9,540), sdds::Book("Deciphering Object-Oriented Programming with C++",21,594), };
std::cout << "1. Book tester >>> ===========================================================" << std::endl; std::cout << "total books: " << numBooks << std::endl; std::cout << "------------------------------------------------------------------------------" << std::endl; for (auto i = 0u; i < numBooks - 1; i++) { std::cout << "| " << i + 1 << "."; books[i].print(std::cout) << "|" << std::endl;; } std::cout << "| " << numBooks << "."; std::cout << books[numBooks - 1] << "|" << std::endl; std::cout << "============================================================== <<< Book tester" << std::endl << std::endl;
std::cout << "2. Collection tester for int and 10 >>> ======================================" << std::endl; { // Collection tester for int-type sdds::Collection
std::cout << "3. Collection tester for double and 10 >>>====================================" << std::endl; {// Collection tester for double-type sdds::Collection
std::cout << "4. Collection tester for Book and 10 >>> =====================================" << std::endl; {// Collection tester for Book-type, capacity 10 std::cout << "[After creating collection in empty state]" << std::endl; sdds::Collection
// Part 2 tester follows from here
std::cout << "[After adding Six books to the collection]" << std::endl; for (auto& b : books) bcol += b; std::cout << "Book with smallest pages-to-chapters ratio: " << std::endl; bcol.getSmallestItem().print(std::cout) << std::endl; std::cout << "Book with largest pages-to-chapters ratio: " << std::endl; bcol.getLargestItem().print(std::cout) << std::endl; std::cout << "size/capacity: " << bcol.size() << "/" << bcol.capacity() << std::endl; std::cout << std::endl; std::cout << "Collection content: "; bcol.print(std::cout); } std::cout << "======================================== <<< Collection tester for Book and 10" << std::endl; std::cout << std::endl;
std::cout << "5. OrderedCollection tester for int >>> ===========================================" << std::endl; { // Collection tester for int-type sdds::OrderedCollection
std::cout << "6. OrderedCollection tester for double >>> ========================================" << std::endl; {// Collection tester for double-type sdds::OrderedCollection
std::cout << "7. OrderedCollection tester for Book >>> ==========================================" << std::endl; {// OrderedCollection tester for Book-type std::cout << "[After creating collection in empty state]" << std::endl; sdds::OrderedCollection
std::cout << "[After adding six books to the collection]" << std::endl; for (auto& b : books) obcol += b; std::cout << "Book with smallest pages-to-chapters ratio: " << std::endl; obcol.getSmallestItem().print(std::cout) << std::endl; std::cout << "Book with largest pages-to-chapters ratio: " << std::endl; obcol.getLargestItem().print(std::cout) << std::endl; std::cout << "size/capacity: " << obcol.size() << "/" << obcol.capacity() << std::endl; std::cout << std::endl; std::cout << "List of OrderedCollection Books: " << std::endl; obcol.print(std::cout); } std::cout << "============================================ <<< OrderedCollection tester for Book " << std::endl; std::cout << std::endl;
return cout + endl; }
Book.h
#ifndef SDDS_BOOK_H #define SDDS_BOOK_H
#include
namespace sdds { class Book {
public: Book(); std::string m_title{}; unsigned m_numChapters{}; unsigned m_numPages{}; Book(const std::string& title, unsigned nChapters, unsigned nPages);
bool isEmpty() const; bool operator<(const Book& other) const; bool operator>(const Book& other) const;
std::ostream& print(std::ostream& os) const;
std::string getTitle() const { return m_title;
}
// Public mutator to set the title void setTitle(const std::string& title) { m_title = title; } };
std::ostream& operator<<(std::ostream& os, const Book& bk); }
#endif // SDDS_BOOK_H
Book.cpp
#include "Book.h" #include
namespace sdds { Book::Book() : m_title(""), m_numChapters(0), m_numPages(0) {}
Book::Book(const std::string& title, unsigned nChapters, unsigned nPages) : m_title(title), m_numChapters(nChapters), m_numPages(nPages) {}
bool Book::isEmpty() const { return m_title.empty() || m_numChapters == 0 || m_numPages == 0; }
bool Book::operator<(const Book& other) const { // Compare based on the average pages per chapter return (static_cast
bool Book::operator>(const Book& other) const { // Compare based on the average pages per chapter return (static_cast
std::ostream& Book::print(std::ostream& os) const { if (!isEmpty()) { std::string leftColumn; leftColumn += m_title; leftColumn += ","; leftColumn += std::to_string(m_numChapters); leftColumn += ","; leftColumn += std::to_string(m_numPages);
/*std::string rightColumn; rightColumn += */
os << std::setw(56) << std::right << leftColumn << " | (" << std::to_string(static_cast
std::ostream& operator<<(std::ostream& os, const Book& bk) { return bk.print(os); } }
Collection.h (contains both .h and .cpp codes)
#ifndef SDDS_COLLECTION_H #define SDDS_COLLECTION_H
#include
namespace sdds { template
static T m_smallestItem; static T m_largestItem;
protected: T m_items[C]{}; void setSmallestItem(const T& item); void setLargestItem(const T& item); T& operator[](size_t index); // Mutator operation void incrSize(); // Mutator operation
public: Collection(); size_t size() const; unsigned capacity() const; bool operator+=(const T& item); static T getSmallestItem(); static T getLargestItem(); std::ostream& print(std::ostream& os) const; //void print(std::ostream& os) const;
std::ostream& printBookData(std::ostream& os, const Book& book) const; }; /* template
template <> Book Collection
template <> Book Collection
/*template
template <> Book Collection
template <> Book Collection
template
template
template
template
template
template
template
template
template
template
template
template
template
}
#endif // SDDS_COLLECTION_H
OrderedCollection.h (contains both .h and .cpp codes)
#ifndef SDDS_ORDERED_COLLECTION_H #define SDDS_ORDERED_COLLECTION_H
#include "Collection.h" namespace sdds { template
template
// Shift elements to make space for the new item for (size_t i = this->size(); i > insertIndex; i--) { this->m_items[i] = this->m_items[i - 1]; }
// Insert the new item this->m_items[insertIndex] = item;
// Update the size this->incrSize();
// Update smallest and largest items if (this->size() == 1 || item < this->getSmallestItem()) { this->setSmallestItem(item); } if (this->size() == 1 || item > this->getLargestItem()) { this->setLargestItem(item); }
return true; }
return false; }
// Explicit instantiation for OrderedCollection with Book type (if needed) template class OrderedCollection
#endif //SDDS_ORDERED
Sample output:
Command Line arguments: --------------------------------- 1: ws ---------------------------------
1. Book tester >>> =========================================================== total books: 6 ------------------------------------------------------------------------------ | 1. Software Architecture for Busy Developers,7,174 | (24.857143) | | 2. Low-Code Application Development with Appian,16,462 | (28.875000) | | 3. Natural Language Processing with TensorFlow,11,514 | (46.727273) | | 4. Learning DevOps,17,560 | (32.941176) | | 5. SQL for Data Analytics,9,540 | (60.000000) | | 6. Deciphering Object-Oriented Programming with C++,21,594 | (28.285714) | ============================================================== <<< Book tester
2. Collection tester for int and 10 >>> ====================================== (smallest,largest) items: (5,35) size/capacity: 7/10 Contents: [25,30,20,15,25,5,35] ========================================= <<< Collection tester for int and 10
3. Collection tester for double and 10 >>>==================================== (smallest,largest) items: (1.2,9.1) size/capacity: 6/10 Contents: [7.8,9.1,3.4,1.2,2.3,6.7] ====================================== <<< Collection tester for double and 10
4. Collection tester for Book and 10 >>> ===================================== [After creating collection in empty state] Book with smallest pages-to-chapters ratio (initial-default state): | Invalid book data | Book with largest pages-to-chapters ratio (initial-deault-state): | Invalid book data | size/capacity: 0/10
[After adding Six books to the collection] Book with smallest pages-to-chapters ratio: Software Architecture for Busy Developers,7,174 | (24.857143) Book with largest pages-to-chapters ratio: SQL for Data Analytics,9,540 | (60.000000) size/capacity: 6/10
Collection content: | ---------------------------------------------------------------------------| | Software Architecture for Busy Developers,7,174 | (24.857143) | | Low-Code Application Development with Appian,16,462 | (28.875000) | | Natural Language Processing with TensorFlow,11,514 | (46.727273) | | Learning DevOps,17,560 | (32.941176) | | SQL for Data Analytics,9,540 | (60.000000) | | Deciphering Object-Oriented Programming with C++,21,594 | (28.285714) | | ---------------------------------------------------------------------------| ======================================== <<< Collection tester for Book and 10
5. OrderedCollection tester for int >>> =========================================== (smallest,largest) items: (5,35) size/capacity: 7/72 Contents: [5,15,20,25,25,30,35] ============================================= <<< OrderedCollection tester for int
6. OrderedCollection tester for double >>> ======================================== (smallest,largest) items: (1.2,9.1) size/capacity: 6/72 Contents: [1.2,2.3,3.4,6.7,7.8,9.1] ========================================== <<< OrderedCollection tester for double
7. OrderedCollection tester for Book >>> ========================================== [After creating collection in empty state] Book with smallest pages-to-chapters ratio (initial-default state): | Invalid book data Book with largest pages-to-chapters ratio (initial-deault-state): | Invalid book data size/capacity: 0/72
[After adding six books to the collection] Book with smallest pages-to-chapters ratio: Software Architecture for Busy Developers,7,174 | (24.857143) Book with largest pages-to-chapters ratio: SQL for Data Analytics,9,540 | (60.000000) size/capacity: 6/72
List of OrderedCollection Books: | ---------------------------------------------------------------------------| | Software Architecture for Busy Developers,7,174 | (24.857143) | | Deciphering Object-Oriented Programming with C++,21,594 | (28.285714) | | Low-Code Application Development with Appian,16,462 | (28.875000) | | Learning DevOps,17,560 | (32.941176) | | Natural Language Processing with TensorFlow,11,514 | (46.727273) | | SQL for Data Analytics,9,540 | (60.000000) | | ---------------------------------------------------------------------------| ============================================ <<< OrderedCollection tester for Book
I just need code for this part with *this* in collection module:
Collection Module
Template Specializations
specialize the initializations of m_smallestItem and m_largestItem objects: when T = Book and C = 72, the values stored {"", 1, 10000} and {"", 10000, 1}, respectively.
*this* specialize the print() function when T = Book and C = 10 to print Book object's data differently, on multiple rows; see sample output for a hint on exact formatting of output.
*this* specialize the print() function when T = Book and C = 72 to print data in the same way as the previous specialization.
No other changes are necessary to this module.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started