Question: driver.cpp #include using namespace std; #include book.h #define MAX_BOOKS 5 void setBooks(Book[]); void displayBooks(Book[]); void setPrice(Book&, const Book&); int main(){ Book collection[MAX_BOOKS]; setBooks(collection); displayBooks(collection); //copy

driver.cpp

#include
using namespace std;
#include "book.h"
#define MAX_BOOKS 5
void setBooks(Book[]);
void displayBooks(Book[]);
void setPrice(Book&, const Book&);
int main(){
Book collection[MAX_BOOKS];
setBooks(collection);
displayBooks(collection);
//copy constructor
Book newBook = collection[0];
newBook.setPrice(0);
newBook.display();
setPrice(newBook, collection[0]);
newBook.display();
}
void setBooks(Book books[]){
try{
books[0].setTitle("The Fifth Season");
books[0].setAuthor("N.K. Jemisin");
books[0].setPublisher("Orbit");
books[0].setYear(2015);
books[0].setPrice(39.99);
books[1].setTitle("The Marvelous Land of Oz");
books[1].setAuthor("L. Frank Baum");
books[1].setPublisher("Reilly & Britton");
books[1].setYear(1904);
books[1].setPrice(199.99);
books[2].setTitle("The Hitchhiker's Guide to the Galaxy");
books[2].setAuthor("Douglas Adams");
books[2].setPublisher("Pan Books");
books[2].setYear(10);
books[2].setPrice(-9.98);
books[3].setTitle("The Olive's Pit");
books[3].setAuthor("Erin Keith");
books[3].setPublisher("Bantam");
books[3].setYear(2049); //ruh roh!
//nothing after the exception occurs executes
books[3].setPrice(19.97);
books[4].setTitle("The Gunslinger");
books[4].setAuthor("Stephen King");
books[4].setPublisher("Grant");
books[4].setYear(1982);
books[4].setPrice(9.99);
}
//but the program doesn't break because we catch it here
catch(const char* e){
cout << e << endl << endl;
}
}
void displayBooks(Book books[]){
for(int i = 0; i < MAX_BOOKS; i++){
cout << "*****" << i + 1 << "*****" << endl;
books[i].display(); cout << endl;
}
}
//we want to change the destination, not the source
void setPrice(Book& destination, const Book& source){
if(destination.getTitle() == source.getTitle()){
destination.setPrice(source.getPrice());
}
}

book.cpp

#include "book.h"
#include
const float MIN_PRICE = .25;
const int MIN_YEAR = 1436;
const int MAX_YEAR = 2020;
using namespace std;
Book::Book(){
title = "";
author = "";
publisher = "";
copyrightYear = MAX_YEAR;
price = MIN_PRICE;
}
Book::Book(string newTitle, string newAuthor, int newYear, double newPrice){
setTitle(newTitle);
setAuthor(newAuthor);
publisher = "";
setYear(newYear);
setPrice(newPrice);
}
Book::Book(string newTitle, string newAuthor, string newPublisher, int newYear, double newPrice){
setTitle(newTitle);
setAuthor(newAuthor);
setPublisher(newPublisher);
setYear(newYear);
setPrice(newPrice);
}
Book::Book(const Book& rhs){
title = rhs.title;
author = rhs.author;
publisher = rhs.publisher;
copyrightYear = rhs.copyrightYear;
price = rhs.price;
}
string Book::getTitle() const{
return title;
}
string Book::getAuthor() const{
return author;
}
string Book::getPublisher() const{
return publisher;
}
int Book::getYear() const{
return copyrightYear;
}
double Book::getPrice() {
return price;
}
void Book::setTitle(string newTitle){
title = newTitle;
}
void Book::setAuthor(string newAuthor){
author = newAuthor;
}
void Book::setPublisher(string newPublisher){
publisher = newPublisher;
}
void Book::setYear(int newYear){
if(newYear < MIN_YEAR || newYear > MAX_YEAR){
stringstream error; // slow but works. Better would be format, but only available in C++ 20
error << "Invalid copyright year \"" << newYear << "\" for book " << title;
throw invalid_argument(error.str());
}
else{
copyrightYear = newYear;
}
}
void Book::setPrice(double newPrice){
if(newPrice < MIN_PRICE){
price = MIN_PRICE;
}
else{
price = newPrice;
}
}
void Book::display(){
if(valid()) {
cout << title << ": " << author << endl;
cout << publisher << ", " << copyrightYear << endl;
cout << "$" << price << endl;
} else {
cout << "This book has incomplete or corrupt data" << endl;
}
}
bool Book::valid() const {
return !(title.empty() || author.empty() || publisher.empty());
}

book.h

#ifndef BOOK_H
#define BOOK_H
#include
#include
class Book{
string title, author, publisher;
double price;
int copyrightYear;
public:
Book();
Book(string, string, int, double);
Book(string, string, string, int, double);
Book(const Book&);
bool valid() const;
string getTitle() const;
string getAuthor() const;
string getPublisher() const;
int getYear() const;
double getPrice();
void setTitle(string);
void setAuthor(string);
void setPublisher(string);
void setYear(int);
void setPrice(double);
void display();
};
#endif

Cant fix the compile error and the run time error.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!