Создание объекта из текстового файла [закрыто]C++

Программы на C++. Форум разработчиков
Ответить
Anonymous
 Создание объекта из текстового файла [закрыто]

Сообщение Anonymous »


I'm quite new to C++, and I have a question regarding a better way to write something I'm trying to do. I have a problem where I have text file where each line is and attribute of and object, structured something like this:

book 1984 An award winning dystopian novel 100-345 33 328 George Orwell dystopian Secker & Warburg The "Book" class is inherited from and "Inventory" class. Both look like this.

class Inventory { protected: std::string name; std::string itemDescription; std::string itemID; int stockNumber; std::string author; public: Inventory(); std::string getName() const; std::string getItemDescription() const; std::string getItemID() const; int getStockNumber() const; void setName(std::string name); void setItemDescription(std::string itemDescription); void setItemID(std::string itemID); void setStockNumber(int stockNumber); }; class Book : public Inventory{ private: int pageNumber; std::string author; std::string genre; std::string publisher; public: Book(); Book(std::string name, std::string itemDescription, std::string itemID, int stockNumber, int pageNumber, std::string author, std::string genre, std::string publisher); int getPageNumber() const; std::string getAuthor() const; std::string getGenre() const; std::string getPublisher() const; void setPageNumber(int pageNumber); void setAuthor(std::string author); void setGenre(std::string genre); void setPublisher(std::string publisher); }; I have a piece of code that works, but I feel like it is not the best way to accomplish what I'm doing:

Book buildBook(ifstream &file){ string name; string itemDescription; string itemID; string stockNumber; string pageNumber; string author; string genre; string publisher; getline(file, name); getline(file, itemDescription); getline(file, itemID); getline(file, stockNumber); getline(file, pageNumber); getline(file, author); getline(file, genre); getline(file, publisher); return {name, itemDescription, itemID, stoi(stockNumber), stoi(pageNumber), author, genre, publisher}; } My question is if there is a better way to rewrite this. Seeing so many variables declared all at once feels very wrong


Источник: https://stackoverflow.com/questions/781 ... -text-file
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «C++»