#include <iostream>
#include <string>
using namespace std;
// Base class Item
class Item {
protected :
string title;
double price;
public :
Item( const string& t, double p) : title( t) , price( p) { }
virtual void display( ) const {
cout << "Title: " << title << ", Price: $" << price << endl;
}
} ;
// Subclass Book
class Book : public Item {
protected :
string author;
string genre;
string ISBN;
public :
Book( const string& t, double p, const string& a, const string& g, const string& i)
: Item( t, p) , author( a) , genre( g) , ISBN( i) { }
void display( ) const override {
Item:: display ( ) ;
cout << "Author: " << author << ", Genre: " << genre << ", ISBN: " << ISBN << endl;
}
} ;
// Subclass CD
class CD : public Item {
protected :
string artist;
string genre;
double duration;
public :
CD( const string& t, double p, const string& a, const string& g, double d)
: Item( t, p) , artist( a) , genre( g) , duration( d) { }
void display( ) const override {
Item:: display ( ) ;
cout << "Artist: " << artist << ", Genre: " << genre << ", Duration: " << duration << " minutes" << endl;
}
} ;
// Further subclass Fiction from Book
class Fiction : public Book {
string categorySpecificData;
public :
Fiction( const string& t, double p, const string& a, const string& g, const string& i, const string& csd)
: Book( t, p, a, g, i) , categorySpecificData( csd) { }
void display( ) const override {
Book:: display ( ) ;
cout << "Category Specific Data: " << categorySpecificData << endl;
}
} ;
// Further subclass NonFiction from Book
class NonFiction : public Book {
string categorySpecificData;
public :
NonFiction( const string& t, double p, const string& a, const string& g, const string& i, const string& csd)
: Book( t, p, a, g, i) , categorySpecificData( csd) { }
void display( ) const override {
Book:: display ( ) ;
cout << "Category Specific Data: " << categorySpecificData << endl;
}
} ;
// Further subclass Rock from CD
class Rock : public CD {
string categorySpecificData;
public :
Rock( const string& t, double p, const string& a, const string& g, double d, const string& csd)
: CD( t, p, a, g, d) , categorySpecificData( csd) { }
void display( ) const override {
CD:: display ( ) ;
cout << "Category Specific Data: " << categorySpecificData << endl;
}
} ;
// Further subclass Pop from CD
class Pop : public CD {
string categorySpecificData;
public :
Pop( const string& t, double p, const string& a, const string& g, double d, const string& csd)
: CD( t, p, a, g, d) , categorySpecificData( csd) { }
void display( ) const override {
CD:: display ( ) ;
cout << "Category Specific Data: " << categorySpecificData << endl;
}
} ;
int main( ) {
// Create sample instances of various books and CDs
Fiction fictionBook( "The Great Gatsby" , 15.99 , "F. Scott Fitzgerald" , "Fiction" , "978-0743273565" , "Classic American Literature" ) ;
NonFiction nonFictionBook( "Sapiens" , 19.99 , "Yuval Noah Harari" , "Non-Fiction" , "978-0062316110" , "History of Humankind" ) ;
Rock rockCD( "Led Zeppelin IV" , 12.99 , "Led Zeppelin" , "Rock" , 42.38 , "Classic Rock Album" ) ;
Pop popCD( "Thriller" , 10.99 , "Michael Jackson" , "Pop" , 42.19 , "Best-selling Pop Album" ) ;
// Display details
fictionBook.display ( ) ;
cout << endl;
nonFictionBook.display ( ) ;
cout << endl;
rockCD.display ( ) ;
cout << endl;
popCD.display ( ) ;
return 0 ;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8c3RyaW5nPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwovLyBCYXNlIGNsYXNzIEl0ZW0KY2xhc3MgSXRlbSB7CnByb3RlY3RlZDoKICAgIHN0cmluZyB0aXRsZTsKICAgIGRvdWJsZSBwcmljZTsKcHVibGljOgogICAgSXRlbShjb25zdCBzdHJpbmcmIHQsIGRvdWJsZSBwKSA6IHRpdGxlKHQpLCBwcmljZShwKSB7fQogICAgdmlydHVhbCB2b2lkIGRpc3BsYXkoKSBjb25zdCB7CiAgICAgICAgY291dCA8PCAiVGl0bGU6ICIgPDwgdGl0bGUgPDwgIiwgUHJpY2U6ICQiIDw8IHByaWNlIDw8IGVuZGw7CiAgICB9Cn07Ci8vIFN1YmNsYXNzIEJvb2sKY2xhc3MgQm9vayA6IHB1YmxpYyBJdGVtIHsKcHJvdGVjdGVkOgogICAgc3RyaW5nIGF1dGhvcjsKICAgIHN0cmluZyBnZW5yZTsKICAgIHN0cmluZyBJU0JOOwoKcHVibGljOgogICAgQm9vayhjb25zdCBzdHJpbmcmIHQsIGRvdWJsZSBwLCBjb25zdCBzdHJpbmcmI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