Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

PLEASE CODE IN C++ AND ALL THE FILES ARE FROM BASE CODE. Book.cc #include #include using namespace std; #include Book.h Book::Book(int i, string t, string

image text in transcribed

image text in transcribed

PLEASE CODE IN C++ AND ALL THE FILES ARE FROM BASE CODE.

Book.cc

#include #include using namespace std;

#include "Book.h"

Book::Book(int i, string t, string a, int y) { id = i; title = t; author = a; year = y; }

void Book::setBook(int i, string t, string a, int y) { id = i; title = t; author = a; year = y; }

void Book::print() { cout

Book.h

#ifndef BOOK_H #define BOOK_H

#include using namespace std;

class Book { public: Book(int=0, string="Unknown", string="Unknown", int=0); void setBook(int, string, string, int); void print();

private: int id; string title; string author; int year; };

#endif

in.txt

1 101 Ender's Game Card, Orson Scott 1985 1 102 Dune Herbert, Frank 1965 1 110 Foundation Asimov, Isaac 1951 1 111 Hitch Hiker's Guide to the Galaxy Adams, Douglas 1979 1 112 1984 Orwell, George 1949 1 113 Stranger in a Strange Land Heinlein, Robert A. 1961 1 114 Farenheit 451 Bradbury, Ray 1954 1 115 2001: A Space Odyssey Clarke, Arthur C. 1968 1 116 I, Robot Asimov, Isaac 1950 1 117 Starship Troopers Heinlein, Robert A. 1959 1 118 Do Androids Dream of Electric Sheep? Dick, Philip K. 1968 1 119 Neuromancer Gibson, William 1984 1 120 Ringworld Niven, Larry 1970 1 121 Rendezvous with Rama Clarke, Arthur C. 1973 1 122 Hyperion Simmons, Dan 1989 0

Main.cc

#include using namespace std; #include

#include "Book.h"

#define MAX_ARR_SIZE 128

int mainMenu(); void printLibrary(Book arr[MAX_ARR_SIZE], int num);

int main() { Book library[MAX_ARR_SIZE]; int numBooks = 0; string title, author; int id, year; int menuSelection;

while (1) { menuSelection = mainMenu();

if (menuSelection == 0) break; else if (menuSelection == 1) { cout > id; cout > year;

library[numBooks].setBook(id, title, author, year); ++numBooks; } }

if (numBooks > 0) printLibrary(library, numBooks);

return 0; }

int mainMenu() { int numOptions = 1; int selection = -1;

cout

while (selection numOptions) { cout > selection; }

return selection; }

void printLibrary(Book arr[MAX_ARR_SIZE], int num) { cout

for (int i=0; i

cout

Makefile

OPT = -Wall

t01: main.o Book.o g++ $(OPT) -o t01 main.o Book.o

main.o: main.cc Book.h g++ $(OPT) -c main.cc

Book.o: Book.cc Book.h g++ $(OPT) -c Book.cc

clean: rm -f *.o t01

Instructions 1. You will begin with the code you saved from previous tutorials, or with the base code 2. You will change the main) function to dynamically allocate each new Book object when the user enters the information for a book. 3. If you are starting from a previous tutorial, make the following changes to the Array class: books will be stored in an array of Book pointers, instead of Book objects you will need to update the library printing function to work with this change o .change the Array class's add ) function as follows: o it will take a Book pointer as parameter it l insert each new book into the array so that it stays in ascending order by year; this will require shifting some books towards the back of the array to make room for a new one terminology: ascending means increasing: descending means decreasing it will compare book years using a new lessThan function in the Book class .add a destructor to the Array class to clean up the dynamically allocated Book objects 4. If you are starting from the base code modify the primitive array declared in main) so that the books are stored in a primitive array of Book pointers, instead of Book objects o you will need to modify the library printing function to work with this change write a global addBook function that takes the primitive array and the new book pointer as parameters and does the following o it will insert each new book into the array so that it stays in ascending order by year; this will require shifting some books towards the back of the array to make room for a new one . terminology: ascending means increasing: descending means decreasing it will compare book years using a new lessThan ) function in the Book class write a global cleanup ) function to deallocate the dynamically allocated memory 5. If you are starting from a previous tutorial, modify the Library class so that the addBook function takes a Book pointer as parameter 6. Add a new lessThan ) function to the Book class. This function will take a Book pointer as parameter and return a boolean. It will return true if the year of the Book on which the function is called is less than the year of the Book parameter 7. Build and run the program. at the end of the program. Check that the books are ordered correctly when the library is printed out 8. Make sure that all dynamically allocated memory is explicitly deallocated when it is no longer used. Use valgrind to check for memory leaks, as we saw in class when we covered section 2.3. 9. Package together the tutorial code into a tar file. Start up a browser in the VM, log into culearn, and go to the tutorial page. Select the tutorial submission link, and upload your new tar file. 10. Save your work to a permanent location, like a memory stick or your Z-drive

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

Step: 3

blur-text-image

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

More Books

Students also viewed these Databases questions

Question

Create an evidence-based recruitment strategy.

Answered: 1 week ago

Question

8. Explain the contact hypothesis.

Answered: 1 week ago

Question

2. Define the grand narrative.

Answered: 1 week ago