Help loading class objects from file input

Hello. I'm fairly new to C++ programming still, and I'm struggling loading a class (and it's two derived classes) from an input file. I'm trying to create an array of class objects (and derived objects) and then load them based upon what is in the file.

Main class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
 class Book
{
    protected:
        string sTitle;
        string sAuthor;
        string sPublisher;
        string sISBN;
        int iCopyrightYear;
        double dPrice;
        int iCopies;

    public:
        //Default Constructor
        Book();

        //Destructor
        virtual ~Book();

        //Overload Constructor
        Book(string m_Title, string m_Author, string m_Publisher, string m_ISBN, int m_CopyrightYear, double m_Price, int m_Copies);

        //accessor functions
        string getTitle() const;
        string getAuthor() const;
        string getPublisher() const;
        string getISBN() const;
        int getCopyrightYear() const;
        double getPrice() const;
        int getCopies() const;
        virtual void display();



        //mutator functions
        void setTitle(string);
        void setAuthor(string);
        void setPublisher(string);
        void setISBN(string);
        void setCopyrightYear(int);
        void setPrice(double);
        void setCopies(int);


Derived classes:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
class PrintedBook : public Book
{
    protected:
        int iNumPages;

    public:

        //default constructor
        PrintedBook();

        //destructor
        ~PrintedBook();

        //overloaded constructor
        PrintedBook(string sTitle, string sAuthor, string sPublisher, string sISBN, int iCopyrightYear, double dPrice, int iCopies, int iNumPages);


        //accessors
        void display();
        int getNumPages() const;


        //mutators
        void setNumPages(int);


};

class AudioBook : public Book
{
    protected:
        int iMinutes;
        string sNarrator;

    public:

        //default constructor
        AudioBook();

        //destructor
        ~AudioBook();

        //overload constructor
        AudioBook(string sTitle, string sAuthor, string sPublisher, string sISBN, int iCopyrightYear, double dPrice, int iCopies, int iMinutes, string sNarrator);

        //accessors
        int getMinutes() const;
        string getNarrator() const;
        void display() const;

        //mutators
        void setMinutes(int);
        void setNarrator(string);


};


The input file is as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
P
The Lady and the Unicorn
Chevalier, Tracy
E P Dutton
2003
0525947671
32.95
25
256
A
The Lady and the Unicorn
Chevalier, Tracy
Penguin Audiobooks
2003
0525947671
24.47
15
240
Narator, John
P
365 Simple Science Experiments With Everyday Materials
Churchill, E. Richard
Black Dog & Leventhal Pub
1997
1884822673
8.88
20
320


Basically if the getline get's a P it's supposed to create a new object using the PrintedBook derived class and then store it to an array, if the getline get's to the "A" it's supposed to create an AudioBook object.

What I'm struggling with is writing something to parse the file line by line and create the objects on the fly and then store them into the array.

Any help would be greatly appreciated!



Last edited on
How are you reading the files?

try

https://gist.github.com/skymarionsky/11131028
Last edited on
I was processing it line by line, I like you code posted to do so, however how do I assigned the lines to the elements of the classes and then put it into a vector or an array?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
ifstream in_stream;
    in_stream.open(working_file.c_str());
    //get first line to determine if it's a P or A.
    getline(in_stream,type);
    in_stream.close();
    in_stream.open(working_file.c_str());
    //used to index array
    int j =1;

    while (in_stream)
    {
        getline(in_stream, type);
        if (type == "P")
        {
            for(int k=1; k<2; k++)
            {
                getline(in_stream, title);
                PrintedBook pb1;
                arrBooks[0] = &pb1;
                arrBooks[0]->setTitle(title);
                getline(in_stream,author);
                arrBooks[0]->setAuthor(author);
                getline(in_stream,publisher);
                arrBooks[0]->setPublisher(publisher);
                getline(in_stream,syear);
                iyear = atoi(syear.c_str());
                arrBooks[0]->setCopyrightYear(iyear);
                getline(in_stream,isbn);
                arrBooks[0]->setISBN(isbn);
                getline(in_stream,sprice);
                dprice=atof(sprice.c_str());
                arrBooks[0]->setPrice(dprice);
                getline(in_stream,spages);
                ipages=atoi(spages.c_str());
                arrBooks[0]->setNumPages(ipages);
                arrBooks[0]->display();
            }

        }
        if (type == "A")
        {
            for(int k=1; k<10; k++)
            {
                getline(in_stream, title);
                cout << endl << endl << j<< ": " << type << ": " << title << endl << endl;
            }
            cout << endl << endl;

        }
        j++;
    }
    in_stream.close();
}


Again, the problem I'm having is figuring out how to load the class objects on the fly when I read in the file and come across either an A (audio book) or P (paperbook) record.

I modified your code it is still incomplete hope you can finish it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
// ConsoleApplication1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <string>

using namespace std;

class Book
{
protected:
	string sTitle;
	string sAuthor;
	string sPublisher;
	string sISBN;
	int iCopyrightYear;
	double dPrice;
	int iCopies;

public:
	//Default Constructor
	Book();

	//Destructor
	//virtual ~Book();

	//Overload Constructor
	Book(string m_Title, string m_Author, string m_Publisher, string m_ISBN, int m_CopyrightYear, double m_Price, int m_Copies);

	//accessor functions
	string getTitle() const;
	string getAuthor() const;
	string getPublisher() const;
	string getISBN() const;
	int getCopyrightYear() const;
	double getPrice() const;
	int getCopies() const;
	//virtual void display();
	
	//mutator functions
	void setTitle(string title);
	void setAuthor(string author);
	void setPublisher(string publisher);
	//void setISBN(string);
	//void setCopyrightYear(int);
	//void setPrice(double);
	//void setCopies(int);
};

Book::Book()
{

}
void Book::setTitle(string title)
{
	sTitle = title;
}

void Book::setAuthor(string author)
{
	sAuthor = author;
}

void Book::setPublisher(string publisher)
{
	sPublisher = publisher;
}

class PrintedBook : public Book
{
protected:
	int iNumPages;

public:

	//default constructor
	PrintedBook();

	//destructor
	~PrintedBook();

	//overloaded constructor
	PrintedBook(string sTitle, string sAuthor, string sPublisher, string sISBN, int iCopyrightYear, double dPrice, int iCopies, int iNumPages);


	//accessors
	void display();
	int getNumPages() const;


	//mutators
	void setNumPages(int);


};


class AudioBook : public Book
{
protected:
	int iMinutes;
	string sNarrator;

public:

	//default constructor
	AudioBook();

	//destructor
	~AudioBook();

	//overload constructor
	AudioBook(string sTitle, string sAuthor, string sPublisher, string sISBN, int iCopyrightYear, double dPrice, int iCopies, int iMinutes, string sNarrator);

	//accessors
	int getMinutes() const;
	string getNarrator() const;
	void display() const;

	//mutators
	void setMinutes(int);
	void setNarrator(string);


};

PrintedBook::PrintedBook()
{

}

PrintedBook::~PrintedBook()
{

}

AudioBook::AudioBook()
{

}

AudioBook::~AudioBook()
{

}

string Book::getAuthor() const
{
	return sAuthor;
}


int main()
{
	ifstream in_stream;
	in_stream.open("C:\\Temp\\book.txt");	
	
	string type;
	string title;
	string author;
	string publisher;
	string syear;
	string iyear;
	string isbn;
	string sprice;
	string dprice;
	string spages;
	string ipages;
	
	// vector that stores printed book 
	vector<PrintedBook> pbookset;
	//vector that stores audio book
	vector<AudioBook> abookset;

	if (in_stream.is_open())
	{		
		while (in_stream.good())
		{	
			getline(in_stream, type);			
			//std::cout << type << std::endl;
			
			if (type == "P")
			{
				PrintedBook pb1;				
				getline(in_stream, title);
				pb1.setTitle(title);
				getline(in_stream, author);
				pb1.setAuthor(author);
				getline(in_stream, publisher);
				pb1.setPublisher(publisher);

				//std::cout << "printed book" << std::endl;
				//std::cout << title << std::endl;
				//std::cout << author << std::endl;
				//std::cout << publisher << std::endl;

				// finish the rest.
				pbookset.push_back(pb1);
			}

			if (type == "A")
			{
				AudioBook ab1;
				getline(in_stream, title);
				ab1.setTitle(title);
				getline(in_stream, author);
				ab1.setAuthor(author);
				getline(in_stream, publisher);
				ab1.setPublisher(publisher);

				// finish the rest

				//std::cout << "audio book" << std::endl;
				//std::cout << title << std::endl;
				//std::cout << author << std::endl;
				//std::cout << publisher << std::endl;

				abookset.push_back(ab1);
			}
			
		}
		in_stream.close();
	}
	else
	{
		std::cout << "unable to open file" << std::endl << std::endl;
	}

	std::cout << pbookset[0].getAuthor() << std::endl;

	std::cout << abookset.front().getAuthor() << std::endl;

	
	int x;
	cin >> x;

	return 0;
}
Last edited on
Hello rafae11! Am I understanding this correctly, that each time through a new object is created such as pb1 or ab1 and then that is pushed onto the vector. Can objects be named the same after they are pushed onto the vector? Is the object destroyed after it's pushed into the vector?

Thanks.
yes you are creating another instance of the class Book to store your data in.

then you are storing the objects in a container called pbookset.

you can access the objects like you would an array by index
or you can use iterators

for example you have 5 objects
you can access each of them by using pbookset[0] , pbookset[1] etc.

the object will continue to exist for the duration of the program or until your delete it.
Awesome, and to be a total n00b how would I pass that vector into a function if I wanted to move the loading of the vectors into a separate function instead of leaving it in main?

you pass it by reference.

example

function process_vector(vector<PrintedBook> &pbookset)

you will be able to modify the contents of pbookset inside the process_vector function.

http://www.cplusplus.com/forum/general/47797/
Topic archived. No new replies allowed.