I have to do a library system using OOP . I have a class "entry" which has some data like title, year etc. and 2 derived classes, one "book" that has author and another one "magazine" which has number. The "library" class has a vector<entry> vector. I want to do a function that searches by author name but I can't compare the string I'm searching for with the string in the author box like:
1 2 3 4 5 6
for(i=0;i<this->vector.size();i++
{
compare string with this->vector[i].author;
}
because the vector has "entry" type info and entry doesn't have author. how should I do it?
You mean to say that the 'library' class has a vector of "std::vector<*entry>" right? Otherwise how would this work?
Well the lazy solution would be to have the books "author" attribute and the magazines "number" attribute occupy the same string variable in the base class. I don't have any suggestions as to what you would call this variable, and there is a very real chance that it would annoy your instructor, but it does solve your problem in a simple and direct way.
the problem is that the number of the magazine is an int opposed to author which is a string ; and the magazine class also has another int which is the publication frequency so Book has a string and Magazin 2 integers in addition to Entry. i guess the solution would be to add these 3 variables in the entry class but then why would I need the derived classes for? I thought about another solution for the library class which instead of using a vector for entries uses a vector for books and one for magazines; i would like to have them both in one vector though so I guess I'll use the first solution
I disagree, having three variables in the base class is NOT the solution here. Numbers can easily be represented as strings and unless you're doing some kind of mathematical operation on them there is no reason for them not to be.
I'm sorry if you read my deleted post, without any context it took me a minute to understand what you are suggesting. Something like that would work I suppose, but it would add another step to the user input because you would have to tell the program what datatype to save your input as, a number or a string. I still think making the magazine number into a string would streamline this part, but to each their own.