In: Computer Science
Part 1 (Objective C++ and please have output screenshot)
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:
The class will have the following member functions (details about each one are below:)
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:
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:
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
Part1:
book.h File--------:
class Book
{
private :
public :
string name;
string title;
int ISBN;
Book();
void Print();
void GetData(char* str);
int GetISBN();
};
book.cpp-----------:
include "book.h"
Book::Book()
{
name = "No Name";
title = "Unknown title";
ISBN = 0;
}
void Book::Print()
{
cout<<"\n"<<name<<"
"<<title
<<" "<<ISBN<<endl;
}
void Book::GetData(char* file)
{
ifstream ifs(file);
string line;
int i = -1, j = 1;
if(ifs)
{
while(getline(ifs,line) &&
fileCounter == j++)
{
if(++i==
0)
name = line;
else if(++i ==
1)
title = line;
else
ISBN = atoi(line.c_str());
fileCounter++;
}
}
}
int Book::GetISBN()
{
return ISBN;
}
Mp8bookDriver.cpp----:
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int fileCounter = 1;
#include "book.h"
//using namespace std;
int main()
{
Book book1 = Book();
Book book2 = Book();
cout<<"Testing the book class
by(ABC)"<<endl;
cout<<"The information for
book1 is:"<<endl;
book1.Print();
char* filePath = (char
*)"mp7book.txt";
book2.GetData(filePath);
cout<<"The information for
book2 is:"<<endl;
book2.Print();
int isbn = book2.GetISBN();
cout<<"Book2 has ISBN
:"<<isbn<<endl;
cout<<"Press any key to
continue"<<endl;
getchar();
}
Part 2:-::
book.h
class Book
{
private :
public :
string name;
string title;
int ISBN;
Book();
void Print();
void GetData(char* str);
int GetISBN();
};
book.cpp
include "book.h"
Book::Book()
{
name = "No Name";
title = "Unknown title";
ISBN = 0;
}
void Book::Print()
{
cout<<"\n"<<name<<"
"<<title
<<" "<<ISBN<<endl;
}
void Book::GetData(char* file)
{
ifstream ifs(file);
string line;
int i = -1, j = 1;
if(ifs)
{
while(getline(ifs,line) &&
fileCounter == j++)
{
if(++i==
0)
name = line;
else if(++i ==
1)
title = line;
else
ISBN = atoi(line.c_str());
fileCounter++;
}
}
}
int Book::GetISBN()
{
return ISBN;
}
Mp8bookDriver.cpp
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int fileCounter = 1;
#include "book.h"
int binarySearch(Book book[], int s, int e, int num)
{
if (s <= e) {
int mid = (s+e)/2;
if (book[mid].ISBN == num)
return mid ;
if (book[mid].ISBN > num)
return binarySearch(book, s, mid-1, num);
if (book[mid].ISBN > num)
return binarySearch(book, mid+1, e, num);
}
return -1;
}
int main()
{
Book book[10] = Book();
char* filePath =
(char*)"mp8bookarray.txt";
for(int i=0; i<10; i++)
book[i].GetData(filePath);
for(int i=0; i<10; i++)
book[i].Print();
ifstream
ifs("mp8bookISBN.txt");
string line = "";
cout<<"Searchng by ISBN
Number"<<endl;
while(ifs>>line)
{
int id =
binarySearch(book, 0, 9, atoi(line.c_str()));
if(id>-1)
book[id].Print();
else
cout<<"No book found for
"<<line<<" ISBN"<<endl;
}
cout<<"Press any key to
continue"<<endl;
getchar();
}