C++**** Implement a Book class. The book class should have the following have pr
ID: 3914505 • Letter: C
Question
C++****
Implement a Book class. The book class should have the following have private member variables
for the ISBN (store as a string), title, author and whether or not the book is checked out or not.
? Write a constructor that assigns values to each member variable as well as a default
constructor that leaves data uninitialized.
? Write public methods for retrieving these data values.
? Write a public method for checking a book in and out.
? Write a public method -- istream& read_book(istream& is);
? We will use this function to load a single book’s data into a Book object.
? This method will read a Book ’s data (from a text file) in the following format:
( Title: "Adventures of Huckleberry Finn" ) ( ISBN: 1234567 ) ( Author: Mark Twain )
Add operators for the Book class. Have the == operator check whether the ISBN numbers are the
same for two books. Have != also compare the ISBN numbers. Have << print out the title, author,
and ISBN on separate lines. ==, != should be class methods, and << must be a function.
Explanation / Answer
//Book.h
#pragma once
#include<iostream>
#include<string>
using namespace std;
class Book
{
int ISBN;
string title;
string author;
string checked_out;
public:
Book();
Book(int no, string tit, string au, string status);
int get_ISBN() const;
string get_title() const;
string get_author() const;
string get_status() const;
istream& read_book(istream& is);
bool operator==(Book &obj);
bool operator!=(Book &obj);
friend ostream& operator<<(ostream& out, Book &obj);
};
--------------------------------------------
//Book.cpp
#include"book.h"
Book::Book()
{
ISBN=0;
title="";
author="";
checked_out = "";
}
Book::Book(int no, string tit, string au, string status)
{
ISBN = no;
title = tit;
author = au;
checked_out = status;
}
int Book::get_ISBN() const
{
return ISBN;
}
string Book::get_title() const
{
return title;
}
string Book::get_author() const
{
return author;
}
string Book::get_status() const
{
return checked_out;
}
istream& Book::read_book(istream& is)
{
char ch;
string str;
int count = 0;
cout << "Enter in format Title => ISBN=>Author ";
while (!is.eof())
{
is >> str;
if (isdigit(str[0]))
{
++count;
ISBN = stoi(str);
count++;
}
else
{
if (count == 0)
{
title += str;
title += ' ';
}
if (count == 2)
{
author += str;
author += ' ';
}
}
}
return is;
}
bool Book::operator==(Book &obj)
{
if (ISBN == obj.ISBN)
return true;
else
return false;
}
bool Book::operator!=(Book &obj)
{
if (ISBN != obj.ISBN)
return true;
else
return false;
}
ostream& operator<<(ostream& out, Book &obj)
{
cout << "Book's information: ";
out << "Title : " << obj.title << endl;
out << "ISBN : " << obj.ISBN << endl;
out << "Author : " << obj.author << endl;
//out << "Checkout : " << obj.checked_out << endl;
return out;
}
----------------------------------------------
//main.cpp
#include<fstream>
#include"book.h"
using namespace std;
int main()
{
Book bk;
ifstream in;
in.open("book.txt");
if (!in)
{
cout << "Not able to open input file book.txt" << endl;
return -1;
}
bk.read_book(in);
cout << bk << endl;
//check overloaded constructor
Book bk1(1234567, "American elf", "James Kochalka", "yes");
//print info using << operator
cout << bk1 << endl;
//check if two ISBN equal or not
if (bk1 == bk)
{
cout << "ISBN of bk1 and bk objects are equal ";
}
else
{
cout << "ISBN of bk1 and bk objects are not equal ";
}
//check overloaded operator !=
if (bk1 != bk)
{
cout << "ISBN of bk1 and bk objects are not equal ";
}
else
{
cout << "ISBN of bk1 and bk objects are equal ";
}
}
/*output
Enter in format Title => ISBN=>Author
Book's information:
Title : Adventures of Huckleberry Finn
ISBN : 1234567
Author : Mark Twain
Book's information:
Title : American elf
ISBN : 1234567
Author : James Kochalka
ISBN of bk1 and bk objects are equal
ISBN of bk1 and bk objects are equal
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.