C++ Build a Program to Display all the Books in a File Using the book class incl
ID: 3697460 • Letter: C
Question
C++ Build a Program to Display all the Books in a File
Using the book class included, create another program that when run, will display all the books that are stored in the File “Books.txt”.
_____________________________________________________________
#include <string>
#include <iostream>
using namespace std;
class Book
{
private:
string isbn, name, author, publisher;
double price;
int num_pages;
public:
Book();
Book(string p_isbn, string p_name, string p_author, string p_publisher, double p_price, int p_numpages);
string get_isbn();
string get_name();
string get_author();
string get_publisher();
double get_price();
int get_numpages();
void set_isbn(string p_isbn);
void set_name(string p_name);
void set_author(string p_author);
void set_publisher(string p_publisher);
void set_price(double p_price);
void set_numpages(int p_numpages);
void display();
};
Book::Book()
{
num_pages = 200;
isbn = "default_isbn";
name = "chegg.com";
author = "user";
publisher = "default_publisher";
price = 100.00;
}
Book::Book(string p_isbn, string p_name, string p_author, string p_publisher, double p_price, int p_numpages)
{
num_pages = p_numpages;
isbn = p_isbn;
name = p_name;
author = p_author;
publisher = p_publisher;
price = p_price;
}
string Book::get_isbn()
{
return isbn;
}
string Book::get_name()
{
return name;
}
string Book::get_author()
{
return author;
}
string Book::get_publisher()
{
return publisher;
}
double Book::get_price()
{
return price;
}
int Book::get_numpages()
{
return num_pages;
}
void Book::set_isbn(string p_isbn)
{
isbn = p_isbn;
}
void Book::set_name(string p_name)
{
name = p_name;
}
void Book::set_author(string p_author)
{
author = p_author;
}
void Book::set_publisher(string p_publisher)
{
publisher = p_publisher;
}
void Book::set_price(double p_price)
{
price = p_price;
}
void Book::set_numpages(int p_numpages)
{
num_pages = p_numpages;
}
void Book::display()
{
cout<<"isbn "< cout<<"name "<< name < cout<<"author "<< author < cout<<"publisher "<< publisher < cout<<"price "<< price < cout<<"number of pages "<< num_pages < }
In book.cpp
#include "book.h"
int main()
{
Book obj1("default_isbn", "compilers", "I dont know", "random publisher", 100, 200);
obj1.display();
}
Explanation / Answer
I assume that Content of Books.txt is:
IND123
Computer Network
Robert Krush
GMT HIll
432
340
Then Please find book.cpp
#include "book.h"
#include<fstream>
#include<iostream>
using namespace std;
int main(){
ifstream readBook("Books.txt");
string isbn;
string name;
string author;
string publisher;
double price;
int pages;
while(!readBook.eof()){
getline(readBook, isbn);
if(isbn.empty())
break;
getline(readBook,name );
getline(readBook, author);
getline(readBook, publisher);
readBook>>pages;
readBook>>price;
// creating book object with all user data
Book obj1(isbn, name, author, publisher, price, pages);
obj1.display();
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.