Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write the following Book class: A Book has-a title, ISBN number, author and pric

ID: 3587563 • Letter: W

Question

Write the following Book class: A Book has-a title, ISBN number, author and price.

Include the following:

• All instance fields (variables)

• Two Constructors: a default constructor and one requiring all fields as parameters

• Methods – an accessor and mutator for each instance fields

Include the following:

1. All instance fields (variables) included and of appropriate type.

2. Two constructors included and coded correctly: a no-args constructor and one requiring a parameter for each instance field. All fields initialized.

3. All methods (accessors and mutators)included and coded correctly.

Explanation / Answer

#include <iostream>

using namespace std;

class Book

{

//variables

private:

string title;

string ISBN;

string author;

double price;

public:

Book() //default constructor

{

title = " ";

ISBN = " ";

author = " ";

price = 0.0;

}

Book(string title,string ISBN,string author,double price) //argument constructor

{

this->title = title;

this->ISBN = ISBN;

this->author = author;

this->price = price;

}

//set and get methods for all variables

void setTitle(string title)

{

this->title = title;

}

string getTitle()

{

return title;

}

void setISBN(string ISBN)

{

this->ISBN = ISBN;

}

string getISBN()

{

return ISBN;

}

void setAuthor(string author)

{

this->author = author;

}

string getAuthor()

{

return author;

}

void setPrice(double price)

{

this->price = price;

}

double getPrice()

{

return price;

}

};

int main() {

//objects of class Book

Book b1("C++ Programming","1-23455-567","Bjarne Stroustrup",50.89);

cout<<"Book b1 :"<<b1.getTitle()<<" by "<<b1.getAuthor()<<" of price "<<b1.getPrice();

Book b2;

b2.setTitle("Java Testing and Design");

b2.setAuthor("Frank Cohen");

b2.setPrice(34.56);

cout<<" Book b2 :"<<b2.getTitle()<<" by "<<b2.getAuthor()<<" of price "<<b2.getPrice();

return 0;

}

Output: