Advertisement
brospresident

Untitled

Nov 20th, 2021
1,271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.56 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class Book {
  6.     private:
  7.         string title;
  8.         string author;
  9.         string publisher;
  10.         int year;
  11.         int pages;
  12.         double price;
  13.         int rating;
  14.  
  15.     public:
  16.         Book(string title, string author, string publisher, int year, int pages, double price, int rating) {
  17.             this->title = title;
  18.             this->author = author;
  19.             this->publisher = publisher;
  20.             this->year = year;
  21.             this->pages = pages;
  22.             this->price = price;
  23.             this->rating = rating;
  24.         }
  25.  
  26.         string getTitle() {
  27.             return title;
  28.         }
  29.  
  30.         string getAuthor() {
  31.             return author;
  32.         }
  33.  
  34.         string getPublisher() {
  35.             return publisher;
  36.         }
  37.  
  38.         int getYear() {
  39.             return year;
  40.         }
  41.  
  42.         int getPages() {
  43.             return pages;
  44.         }
  45.  
  46.         double getPrice() {
  47.             return price;
  48.         }
  49.  
  50.         int getRating() {
  51.             return rating;
  52.         }
  53.  
  54.         void setTitle(string title) {
  55.             this->title = title;
  56.         }
  57.  
  58.         void setAuthor(string author) {
  59.             this->author = author;
  60.         }
  61.  
  62.         void setPublisher(string publisher) {
  63.             this->publisher = publisher;
  64.         }
  65.  
  66.         void setYear(int year) {
  67.             this->year = year;
  68.         }
  69.  
  70.         void setPages(int pages) {
  71.             this->pages = pages;
  72.         }
  73.  
  74.         void setPrice(double price) {
  75.             this->price = price;
  76.         }
  77.  
  78.         void setRating(int rating) {
  79.             this->rating = rating;
  80.         }
  81.  
  82.         virtual void print() {
  83.             cout << "Title: " << title << endl;
  84.             cout << "Author: " << author << endl;
  85.             cout << "Publisher: " << publisher << endl;
  86.             cout << "Year: " << year << endl;
  87.             cout << "Pages: " << pages << endl;
  88.             cout << "Price: " << price << endl;
  89.             cout << "Rating: " << rating << endl;
  90.         }
  91.  
  92.         friend class Librarian;
  93.         friend Book compareBooksByRating(Book b1, Book b2);
  94. };
  95.  
  96. class Novel : public Book {
  97.     private:
  98.         string genre;
  99.  
  100.     public:
  101.         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) {
  102.             this->genre = genre;
  103.         }
  104.  
  105.         string getGenre() {
  106.             return genre;
  107.         }
  108.  
  109.         void setGenre(string genre) {
  110.             this->genre = genre;
  111.         }
  112.  
  113.         void print() {
  114.             Book::print();
  115.             cout << "Novel Genre: " << genre << endl;
  116.         }
  117. };
  118.  
  119. Book compareBooksByRating(Book b1, Book b2) {
  120.     if (b1.getRating() > b2.getRating()) {
  121.         return b1;
  122.     } else {
  123.         return b2;
  124.     }
  125. }
  126.  
  127. class Librarian {
  128.     public:
  129.         void printBook(Book b) {
  130.             b.print();
  131.         }
  132.  
  133.         Book compareBooksByRating(Book b1, Book b2) {
  134.             return compareBooksByRating(b1, b2);
  135.         }
  136. };
  137.  
  138. int main () {
  139.     Book b1("The Great Gatsby", "F. Scott Fitzgerald", "Charles Scribner's Sons", 1925, 180, 9.99, 4);
  140.     Novel n1("The Hobbit", "J. R. R. Tolkien", "Allen & Unwin", 1937, 304, 19.99, 5, "Fantasy");
  141.     Librarian l;
  142.  
  143.     l.printBook(b1);
  144.     l.printBook(n1);
  145.  
  146.     cout << "Comparing books by rating..." << endl;
  147.     cout << "The book with the better rating is " << compareBooksByRating(b1, n1).getTitle() << endl;
  148.  
  149.     return 0;
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement