Answer to Question #296533 in C++ for Fatimah

Question #296533

1. Write a list of book defining parameters. (5 at least)



o Name (String) – Topic (String) – Pages (int) – IsEnglishLanguage (bool) – Price



(float)



2. Declare and initialize the variables of book parameters.



3. Take input from user and output all the parameters given by user.



4. Create a function to initialize book parameter taken form user.



5. Initialize and store 2 books’ parameters. Print the book with more pages, total number of



pages.



6. Print the page numbers of each book using loop. (1 – 2 – 3 – 4 …..)



7. Store the 3 books data using array (each parameter separate).



8. Use loop store 10 books data in array, given by user. Print the data after input using



function and loop.



9. Store one book data via pointer



10. Store one book data via Reference



11. Store one book data via pointer. Use function to initialize the data given by user.



12. Store one book data via Reference. Use function to initialize the data given by user.

1
Expert's answer
2022-02-11T07:43:48-0500
#include<iostream>
#include<string>
#include<iomanip>

using namespace std;

class Book//1
{
	string Name;
	string Topic;
	int Pages;
	bool IsEnglishLanguage;
	float Price;
public:
	//default constructor
	Book(){}
	//parametrized constructor
	Book(string _Name, string _Topic, int _Pages, bool _IsEnglishLanguage,
	float _Price):Name(_Name),Topic(_Topic),Pages(_Pages),
	IsEnglishLanguage(_IsEnglishLanguage),Price(_Price){}
	//copy constructor
	Book(const Book& b):Name(b.Name),Topic(b.Topic),Pages(b.Pages),
	IsEnglishLanguage(b.IsEnglishLanguage),Price(b.Price){}
	
	void SetData()
	{
		cout<<"Please, enter a Name of book: ";
		getline(cin,Name,'\n');
		cout<<"Please, enter a Topic of book: ";
		getline(cin,Topic,'\n');
		cout<<"Please, enter a number of Pages in book: ";
		cin>>Pages;
		cout<<"Is book in English Language? [y/n]: ";
		char c;
		cin>>c;
		if(c=='y'||c=='Y')IsEnglishLanguage=true;
		else IsEnglishLanguage=false;
		cout<<"Please, enter Price of book: ";
		cin>>Price;
	}
 	void Display()
	{
		cout<<"\nInfo about book:"
			<<"\nName: "<<Name
			<<"\nTopic: "<<Topic
			<<"\nPages: "<<Pages
			<<"\nIn English: "<<boolalpha<<IsEnglishLanguage
			<<"\nPrice: "<<Price;
	}
	void PrintPages()
	{
		cout<<endl;
		for(int i=1;i<Pages;i++)
		{
			cout<<i<<" - ";
		}
		cout<<Pages;
	}
	friend void CompareBookPages(Book& a,Book& b);
};

void CompareBookPages(Book& a, Book& b)
{
	if(a.Pages>b.Pages)
	{
		a.Display();
	}
	else if(a.Pages<b.Pages)
	{
		b.Display();
	}
	else
		cout<<"\nBooks are equal!";
	cout<<"\nTotal number of pages are "<<a.Pages+b.Pages;
}

int main()
{
	Book a;
	a.SetData();//3
	a.Display();
	a.PrintPages();//6
	Book b("Amazon`s forest","Adventures",300,true,50);//2
	b.Display();
	Book c(b);//4
	c.Display();
	CompareBookPages(a,b);//5
	Book arr1[3]={Book("Money","Finances",100,true,18),//7
				Book("Space modules","Science",400,true,80),
				Book("Android systems","High-Tech",800,true,140)};
				
	const int N=2;
	Book arr2[N];//8
	cout<<endl;
	for(int i=0;i<N;i++)
	{
		 arr2[i].SetData();
	}
	for(int i=0;i<N;i++)
	{
		 arr2[i].Display();
	}
	Book* pb=&b;//9
	pb->Display();
	
	Book &pa=a;//10
	pa.Display();
	
	Book d;//11
	Book* pd=&d;
	pd->SetData();
	pd->Display();
	
	Book e;//12
	Book &re=e;
	re.SetData();
	re.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