WAP to Create a class ‘Book’. Derive two classes from it namely ‘’Print_Book’ and ‘E_Book’. Store the following details about the book in the relevant classes. Title, Author, Price, No of pages and Size in KB. Input information for ‘m’ number of print books and ‘n’ number of eBooks and display it.
#include <iostream>
using namespace std;
class Book{
protected:
string Title;
string Author;
double Price;
int No_of_pages;
};
class Print_Book: public Book{
public:
void getData(){
cout<<"\nEnter the Title of the book: ";
cin>>Title;
cout<<"\nEnter the name of the Author: ";
cin>>Author;
cout<<"\nEnter the price of the Book: ";
cin>>Price;
cout<<"\nEnter the number of pages: ";
cin>>No_of_pages;
}
void display(){
cout<<"\nTitle: "<<Title;
cout<<"\nAuthor: "<<Author;
cout<<"\nPrice: "<<Price;
cout<<"\nNumber of pages: "<<No_of_pages;
}
};
class E_Book: public Book{
private:
int Size;
public:
void getData(){
cout<<"\nEnter the Title of the book: ";
cin>>Title;
cout<<"\nEnter the name of the Author: ";
cin>>Author;
cout<<"\nEnter the price of the Book: ";
cin>>Price;
cout<<"\nEnter the number of pages: ";
cin>>No_of_pages;
cout<<"\nEnter the size of the Book in kbs: ";
cin>>Size;
}
void display(){
cout<<"\nTitle: "<<Title;
cout<<"\nAuthor: "<<Author;
cout<<"\nPrice: "<<Price;
cout<<"\nNumber of pages: "<<No_of_pages;
cout<<"\nSize: "<<Size;
}
};
int main ()
{
int m=2;
int n=2;
Print_Book p[m];
E_Book e[n];
cout<<"\nEnter details for the print Book:\n";
for(int i=0;i<m;i++){
p[i].getData();
}
cout<<"\nEnter details for the E_Book:\n";
for(int i=0;i<n;i++){
e[i].getData();
}
cout<<"\nDetails for the print Book:\n";
for(int i=0;i<m;i++){
p[i].display();
}
cout<<"\nDetails for the E_Book:\n";
for(int i=0;i<m;i++){
e[i].display();
}
return 0;
}
Comments
Leave a comment