Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Part 1 - LAB (100%) Look at the code provided in the Collection template module, study it and understand it. The Collection class is a

Part 1 - LAB (100%)

Look at the code provided in the Collection template module, study it and understand it. The Collection class is a template that works like a dynamic array of objects and resizes itself as objects are added to it. The code snippet below demonstrates how Collection works:

Collection dCol;

dCol.add(4.23);

dCol.add(5.34);

dCol.add(6.45);

cout << "We have " << dCol.size() << " items in the Collection!" << endl;

for(int i=0; i< dCol.size();i++){

cout << dCol[i] << endl;

}

Output:

We have 3 items in the Collection!

4.23

5.34

6.45

Supplied Modules:

  • Collection
  • Write
  • Book
  • Pet
  • Card

Do not modify these modules! Look at the code and make sure you understand them.

Modules that you will modify (The templateFunctions module and the main module)

templateFunctions module

Implement this module in 'templateFunctions.h' header file.

  • "find": checks if a key value matches an array item at a provided index
  • "find": takes 2 incoming keys of different types and searches through an array to find the first match.
  • "operator <<": inserts a collection into the ostream
  • "loadCollection": takes an object and adds it to a collection of the same type

find 3 argument function template

Make a function template called find to check if a key value matches an array item at a provided index Arguments:

  • An array of templated objects; the same type as the Collection type. (template type 1)
  • An integer variable that represents the index to use in the comparison
  • A key templated value to search for, in the array of objects. (template type 2)

The find function is rather simple and will return true or false based on the following condition:

  • if the element at index i of the array equals the key or not Use the "==" operator to check for a match between the objects and the key. This operator is overloaded in each testing class used.

find 4 argument function template

Make a function template called find that accepts four arguments in any order you prefer:

  • An array of templated objects; the same type as the Collection type. (template type 1)
  • An integer represents the number of elements in the array of objects.
  • A key templated value as one of our search parameters. (template type 2)
  • Another key templated value as our second search parameter. (template type 3)

The find function template returns an int that represents the index of a found item that matches the 2 search keys, and returns -1 if no successful matches were found. The find function goes through all the elements of the array of objects checks if the keys are comparable to the array object (use the overloaded comparison operator).

Insertion operator(<<) template

Lists all the elements of an array.

The insertion operator takes a reference to an ostream object and a collection template as parameters. It will then iterate over every item in the collection and insert it and an endl into the ostream parameter.

loadCollection function template

This function takes a reference to a collection (of template type 1) and an object of the same type. It will then call the overloaded += operator in the collections class to add the new item.

This function returns a constant collection reference.

Template type requirements

Each function and overloaded operator that uses templates require some code that is already provided to you in order to work properly. Add a comment to each of the functions/operator you just created listing what code is needed/required to allow those functions to work. Then also copy these comments to the part 2 reflection of the workshop.

The main module

In this workshop you are modifying the main module. Make sure to update the comments at the top to reflect your work on the module

Modify the main module and call the find function with 3 arguments at all spots with the comment labled //TODO 1:

See the comments in main.cpp on where to use this function

Modify the main module and call the find function with 4 arguments at all spots with the comment labled //TODO 2:

See the comments in main.cpp on where to use this function

Modify the main module and call the insertion << operator at all spots with the comment labled //TODO 3:

See the comments in main.cpp on where to use this function

Modify the main module and call the loadCollection function at all spots with the comment labled //TODO 4:

See the comments in main.cpp on where to use this function

output

Printing all the Cards:

King of Hearts

7 of Clubs

5 of Spades

Jack of Diamonds

10 of Hearts

Ace of Spades

Queen of Diamonds

Searching for King of Hearts, Jack of Diamonds and 10 of Spades cards:

Ace of Spades

Jack of Diamonds

King of Hearts

10 of Hearts

(34567 IncorrectTitle), is not in our collection of books

Our results of our Book search: (Dune, IncorrectTitle, Frankenstein, Foundation)

23456 Dune (Author: Frank Herbert) Price: $9.99

67890 Frankenstein (Author: Mary Shelly) Price: $4.99

45678 Foundation (Author: Issac Asimov) Price: $19.99

All the books listed in our collection:

23456 Dune (Author: Frank Herbert) Price: $9.99

45678 Foundation (Author: Issac Asimov) Price: $19.99

56789 The Lord of the Rings (Author: J.R.R. Tolkien) Price: $7.99

67890 Frankenstein (Author: Mary Shelly) Price: $4.99

34567 The Martian (Author: Andy Weir) Price: $10.50

12345 The Hitchhiker's Guide to the Galaxy (Author: Douglas Adams) Price: $12.99

All the Pets listed in our collection:

Type: Rat, Name: Carl Age: 3

Type: Rabbit, Name: Mindy Age: 2

Type: Weasel, Name: Lenny Age: 4

Type: Dog, Name: Waylon Age: 7

Type: Cat, Name: Frank Age: 12

Type: Dog, Name: Billy Joel Age: 13

Our results of our Pet Query: (Dog)

Type: Dog, Name: Waylon Age: 7

Type: Dog, Name: Billy Joel Age: 13

Our results of int Filter:

90

90

Files to submit:

All the files are needed for compilation but only the first two files will be submitted to your professor.

main.cpp

templateFunctions.h

Book.cpp

Book.h

Card.cpp

Card.h

Collection.h

Pet.cpp

Pet.h

Write.cpp

Write.h

Book.cpp

// Workshop 10:

// Book.cpp

// 2022-02-25

// Version: 1.0

// Author: Nathan Misener

// Revised by:

/////////////////////////////////////////////

#define _CRT_SECURE_NO_WARNINGS

#include

#include

#include

using namespace std;

#include "Book.h"

namespace sdds {

Book::Book() {

set(0, "", "", 0.0);

}

Book::Book(int upc, const char* title, const char* author, double price) {

set(upc, title, author, price);

}

void Book::set(int upc, const char* title, const char* author, double price) {

m_upc = upc;

strcpy(m_title, title);

strcpy(m_author, author);

m_price = price;

}

ostream& Book::display(ostream& os)const {

return os << m_upc << " " << m_title << " (Author: " << m_author << ") Price: $" << setprecision(2) << fixed << m_price;

}

bool Book::operator==(int upc)const {

return m_upc == upc;

}

bool Book::operator==(const char* title)const {

return strcmp(m_title,title)==0;

}

std::ostream& operator<<(std::ostream& os,const Book& book) {

book.display(os);

return os;

}

}

Book.h

// Workshop 10:

// Collection template

// 2022-02-25

// Version: 1.0

// Author: Nathan Misener

// Revised by:

/////////////////////////////////////////////

#include

#ifndef SDDS_BOOK_H_

#define SDDS_BOOK_H_

#include "Write.h"

namespace sdds {

class Book : public ReadWrite {

int m_upc;

char m_title[41];

char m_author[41];

double m_price;

public:

Book();

Book(int upc, const char* title, const char* author, double price);

void set(int upc, const char* title, const char* author, double price);

std::ostream& display(std::ostream& os)const;

bool operator==(int upc)const;

bool operator==(const char * title)const;

};

std::ostream& operator<<(std::ostream& os,const Book& book);

}

#endif // !SDDS_BOOK_H_

Card.cpp

// Workshop 10:

// Card.cpp

// 2022-02-25

// Version: 1.0

// Author: Nathan Misener

// Revised by:

/////////////////////////////////////////////

#define _CRT_SECURE_NO_WARNINGS

#include

#include

#include

using namespace std;

#include "Card.h"

using namespace std;

namespace sdds {

Card::Card(const char* suit, const int value) {

set(suit, value);

}

void Card::set(const char* suit, const int value) {

strncpy(m_suit, suit, 10);

m_suit[8] = 0;

m_value = value;

}

std::ostream& Card::display(std::ostream& os)const {

char value[7];

switch (m_value){

case 1:

strncpy(value, "Ace", 4);

break;

case 11:

strncpy(value, "Jack", 5);

break;

case 12:

strncpy(value, "Queen", 6);

break;

case 13:

strncpy(value, "King", 5);

break;

default:

strncpy(value, std::to_string(m_value).c_str(), 3);

}

return os << value << " of " << m_suit;

}

bool Card::operator==(const char* mmStr)const {

return strcmp(m_suit, mmStr) == 0;

}

bool Card::operator==(const int mmVal)const {

return m_value == mmVal;

}

std::ostream& operator<<(std::ostream& os, const Card& card) {

card.display(os);

return os;

}

}

Card.h

// Workshop 10:

// Card.h

// 2022-02-19

// Version: 1.0

// Author: Nathan Misener

// Revised by:

/////////////////////////////////////////////

#include

#ifndef SDDS_CARD_H_

#define SDDS_CARD_H_

#include "Write.h"

namespace sdds {

class Card : public ReadWrite {

char m_suit[10];

int m_value;

public:

Card(const char* suit = "", const int value= 0);

void set(const char* suit, const int value);

std::ostream& display(std::ostream& os)const;

bool operator==(const char* mmSubstr)const;

bool operator==(const int mmVal)const;

};

std::ostream& operator<<(std::ostream& os, const Card& card);

}

#endif // !SDDS_CARD_H_

Collection.h

// Workshop 10:

// Collection.h

// 2022-02-25

// Version: 1.0

// Author: Nathan Misener

// Revised by:

/////////////////////////////////////////////

#ifndef SDDS_COLLECTION_H_

#define SDDS_COLLECTION_H_

namespace sdds {

template

class Collection {

T* m_data = nullptr;

int m_size = 0;

public:

Collection(int size = 0);

Collection(const Collection& CP);

Collection& operator=(const Collection& RO);

int size()const;

void resize(int newsize);

T& operator[](int index);

Collection& operator+=(const T& element);

~Collection();

};

template

Collection::Collection(int size) :m_size(size) {

if (m_size <= 0) m_size = 0;

if (m_size > 0) m_data = new T[m_size];

}

template

Collection::Collection(const Collection& CP) {

operator=(CP);

}

template

Collection& Collection::operator=(const Collection& RO) {

if (this != &RO) {

delete[] m_data;

m_data = new T[m_size = RO.m_size];

for (int i = 0; i < m_size; i++) m_data[i] = RO.m_data[i];

}

return *this;

}

template

int Collection::size()const {

return m_size;

}

template

void Collection::resize(int newsize) {

int i;

T* temp = nullptr;

if (newsize < 0) newsize = 0;

if (newsize > 0) {

temp = new T[newsize];

for (i = 0; i < m_size && i < newsize; i++) {

temp[i] = m_data[i];

}

}

delete[] m_data;

m_data = temp;

m_size = newsize;

}

template

T& Collection::operator[](int index) {

if (index >= m_size) resize(index + 1);

return m_data[index];

}

template

Collection& Collection::operator+=(const T& element) {

(*this)[size()] = element;

return *this;

}

template

Collection::~Collection() {

delete[] m_data;

}

}

#endif // !SDDS_COLLECTION_H_

Pet.cpp

// Workshop 10:

// Pet.cpp

// 2022-02-25

// Version: 1.0

// Author: Nathan Misener

// Revised by:

/////////////////////////////////////////////

#define _CRT_SECURE_NO_WARNINGS

#include

#include

#include

using namespace std;

using namespace std;

#include "Pet.h"

namespace sdds {

Pet::Pet() {

set("", "", 0);

}

Pet::Pet(const char* name, const char* type, int age) {

set(name, type, age);

}

void Pet::set(const char* name, const char* type, int age) {

strcpy(m_name, name);

strcpy(m_type, type);

m_age = age;

}

ostream& Pet::display(ostream& os)const {

return os <<"Type: " << m_type << ", Name: " << m_name << " Age: " << m_age;

}

bool Pet::operator==(int age)const {

return m_age == age;

}

bool Pet::operator==(const char* type)const {

return strcmp(m_type, type) == 0;

}

std::ostream& operator<<(std::ostream& os,const Pet& pet) {

pet.display(os);

return os;

}

}

Pet.h

// Workshop 10:

// Pet.h

// 2022-02-25

// Version: 1.0

// Author: Nathan Misener

// Revised by:

/////////////////////////////////////////////

#include

#ifndef SDDS_PET_H_

#define SDDS_PET_H_

#include "Write.h"

namespace sdds {

class Pet : public ReadWrite {

char m_name[41];

char m_type[41];

int m_age;

public:

Pet();

Pet(const char* name, const char* type, int age);

void set(const char* name, const char* type, int age);

std::ostream& display(std::ostream& os)const;

bool operator==(const char* type)const;

bool operator==(const int age)const;

};

std::ostream& operator<<(std::ostream& os, const Pet& Pet);

}

#endif // !SDDS_PET_H_

Write.cpp

// Workshop 10:

// Write.cpp

// 2022-02-25

// Version: 1.0

// Author: Nathan Misener

// Revised by:

/////////////////////////////////////////////

#include

#include "Write.h"

using namespace std;

namespace sdds {

ostream& operator<<(ostream& os, const ReadWrite& c) {

return c.display(os);

}

}

Write.h

// Workshop 10:

// Write.h

// 2022-02-25

// Version: 1.0

// Author: Nathan Misener

// Revised by:

/////////////////////////////////////////////

#include

#ifndef SDDS_READWRITE_H_

#define SDDS_READWRITE_H_

namespace sdds {

class ReadWrite {

public:

virtual std::ostream& display(std::ostream& os)const = 0;

virtual ~ReadWrite() {

}

};

std::ostream& operator<<(std::ostream& os, const ReadWrite& c);

}

#endif // !SDDS_READWRITE_H_

main.cpp

// Workshop 10:

// Collection template

// 2022-02-25

// Version: 1.0

// Author: Nathan Misener

// Revised by:

/////////////////////////////////////////////

#include

#include "Card.h"

#include "Book.h"

#include "Pet.h"

#include "Collection.h"

#include "templateFunctions.h"

using namespace std;

using namespace sdds;

int main()

{

Card C[7] = { Card("Hearts", 13), Card("Clubs", 7), Card("Spades", 5),

Card("Diamonds", 11),Card("Hearts", 10), Card("Spades", 1),

Card("Diamonds", 12) };

Book B[6] = { Book(23456, "Dune","Frank Herbert",9.99),Book(45678, "Foundation", "Issac Asimov", 19.99), Book(56789, "The Lord of the Rings","J.R.R. Tolkien", 7.99),

Book(67890, "Frankenstein", "Mary Shelly", 4.99), Book(34567, "The Martian", "Andy Weir",10.50),Book(12345, "The Hitchhiker's Guide to the Galaxy", "Douglas Adams",12.99)};

Pet P[6] = { Pet("Carl", "Rat", 3), Pet("Mindy", "Rabbit", 2),

Pet("Lenny", "Weasel",4), Pet("Waylon", "Dog", 7),

Pet("Frank", "Cat", 12), Pet("Billy Joel","Dog",13)};

//these collections will hold the copies of the arrays for printing

Collection cCol1;

Collection bCol;

Collection pCol;

//these collections will hold the results of the finds

Collection cCol2;

Collection bCol2;

Collection pCol2;

Collection iCol;

int ii, index;

// Loading up the collections of Cards

for (ii = 0; ii < 7; ii++) {

//Load the collection of cards into cCol1

loadCollection(_________);//****TODO 4:

}

// Loading up the collections of Books

for (ii = 0; ii < 6; ii++) {

//Load the collection of cards into bCol

loadCollection(__________); //****TODO 4:

}

// Loading up the collections of Pets

for (ii = 0; ii < 6; ii++) {

//Load the collection of cards into pCol

loadCollection(_________); //****TODO 4:

}

//Printing out all the Cards using the ostream insertion operator

cout << "Printing all the Cards:" << endl;

cout _________; //****TODO 3:

cout << endl;

cout << "Searching for King of Hearts, Jack of Diamonds and 10 of Spades cards: " << endl;

// Call the search method passing in the value and Suit (remember 1 = ace, 11 = jack, 12 = queen, 13 = king)

int valuesToSearch[] = { 1, 11, 10 };

const char* suits[] = { "Spades", "Diamonds", "Spades" };

for (ii = 0; ii < 3; ii++) {

//call the 4 argument find function with values from the 2 above arrays

index = find(_________); //****TODO 2:

if (index >= 0) {

//add the found object at the corresponding index to cCol2. (Review the Collection code for the correct operator)

loadCollection(_________); //****TODO 4:

}

}

//Search for all "Hearts" cards

for (ii = 0; ii < 7; ii++) {

if (find(_________)) { //****TODO 1:

loadCollection(_________); //****TODO 4:

}

}

//Printing cards found in our searches

cout _________; //****TODO 3:

// Call the find function to search the array B

// and store the matches in bCol2 passing the following parallel array arguments as the key

int upcToSearch[] = { 23456, 34567, 67890, 45678};

const char* titlesToSearch[] = { "Dune", "IncorrectTitle", "Frankenstein", "Foundation"};

for (ii = 0; ii < 4; ii++) {

//call the 4 argument find function with values from the 2 above arrays

index = find(_________); //****TODO 2:

if (index >= 0) {

//add the found object at the corresponding index to cCol2. (Review the Collection code for the correct operator)

loadCollection(_________); //****TODO 4:

}

else {

cout << endl << "(" << upcToSearch[ii] << " " << titlesToSearch[ii] << "), is not in our collection of books" << endl << endl;

}

}

cout << "Our results of our Book search: (Dune, IncorrectTitle, Frankenstein, Foundation)" << endl;

//print out our collection of searched books

cout _________; //****TODO 3:

cout << endl << "All the books listed in our collection: " << endl;

//print out our collection of all books

cout _________; //****TODO 3:

// Call the find function to search the array P

// and store the matches in pCol2 passing the key "Dog" into your find function

for (ii = 0; ii < 6; ii++) {

if (find(_________)) { //****TODO 1:

loadCollection(_________); //****TODO 4:

}

}

cout << endl << "All the Pets listed in our collection: " << endl;

//print out our collection of all pet

cout _________; //****TODO 3:

cout << endl <<"Our results of our Pet Query: (Dog)" << endl;

//print out our collection of filtered pets

cout _________; //****TODO 3:

int a[]{90,80,90,70,60,30 };

// Call the find functio to find all elements that match the value 90

// Load the results into a collection iCol

// Insert the iCol into the cout stream

for (ii = 0; ii < 6; ii++) {

if (find(_________)) { //****TODO 1:

loadCollection(_________); //****TODO 4:

}

}

cout << endl << "Our results of int Filter:" << endl;

cout _________; //****TODO 3:

return 0;

}

templateFunctions.h

//Name:

//ID:

//Email:

// Date:

//Section:

#ifndef SDDS_TEMPLATEFUNCTIONS_H_

#define SDDS_TEMPLATEFUNCTIONS_H_

#include

#include "Collection.h"

namespace sdds {

//Find with 3 parameters

//Find with 4 parameters

//Insertion operator

//Load Collection

}

#endif // !SDDS_SEARCH_H_

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

Introduction to Wireless and Mobile Systems

Authors: Dharma P. Agrawal, Qing An Zeng

4th edition

1305087135, 978-1305087132, 9781305259621, 1305259629, 9781305537910 , 978-130508713

More Books

Students also viewed these Programming questions