Create A Book Class With Data Members Book Title, ...

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

  Home Study tools  My courses  My books Career Life

home / study / engineering / computer science / computer science questions and answers / create...

Find solutions forCreate


Question: your homework
a Book
class with data members book title,
author name, price, edition. You are required t...

C++

Show transcribed image text

Expert Answer

Anonymous
answered this

#include<bits/stdc++.h>

class Book {
public;

string booktitle;

string authorname;

float price;

int edition;

stack<Book>b; // stack to store the books

vector<Book>v; // using vector alse to store the books


which is easier to search for a book than in stack

int len = 5; // using this variable for to store the maximum


count of books that can be stored in the stack

Book() {

booktitle = "Harry Potter"; //for example am initialising th


book title as Harry potter

void setData(string title, string aname, float bprice, int


bedition) {

booktitle = title; // initialising the data memebers with the


user values

authorname = aname;

price = bprice;

edition = bedition;

bool isEmpty() {

return b.empty(); // empty() method will retuen true if stck is


empty

bool isFull() {

return b.size() == len; // size() function will return the size


of stack

void push(Book book) {

string question;

if(len == b.size()) { // checking whether the stack is full

cin>>question; // if it is full reading whether user wants


to push or not

if(question == "yes") {

len++; // increasing the stack size;

if(count(v.begin(), v.end(), book) == 0) { // checking whether


the book is already present in the vector(count
0 indicates that
the book is not present)

v.push_back(book); //push into vector

b.push(book); //push into stack


} else {

cout<<"Book is already present";

} else {

if(count(v.begin(), v.end(), book) == 0) {

v.push_back(book);

b.push(book);

} else {

cout<<"Book is already present";

void pop() {

b.pop(); // removing the top most book

v.pop_back();

  }

void reverseStack() {

stack<Book>t1; // temporary stack to store the reverse


values of the stack

while(!b.empty()) {

t1.push(b.top());

b.pop();

b = t1;

reverse(v.begin(), v.end());

bool searchBook() {

string type;

string name;

cin>>type;

cin>>name;

if(type == "book name") {

for(int i = 0; i < v.size(); i++) { // searching in the


vector
if(v[i].booktitle == name) {

return true;

} else {

for(int i = 0; i < v.size(); i++) {

if(v[i].authorname == name) {

return true;

return false;

0 Comments

Was this answer helpful? 0

0
Questions viewed by other students

You are required to implement the following functions using the Last In First Out data structure. Create
a Student class with data-members name, regNo, department and cGpa. You are required to implement
the following functions using the First In First Out (queue) data structure • Constructor – for initializing
the default values of a Students setData() - for setting the data in the...
See answer

I need help with this please... in Java - Two files are


required, a data class named Book and an
executable class named
TestBook. Class Book has instance data members (all private) String
title, String
author, int pages, double price. has a public static
int variable named numBooks with an initial value of
zero. has a
parameterized constructor that will be used to make a Book object...
See answer 100% (1 rating)

Show more 
COMPANY

LEGAL & POLICIES

CHEGG PRODUCTS AND SERVICES

CHEGG NETWORK

CUSTOMER SERVICE

© 2003-2021 Chegg Inc. All rights reserved.

You might also like