Answer to Question #183906 in C++ for Taj aslam

Question #183906

Console Application for library system 

Objective: Design and implement an application in C++ to maintain inventory of a library

using object oriented paradigm. 

Define class for library considering the followings:

a. Enlist data members and methods with access specifies.

b. Enlist constant and static members of the class.

c. Specify interface of the class with user.

d. Specify constructors and destructor.

e. Methods for shallow and deep copy constructor.

Your program should maintain the inventory of 20 books.

1
Expert's answer
2021-04-21T12:42:47-0400


#include <iostream>
#include <string>
using namespace std;



class Book{
private:
	string title;
	string author;
	int year;
	string publisher;
	double cost;


public:
	//constructors
	Book(){}


	Book(string title,string author,int year,string publisher,double cost){
		this->title=title;
		this->author=author;
		this->year=year;
		this->publisher=publisher;
		this->cost=cost;
	}
	//destructor
	~Book(){}


	//shallow and deep copy constructor
	Book(Book& book){
		this->title=book.title;
		this->author=book.author;
		this->year=book.year;
		this->publisher=book.publisher;
		this->cost=book.cost;
	}


	string getTitle()
	{
		return this->title;
	}


	string getAuthor()
	{
		return this->author;
	}
	int getYear()
	{
		return this->year;
	}
	string getPublisher()
	{
		return this->publisher;
	} 
	double getCost()
	{
		return this->cost;
	}
	void setTitle(string title)
	{
		this->title=title;
	}


	void setAuthor(string author)
	{
		this->author=author;
	}
	void setYear(int year)
	{
		this->year=year;
	}
	void setPublisher(string publisher)
	{
		this->publisher=publisher;
	} 
	void setCost(double cost)
	{
		this->cost=cost;
	}  


	void display()
    {
		cout<<"Title: "<<title<<"\n";
		cout<<"Author: "<<author<<"\n";
		cout<<"Year: "<<year<<"\n";
		cout<<"Publisher: "<<publisher<<"\n";
		cout<<"Cost: "<<cost<<"\n\n";
    }


};


class Library{
private:
	// the inventory of 20 books.
	static const int maxNumberBooks=20;
	int currentNumberBooks;
	//Enlist constant and static members of the class.
	Book books[20];


public:
	//constructor
	Library(){
		this->currentNumberBooks=0;
	}
	//destructor
	~Library(){}


	void addBook(Book newBook){
		if(this->currentNumberBooks<maxNumberBooks){
			this->books[this->currentNumberBooks]=newBook;
			this->currentNumberBooks++;
		}else{
			cout<<"\nThe library is full.\n\n";
		}
	}


	void displayBooks(){
		for(int i=0;i<this->currentNumberBooks; i++){
			this->books[i].display();
		}
	}


};


int main()
{  


	Library library;
	library.addBook(Book("C++","Peter Smith",2020,"Addison-Wesley Professional",46.56));
	library.addBook(Book("Effective Modern C++: 42 Specific Ways to Improve Your Use","Scott Meyers",2019,"Addison-Wesley Professional",55.66));
	library.addBook(Book("Java","John Smith",2015,"Addison-Wesley Professional",36.56));
	
	library.displayBooks();


	system("pause");
	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