Answer to Question #327866 in C++ for Lol

Question #327866

Create a class Library with data members dept_name, rack_no, quantity which is inherited in class Book with data members book_name, author_name. Initialize the data for book and display the data of book with its dept_name, book_name, author_name, quantity and rack_no.

1
Expert's answer
2022-04-13T02:15:07-0400
#include <iostream>
#include <string>


class Library {
  // Make this variables under 'protected' mark
  // so we can access them only from current or descendant classes
  protected:
    std::string dept_name;
    int rack_no;
    int quantity;
  public:
    Library(std::string _dept_name, int _rack_no, int _quantity)
      : dept_name(_dept_name), rack_no(_rack_no), quantity(_quantity) {}
    ~Library() {}
};


class Book : Library {
  private:
    std::string book_name;
    std::string author_name;
  public:
    Book(std::string _dept_name, int _rack_no, int _quantity,
    std::string _book_name, std::string _author_name)
      : Library(_dept_name, _rack_no, _quantity), book_name(_book_name), author_name(_author_name) {}
    std::string get_dept_name() {
      return dept_name;
    }
    int get_rack_no() {
      return rack_no;
    }
    int get_quantity() {
      return quantity;
    }
    std::string get_book_name() {
      return book_name;
    }
    std::string get_author_name() {
      return author_name;
    }
    ~Book() {}
};


void display_book(Book book) {
  std::cout << "Department:  " << book.get_dept_name() << '\n';
  std::cout << "Name:        " << book.get_book_name() << '\n';
  std::cout << "Author:      " << book.get_author_name() << '\n';
  std::cout << "Quantity:    " << book.get_quantity() << '\n';
  std::cout << "Rack number: " << book.get_rack_no() << '\n';
}


int main() {
  std::string dept_name;
  int rack_no;
  int quantity;
  std::string book_name;
  std::string author_name;
  std::cout << "Enter library department: ";
  std::getline(std::cin, dept_name);
  std::cout << "Enter rack number: ";
  std::cin >> rack_no;
  std::cout << "Enter quantity of books: ";
  std::cin >> quantity;


  // call cin.ignore() to drop out newline symbol ('\n')
  // from buffer because otherwise it will be read into book_name
  std::cin.ignore();
  std::cout << "Enter book name: ";
  std::getline(std::cin, book_name);
  std::cout << "Enter author name: ";
  std::getline(std::cin, author_name);
  Book book(dept_name, rack_no, quantity, book_name, author_name);
  std::cout << "\nBook data:\n--------------------\n";
  display_book(book);
  return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS