Answer to Question #188033 in C++ for Garima

Question #188033

We want to store the information of different Books: Author name, Book name, Publications and Price. Create a class named BOOK with appropriate data members and member functions which store all information into a text file.


1
Expert's answer
2021-05-01T19:47:25-0400
#include <iostream>
#include <string>
#include <fstream>
 
using namespace std;
 
class Book
{
public:
	Book(string authorName, string bookName, int publications, double price);
	string GetAuthor() { return authorName; }
	string GetBook() { return bookName; };
	int GetPublications() { return publications; }
	double GetPrica() { return price; }
	void StoreData();
 
private:
	string authorName;
	string bookName;
	int publications;
	double price;
};
 
Book::Book(string authorName, string bookName, int publications, double price)
{
	this->authorName = authorName;
	this->bookName = bookName;
	this->price = price;
	this->publications = publications;
}
 
void Book::StoreData()
{
	ofstream fout;
	fout.open("Books.txt", ios::app);
	if (!fout)
	{
		cout << "File not opened!" << endl;
		return;
	}
	fout << authorName << " " << bookName << " " << publications << " " << price << endl;
	fout.close();
	return;
}
 
int main()
{
	Book b("Mike", "Old man", 1000, 24.95);
	b.StoreData();
	Book a("Kate", "New man", 1500, 14.95);
	a.StoreData();
	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