Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- class Book {
- private:
- string title;
- string author;
- string publisher;
- int year;
- int pages;
- double price;
- int rating;
- public:
- Book(string title, string author, string publisher, int year, int pages, double price, int rating) {
- this->title = title;
- this->author = author;
- this->publisher = publisher;
- this->year = year;
- this->pages = pages;
- this->price = price;
- this->rating = rating;
- }
- string getTitle() {
- return title;
- }
- string getAuthor() {
- return author;
- }
- string getPublisher() {
- return publisher;
- }
- int getYear() {
- return year;
- }
- int getPages() {
- return pages;
- }
- double getPrice() {
- return price;
- }
- int getRating() {
- return rating;
- }
- void setTitle(string title) {
- this->title = title;
- }
- void setAuthor(string author) {
- this->author = author;
- }
- void setPublisher(string publisher) {
- this->publisher = publisher;
- }
- void setYear(int year) {
- this->year = year;
- }
- void setPages(int pages) {
- this->pages = pages;
- }
- void setPrice(double price) {
- this->price = price;
- }
- void setRating(int rating) {
- this->rating = rating;
- }
- virtual void print() {
- cout << "Title: " << title << endl;
- cout << "Author: " << author << endl;
- cout << "Publisher: " << publisher << endl;
- cout << "Year: " << year << endl;
- cout << "Pages: " << pages << endl;
- cout << "Price: " << price << endl;
- cout << "Rating: " << rating << endl;
- }
- friend class Librarian;
- friend Book compareBooksByRating(Book b1, Book b2);
- };
- class Novel : public Book {
- private:
- string genre;
- public:
- Novel(string title, string author, string publisher, int year, int pages, double price, int rating, string genre) : Book(title, author, publisher, year, pages, price, rating) {
- this->genre = genre;
- }
- string getGenre() {
- return genre;
- }
- void setGenre(string genre) {
- this->genre = genre;
- }
- void print() {
- Book::print();
- cout << "Novel Genre: " << genre << endl;
- }
- };
- Book compareBooksByRating(Book b1, Book b2) {
- if (b1.getRating() > b2.getRating()) {
- return b1;
- } else {
- return b2;
- }
- }
- class Librarian {
- public:
- void printBook(Book b) {
- b.print();
- }
- Book compareBooksByRating(Book b1, Book b2) {
- return compareBooksByRating(b1, b2);
- }
- };
- int main () {
- Book b1("The Great Gatsby", "F. Scott Fitzgerald", "Charles Scribner's Sons", 1925, 180, 9.99, 4);
- Novel n1("The Hobbit", "J. R. R. Tolkien", "Allen & Unwin", 1937, 304, 19.99, 5, "Fantasy");
- Librarian l;
- l.printBook(b1);
- l.printBook(n1);
- cout << "Comparing books by rating..." << endl;
- cout << "The book with the better rating is " << compareBooksByRating(b1, n1).getTitle() << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement