Question

please Code in c++ Create a new Library class. You will need both a header file...

please Code in c++

  1. Create a new Library class. You will need both a header file and a source file for this class. The class will contain two data members:
    • an array of Book objects
    • the current number of books in the array
      Since the book array is moving from the main.cc file, you will also move the constant array size definition (MAX_ARR_SIZE) into the Library header file.
  2. Write the following functions for the Library class:
    • a constructor that initializes the data member(s) that require initialization; think about what these
      might be
    • an addBook(Book&) function that adds the given book parameter to the back of the book array
      ◦ terminology: the back of a collection is its end; the front of a collection is its beginning
    • a print() function that prints out all the books in the array to the screen
  3. Change the program so that the main() function:
    • doesn’t declare a book array anymore; instead, it will declare a Library object
    • uses the Library object and its functions, instead of manipulating the book array directly
    • creates temporary Book objects to be added to the library

◦ the Book class’s setBook() function should no longer be used and should be removed • adds the new book to the library using functions implemented in step #3

  1. Update the Makefile so that the new Library class gets compiled and linked into the executable, as we saw in the course material section on Makefiles.
  2. Build and run the program. Check that the book information is correct when the library is printed out at the end of the program.

Book.h

#define BOOK_H

#define BOOK_H

#include <string>

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

Book.cc

#include <iostream>

#include <iomanip>

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 << setw(3) << id

   <<" Title: " << setw(40) << title

   <<"; Author: " << setw(20) << author

   <<"; Year: " << year << endl;

}

main.cc

#include <iostream>

using namespace std;

#include <string>

#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: ";

cin >> id;

cout << "title: ";

cin.ignore();

getline(cin, title);

cout << "author: ";

getline(cin, author);

cout << "year: ";

cin >> 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 << endl;

cout << "(1) Add book" << endl;

cout << "(0) Exit" << endl;

while (selection < 0 || selection > numOptions) {

cout << "Enter your selection: ";

cin >> selection;

}

return selection;

}

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

{

cout << endl << endl << "LIBRARY: " << endl;

for (int i=0; i<num; ++i)

arr[i].print();

cout << endl;

}

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

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

0 0
Add a comment Improve this question Transcribed image text
Answer #1

// I hope this will help you.. Please feel free to ask in case of any doubts

//Create the header file Book.h save it with that name below is header file and Book.cpp and main.cpp //file each file save with separate in one folder. Below is the all files.
//In header file only method declaration and variable declarations.

Book.h

#ifndef BOOK_H
#define BOOK_H

#include <string>
using namespace std;

class Book // Book class
{
public:
Book(int=0, string="Unknown",string="Unknown", int=0); // Book constructor
void setBook(int, string, string, int); //setBook method will set the bookid, title, author, year
void print(); // prints the book info added

private:
// Declare the id, title, author year
int id;
string title;
string author;
int year;
};

#endif

~  

//Book.cpp file will implement the method which in declare in Book.h header file

Book.cpp

#include <iostream>
#include <iomanip>
using namespace std;

#include "Book.h"

Book::Book(int i, string t, string a, int y) //Constructor will default set the book details which is id,title,author, year
{
id = i;
title = t;
author = a;
year = y;
}

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

main.cpp

#include <iostream>
using namespace std;
#include <string>

#include "Book.h"

#define MAX_ARR_SIZE 128 //set the size

int mainMenu(); //method declare mainMenu
void printLibrary(Book arr[MAX_ARR_SIZE], int num); // printLibrary method will take the array of book and num


int main() // main method
{
Book library[MAX_ARR_SIZE];// create a object of Book class
int numBooks = 0; //declare the numBook and set to 0
string title, author; // declare a title,year variable
int id, year; // declare id,year
int menuSelection; //variable menuSelection

while (1) { // run the loop continuously until user select 0 means exit
menuSelection = mainMenu(); // initialize the mainMenu() value into menuSelection

//if menu selection has 0 the loop will terminate
if (menuSelection == 0)
break;
else if (menuSelection == 1) { //menuSelection has 1 the if condition will execute and take the book info input from the user input
cout << "id: "; // input id
cin >> id;
cout << "title: "; //input book title
cin.ignore();
getline(cin, title);
cout << "author: "; //input author of book
getline(cin, author);
cout << "year: "; // input year
cin >> year;

library[numBooks].setBook(id, title, author, year); // call the setBook() method to set the book info in library array object
++numBooks; increase the nuBooks by one
}
}

if (numBooks > 0) // if numBooks has greater than 0 the prints the book info by using printLibrary method
printLibrary(library, numBooks);

return 0;
}

int mainMenu() // main menu method
{
int numOptions = 1; // declare the numOptuons and set to 1
int selection = -1; // declare selection and set to -1

cout << endl;//new line
//Prints the menu selection
cout << "(1) Add book" << endl;
cout << "(0) Exit" << endl;

while (selection < 0 || selection > numOptions) { // check if the selection is less than 0 or selection is greater than numOption
cout << "Enter your selection: "; // ask the user to enter selection menu
cin >> selection;
}
//When user enter the selection option it will return to the method
return selection;
}


//printLibrary method will take the Book array object and num to prints the entered book info using for loop one by one
void printLibrary(Book arr[MAX_ARR_SIZE], int num)
{
cout << endl << endl << "LIBRARY: " << endl;

for (int i=0; i<num; ++i)
arr[i].print();

cout << endl;
}

Add a comment
Know the answer?
Add Answer to:
please Code in c++ Create a new Library class. You will need both a header file...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • A library maintains a collection of books. Books can be added to and deleted from and...

    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...

  • fully comments for my program, thank you will thumb up #include <iostream> #include <fstream> #include <string>...

    fully comments for my program, thank you will thumb up #include <iostream> #include <fstream> #include <string> #include <iomanip> using namespace std; struct book { int ISBN; string Author; string Title; string publisher; int Quantity; double price; }; void choice1(book books[], int& size, int MAX_SIZE) { ifstream inFile; inFile.open("inventory.txt"); if (inFile.fail()) cout <<"file could not open"<<endl; string str;    while(inFile && size < MAX_SIZE) { getline(inFile, str); books[size].ISBN = atoi(str.c_str()); getline(inFile, books[size].Title);    getline(inFile, books[size].Author); getline(inFile, books[size].publisher);          getline(inFile,...

  • Objectives: The main objective of this assignment is checking students’ ability to implement membership functions. After...

    Objectives: The main objective of this assignment is checking students’ ability to implement membership functions. After completing this assignment, students will be able to:  implement member functions  convert a member function into a standalone function  convert a standalone function into a member function  call member functions  implement constructors  use structs for function overloading Problem description: In this assignment, we will revisit Assignment #1. Mary has now created a small commercial library and has managed...

  • using the source code at the bottom of this page, use the following instructions to make...

    using the source code at the bottom of this page, use the following instructions to make the appropriate modifications to the source code. Serendipity Booksellers Software Development Project— Part 7: A Problem-Solving Exercise For this chapter’s assignment, you are to add a series of arrays to the program. For the time being, these arrays will be used to hold the data in the inventory database. The functions that allow the user to add, change, and delete books in the store’s...

  • The following C++ code include 3 files: Patient.h, Patient.cpp and Main.cpp. The program basically creates and stores patient records. The original code has everything in a single .cpp file. I tried t...

    The following C++ code include 3 files: Patient.h, Patient.cpp and Main.cpp. The program basically creates and stores patient records. The original code has everything in a single .cpp file. I tried to divide the code in 3 parts (Patient.h, Patient.cpp and Main.cpp), but it is giving me errors. Patient.h #ifndef PATIENT_H #define PATIENT_H #include <string> #include "Patient.cpp" using namespace std; class Patient{ private : string firstname; string lastname; string location; static int cnt; int id; public : Patient(string, string, string);...

  • Please help fix my code C++, I get 2 errors when running. The code should be...

    Please help fix my code C++, I get 2 errors when running. The code should be able to open this file: labdata.txt Pallet PAG PAG45982IB 737 4978 OAK Container AYF AYF23409AA 737 2209 LAS Container AAA AAA89023DL 737 5932 DFW Here is my code: #include <iostream> #include <string> #include <fstream> #include <vector> #include <cstdlib> #include <iomanip> using namespace std; const int MAXLOAD737 = 46000; const int MAXLOAD767 = 116000; class Cargo { protected: string uldtype; string abbreviation; string uldid; int...

  • How can I make this compatible with older C++ compilers that DO NOT make use of...

    How can I make this compatible with older C++ compilers that DO NOT make use of stoi and to_string? //Booking system #include <iostream> #include <iomanip> #include <string> using namespace std; string welcome(); void print_seats(string flight[]); void populate_seats(); bool validate_flight(string a); bool validate_seat(string a, int b); bool validate_book(string a); void print_ticket(string passenger[], int i); string flights [5][52]; string passengers [4][250]; int main(){     string seat, flight, book = "y";     int int_flight, p = 0, j = 0;     int seat_number,...

  • For an ungraded C++ lab, we have practice with structs. I'm having a hard time calling...

    For an ungraded C++ lab, we have practice with structs. I'm having a hard time calling the functions relating to our book struct correctly. The issues lie in main(). Here is the .cpp: #include "book.h" void add_author(Book& book){         if (book.num_auth==3){             std::cout<<"\nA book can't have more than 3 authors!";         }         else{             std::cout<<"\nAdd Author: ";             getline(std::cin, book.authors[book.num_auth]);             book.num_auth++;         } }; void remove_last(Book& book){         if(book.num_auth<=1){             std::cout<<"\nA book must have at least 1 author!";...

  • I need a detailed pseudocode for this code in C ++. Thank you #include <iostream> #include...

    I need a detailed pseudocode for this code in C ++. Thank you #include <iostream> #include <string> #include <iomanip> using namespace std; struct Drink {    string name;    double cost;    int noOfDrinks; }; void displayMenu(Drink drinks[], int n); int main() {    const int size = 5;       Drink drinks[size] = { {"Cola", 0.65, 2},    {"Root Beer", 0.70, 1},    {"Grape Soda", 0.75, 5},    {"Lemon-Lime", 0.85, 20},    {"Water", 0.90, 20} };    cout <<...

  • my program wont run on my computer and im not understanding why. please help. #include<iostream> #include<iomanip>...

    my program wont run on my computer and im not understanding why. please help. #include<iostream> #include<iomanip> #include<string> #include "bookdata.h" #include "bookinfo.h" #include "invmenu.h" #include "reports.h" #include "cashier.h" using namespace std; const int ARRAYNUM = 20; BookData bookdata[ARRAYNUM]; int main () { bool userChoice = false; while(userChoice == false) { int userInput = 0; bool trueFalse = false; cout << "\n" << setw(45) << "Serendipity Booksellers\n" << setw(39) << "Main Menu\n\n" << "1.Cashier Module\n" << "2.Inventory Database Module\n" << "3.Report Module\n"...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT