Answer to Question #235254 in C++ for Smiley

Question #235254
Create a C++ class called Book containing bookTitle, author(s), publication, yearOfPublication and price as data members. Write functions for the following operations and test them by creating an array of Book objects. Also include member functions in the same class for input and output of data members and perform the following operations. i) Display the books name sorted in ascending order. ii) Display the books detail authored by “XXX” ( “XXX” must be accepted from user) iii) Display the details of books whose price is greater than 2000.
1
Expert's answer
2021-09-09T17:13:31-0400
#include<iostream>
using namespace std;
class Book{
	private:
	string bookTitle, author, publication;
	int  yearOfPublication;
	double price;
	public:
		void setBookDetails(string title, string a, string pub, int year, double pr){
			
			cout<<"Enter the title of the book\n ";
			cin>>title;
			cout<<"Enter the author of the book\n ";
			cin>>a;
			cout<<"Enter the publication of the book\n ";
			cin>>pub;
			cout<<"Enter the book's year of publication\n ";
			cin>>year;
			cout<<"Enter the book's price\n ";
			cin>>pr;
			bookTitle = title;
			author = a;
			publication = pub;
			yearOfPublication = year;
			price = pr;
			
		}
		void display(){
			cout<<"Book Title:  "<<bookTitle<<endl;
				cout<<"Book Author:  "<<author<<endl;
				cout<<"Book Publication:  "<<publication<<endl;
				cout<<"Book Year Of Publication:  "<<author<<endl;
			cout<<"Book Price:  "<<price<<endl;
							
				
		}
		string getName(){
			return bookTitle;
		}
		string getAuthor(){
			return author;
		}
		double getPrice(){
			return price;
		}
};


int main(){
	int n;
	cout<<"Enter the number of books\n";
	cin>>n;
	Book books[n];
	cout<<"Enter the details for the books\n";
	for(int i=0; i<n; i++){
		string title;
		 string a;
		 string pub;
		 int year; 
		double pr;
		Book j;
		cout<<"Book "<<(i+1)<<endl;
		j.setBookDetails(title,a, pub,year, pr);
		books[i] = j;
	}
	cout<<"The Details Entered are: \n";
	for(int i=0; i<n; i++){
			cout<<"Book "<<(i+1)<<endl;
			books[i].display();
	}
	cout<<"\n\nDisplaying the Books' Names\n";
	cout<<"Book Names:\n";
	for(int i=0; i<n; i++){
		cout<<books[i].getName()<<endl;
	}
	string author;
	cout<<"\n\nEnter the name of the Author to display all books the author owns\n";
	cin>>author;
	for(int i=0; i<n; i++){
		if(books[i].getAuthor()==author){
			books[i].display();
		}
	}
	cout<<"\n\nThe books having prices greater than 2000\n";
	for(int i=0; i<n; i++){
		if(books[i].getPrice() >2000){
			
			books[i].display();
		}
	}
}

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