I am working on a project which involves polymorphism and inheritance.
lets assume that the hierarchy of the classes used in the project are:
Media ----> Book ----> MediaRegistry
and the declaration of each class are as follows: (the classes have been narrowed down to the members which I have problem)
class Media
{
public:
Media();
Media(int id, string content);
virtual void input(istream& in) = 0;
friend istream& operator>>(istream& in, Media& media);
protected:
string _mediaTitle;
int _id;
};
with:
istream& operator>>(istream& in, Media& media)
{
media.input(in);
return in;
}
and:
class Book:public Media // Inherits from abstract base class Media
{
public:
Book();
Book(int id, string title, int nrOfPages); // Constructor
void input(istream& in);
private:
int _nrOfPages;
};
with:
void Book::input(istream& in)
{
in >> _id >> _mediaTitle >> _nrOfPages;
}
and:
class MediaRegistry
{
public:
MediaRegistry();
int addMedia(Media*);
int loadMedia();
private:
static const int MAX = 10;
//Media* _pMedias[MAX]; // Vector for MAX numbers of Media pointers
std::vector<Media*> _pMedias;
int _nrOfMedias; // _nrOfMedias == MAX when registry is full
ifstream inFile;
ofstream outFile;
string path = ".\\data.txt";
};
with:
int MediaRegistry::loadMedia()
{
inFile.open(path.c_str());
while (!inFile.eof())
{
int mediaType = 0;
inFile >> mediaType;
if (inFile.eof())
break;
Media* media = nullptr;
media = new Book();
inFile >> *media;
addMedia(media);
}
inFile.close();
return 0;
}
Now in the data.txt
, as title of the media, if I have space between the words (i.e, "The Clown" in stead of e.g "the-Clown"), the program encounters a problem in this member function istream& operator>>(istream& in, Media& media)
. I cannot understand, however, I debugged it many times to track the problem. In fact, I want my program could be able to get a string with space in it, but it doesn't do that now??