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.
#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;
}
Comments
Leave a comment