art 1 The purpose of this part of the assignment is to give you practice in crea
ID: 3582807 • Letter: A
Question
art 1 The purpose of this part of the assignment is to give you practice in creating a class. You will develop a program that creates a class for a book. The main program will simply test this class. The class will have the following data members: A string for the name of the author A string for the book title A long integer for the ISBN The class will have the following member functions (details about each one are below:) A default constructor A Print function which prints out all the information about the book. A GetData function which reads information from a file into the data members. A function GetISBN that returns the integer containing the ISBN. (This will be needed in Part 2). You must create your program using the following three files: book.h – used for declaring your class. In this header file, the declarations of the class and its members (both data and functions) will be done without the definitions. The definitions should be done in the book.cpp file. book.cpp – contains the definitions of the member functions: The default constructor will initialize the author's name to “No name”, the title to "Unknown title", and the ISBN to 0. The Print function will display all of the information about a book in a clear format. This will be a const function because it does not change the data members. For formatting purposes, you may assume that no name will have more than 20 characters and that no book title will have more than 50 characters. The GetData function will have the input file as a parameter, will read information from the file and put appropriate values into the data members. (See below for the format of the data file.) The GetISBN function will simply return the long integer containing the ISBN. It also will be a const function. Mp8bookDriver.cpp – should contain the main program to test the class. It should declare two book objects (book1 and book2) using the default constructor. Call the print function for book1 (to show that the default constructor is correct). Open the input file and call the GetData function for book2 and then print its information. Finally, test the GetISBN function for book2 and output the result returned from the function. Format of Data file The name of the data file is mp7book.txt It has data for one book arranged as follows: The name is on one line by itself (hint: use getline). The title is on a line by itself. The ISBN is on the third line. mp7book.txt Jane Smith History Of This World 12349876 Get this part of the program working and save all the files before starting on Part 2. The output should be similar to the following: Testing the book class by (your name) The information for book 1 is: No name Unknown title 0 The information for book 2 is: Jane Smith History Of The World 12349876 book2 has ISBN 12349876 Press any key to continue Part 2 Now you will use the book class to create an array of books for a small library. Note that the book.h and book.cpp files should not have to be changed at all - you just have to change the main program in the Mp8bookDriver.cpp file. There is a new data file, mp7bookarray.txt. It contains the information for the books in the library using the same format as described above for each book. There will be exactly 10 books in the file. Declare an array of books that could hold 10 book objects. Open the new data file and use a loop to call the GetData function to read the information about the books into the objects in the array. Print out the list of books in the library in a nice format. Notice that the books are arranged in order by ISBN in the data file. Now imagine customers coming into the library who want to know whether a particular book is in the collection. Each customer knows the ISBN of the book. Open the third data file (mp8bookISBN.txt) which contains ISBN's, read each one, and use a binary search to find out whether the book is in the array. If it is found, print out all the information about the book, if not, print an appropriate message. Then repeat the process for each of the ISBN's until you get to the end of the file. mp8bookarray.txt H. M. Deitel C++ How to Program 130895717 Judy Bishop Java Gently 201593998 Jeff Salvage The C++ Coach 201702894 Thomas Wu Object-Oriented Programming with Java 256254621 Cay Horstmann Computing Concepts with C++ 471164372 Gary Bronson Program Development and Design 534371302 Joyce Farrell Object-Oriented Programming 619033614 D. S. Malik C++ Programming 619062134 James Roberge Introduction to Programming in C++ 669347183 Nell Dale C++ Plus Data Structures 763714704 mp8bokkISBN.txt 201593998 888899999 763714704 111122222 256254621 130895717 488881111 534371302 619033614
Explanation / Answer
book.h
#include <iostream>
#include <fstream>
using namespace std;
class book
{
public:
string author;
string title;
long int isbn;
book();
void printDetails() const;
void GetData(ifstream *infile);
long int GetISBN() const;
};
---------------------------------------------------------------------------------------------
book.cpp
#include <fstream>
#include <cstdlib>
#include <string>
#include "book.h"
using namespace std;
book::book()
{
author="No name";
title="Unknown title";
isbn=0;
}
void book::printDetails() const
{
cout<<" Author: "<<author;
cout<<" Title: "<<title;
cout<<" ISBN: "<<isbn;
}
void book::GetData(ifstream *infile)
{
string sLine = "";
getline(*infile, sLine);
author=sLine;
getline(*infile, sLine);
title=sLine;
getline(*infile, sLine);
isbn=atoi(sLine.c_str());
}
long int book::GetISBN() const
{
return isbn;
}
---------------------------------------------------------------------------------------------------------
PART 1
SOURCE CODE:
Mp8BookDriver.cpp
#include <fstream>
#include <cstdlib>
#include <string>
#include <iostream>
#include "book.cpp"
using namespace std;
int main()
{
const book b1;
book b2;
ifstream infile;
infile.open("mp7book.txt");
cout<<"The information for book 1 is: ";
b1.printDetails();
b2.GetData(&infile);
cout<<" The information for book 2 is: ";
b2.printDetails();
cout<<" Book 2 has ISBN: "<<b2.GetISBN();
infile.close();
return 1;
}
OUTPUT:
The information for book 1 is:
Author: No name
Title: Unknown title
ISBN: 0
The information for book 2 is:
Author: Jane Smith
Title: History Of This World
ISBN: 12349876
Book 2 has ISBN: 12349876
---------------------------------------------------------------------------------------------------------
PART 2
SOURCE CODE:
Mp8BookDriver.cpp
#include <fstream>
#include <cstdlib>
#include <string>
#include <iomanip>
#include "book.cpp"
using namespace std;
int main()
{
book b[10],temp;
int i=0,j=0,n=10,beg=0,end=n-1,mid=0,key=0,count=0;
string sLine="";
ifstream infile;
infile.open("mp8bookarray.txt");
for(i=0;i<10;i++)
b[i].GetData(&infile);
//Sorting the books acording to ISBN
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if(b[j+1].isbn<b[j].isbn)
{
temp=b[j];
b[j]=b[j+1];
b[j+1]=temp;
}
}
}
//Displaying the sorted list of books
cout<<" -------------------------------------------------------------------------------";
cout<<left<<" "<<setw(20)<<"Author"<<setw(50)<<"Title"<<"ISBN";
cout<<" -------------------------------------------------------------------------------";
for(i=0;i<10;i++)
{
cout<<left<<" "<<setw(20)<<b[i].author<<setw(50)<<b[i].title<<b[i].isbn;
}
cout<<" -------------------------------------------------------------------------------";
infile.close();
//Finding out a book in the list by using its ISBN
infile.open("mp8bookISBN.txt");
while (!infile.eof())
{
getline(infile, sLine);
key=atoi(sLine.c_str());
while(beg<=end)
{
mid=(beg+end)/2;
if( key < b[mid].isbn )
end=mid-1;
else if( key > b[mid].isbn )
beg=mid+1;
else if(b[mid].isbn==key)
{
cout<<" ";
b[mid].printDetails();
beg=n+1;
}
}
if(beg!=(n+1))
cout<<" The book details for ISBN "<<key<<" could not be found!";
beg=0;end=n-1;mid=0;key=0;
}
return 1;
}
OUTPUT:
-------------------------------------------------------------------------------
Author Title ISBN
-------------------------------------------------------------------------------
H. M. Deitel C++ How to Program 130895717
Judy Bishop Java Gently 201593998
Jeff Salvage The C++ Coach 201702894
Thomas Wu Object-Oriented Programming with Java 256254621
Cay Horstmann Computing Concepts with C++ 471164372
Gary Bronson Program Development and Design 534371302
Joyce Farrell Object-Oriented Programming 619033614
D. S. Malik C++ Programming 619062134
James Roberge Introduction to Programming in C++ 669347183
Nell Dale C++ Plus Data Structures 763714704
-------------------------------------------------------------------------------
Author: Judy Bishop
Title: Java Gently
ISBN: 201593998
The book details for ISBN 888899999 could not be found!
Author: Nell Dale
Title: C++ Plus Data Structures
ISBN: 763714704
The book details for ISBN 111122222 could not be found!
Author: Thomas Wu
Title: Object-Oriented Programming with Java
ISBN: 256254621
Author: H. M. Deitel
Title: C++ How to Program
ISBN: 130895717
The book details for ISBN 488881111 could not be found!
Author: Gary Bronson
Title: Program Development and Design
ISBN: 534371302
Author: Joyce Farrell
Title: Object-Oriented Programming
ISBN: 619033614
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.