Write code in c++ a. Some of the characteristics of a book are the title, author
ID: 3768833 • Letter: W
Question
Write code in c++
a. Some of the characteristics of a book are the title, author(s), publisher,
ISBN, price, and year of publication. Design a class bookType that
defines the book as an ADT.
i. Each object of the class bookType can hold the following
information about a book: title, up to four authors, publisher,
ISBN, price, and number of copies in stock. To keep track of
the number of authors, add another member variable.
ii. Include the member functions to perform the various operations on
objects of type bookType. For example, the usual operations that
can be performed on the title are to show the title, set the title, and
check whether a title is the same as the actual title of the book.
Similarly, the typical operations that can be performed on the
number of copies in stock are to showthe number of copies in stock,
set the number of copies in stock, update the number of copies in
stock, and return the number of copies in stock. Add similar operations
for the publisher, ISBN, book price, and authors. Add the
appropriate constructors and a destructor (if one is needed).
b. Write the definitions of the member functions of the class bookType.
Explanation / Answer
#include<iostream>
#include<cstring>
using namespace std;
//Creating a template class with name media of type T
template<class T>
class media
{
protected:
char title[50];
int publication;
T price;
public:
virtual void read()=0;
virtual void show()=0;
};
//Creating a template class with name book of type T
template<class T>
class book:public media<double>
{
private:
int pages;
public:
void read();
void show();
};
//Redefinition of virtual function read() in class book
template<class T>
void book<T>::read()
{
cout<<"Enter book title:";
cin.ignore();
cin.getline(title,50);
cout<<"Enter no. of publications:";
cin>>publication;
cout<<"Enter price of the book(in rupees):";
cin>>price;
cout<<"Enter no. of pages:";
cin>>pages;
}
//Redefinition of virtual function show() in class book
template<class T>
void book<T>::show()
{
cout<<"Book information"<<endl<<"------------------------------"<<endl;
cout<<"Book title:"<<title<<endl;
cout<<"No. of publications:"<<publication<<endl;
cout<<"Price:"<<price<<endl;
cout<<"No. of pages:"<<pages<<endl<<endl;
}
//Creating a template class with name tape of type T
template<class T>
class tape:public media<double>
{
private:
T time;
public:
void read();
void show();
};
//Redefinition of virtual function read() in class tape
template<class T>
void tape<T>::read()
{
cout<<"Enter title:";
cin.ignore();
cin.getline(title,50);
cout<<"Enter no. of publications:";
cin>>publication;
cout<<"Enter price(in rupees):";
cin>>price;
cout<<"Enter playing time(mins.):";
cin>>time;
}
//Redefinition of virtual function show() in class tape
template<class T>
void tape<T>::show()
{
cout<<"Tape information"<<endl<<"------------------------------"<<endl;
cout<<"Title:"<<title<<endl;
cout<<"No. of publications:"<<publication<<endl;
cout<<"Price:"<<price<<endl;
cout<<"Playing time:"<<time<<endl<<endl;
}
int main()
{
media<double>*m[9999];
int n=0;
char ch;
cout<<"Check for int data ------------------------------------------- ";
do
{
cout<<"Enter book or tape(b/t):";
cin>>ch;
if(ch=='b')
m[n]=new book<int>;
else
m[n]=new tape<int>;
m[n++]->read();
cout<<"Do you want to enter another record(Y/N)?:";
cin>>ch;
}while(ch=='Y'||ch=='y');
cout<<endl<<endl;
for(int i=0;i<n;i++)
m[i]->show();
cout<<"Check for double data ------------------------------------------- ";
n=0;
do
{
cout<<"Enter book or tape(b/t):";
cin>>ch;
if(ch=='b')
m[n]=new book<double>;
else
m[n]=new tape<double>;
m[n++]->read();
cout<<"Do you want to enter another record(Y/N)?:";
cin>>ch;
}while(ch=='Y'||ch=='y');
cout<<endl<<endl;
for(int i=0;i<n;i++)
m[i]->show();
return 0;
}
Thank you
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.