Prasanth Madhavan
Prasanth Madhavan

Reputation: 13309

class objects in c++

i need to write a program in which main() would read a file(containing some predefined instructions) and create classes for each line and if a class object was already created, create a new class object.. something like

main()
{
     read file;
     save to a vector;

     for(i < vectorsize; i++)
          if(vector[i]== "book")
                 if(book b was already created) 
                       book c; 
                 else book b;
}

Upvotes: 0

Views: 139

Answers (2)

Rudi
Rudi

Reputation: 19940

You might use a std::map to store the created books. A map is a key->value store where you can adress the content by a own defined key.

typedef std::map<std::string, Book> BookMap;
int main()
{
     read file;
     save to a vector;
     BookMap books;

     for(i < vectorsize; i++)
          if(vector[i]== "book")
                 BookMap::const_iterator alreadyCreatedBook(books.find(b.name));
                  // When there is no book in the map, the map returns it's end() element
                 if(books.end() != createdBook)
                       alreadyCreatedBook->second; 
                 else
                     books[b.name] = b;
}

Upvotes: 1

Hans Olsson
Hans Olsson

Reputation: 55001

Rather than having a vector you might want look at a std::map where you can have the book name be the key and the actual book be the value. That way you can find the book you're looking for very easily.

Upvotes: 1

Related Questions