Thread: creating class instance from a file

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    25

    creating class instance from a file

    Keeping in mind that I'm a beginning programmer, I have a question about a program I've been writing for practice, and to hopefully learn some new insights on programming technique.

    Please also remember that these are concepts I've come up with entirely on my own, and if they seem a little weak, please don't hesitate to suggest a better way of doing something.

    It's going to be a console app that uses two classes: list, and contact.
    Each list will have a name, and a vector of contacts. The class contact would consist of multiple strings to store the first and last names, e-mail, and phone number, etc.. I want to be able to perform various operations on the list, like sorting, editing or deleteing contacts on the list.

    To perform an operation on a list however, you must first load it. This would consist of creating an instance of the class list, and it's contacts, depending on what the program reads from a file which would contain the list, and it's contacts.

    I was wondering how on earth I could 'load' a list. Reading in the file probably wouldn't be much of a problem once I get rolling, but I'm a little confused on how to refer to the loaded list. Should I create a global pointer to a list that starts out blank? ('Blank' perhaps represented as a boolean variable so that the program will be able to tell if you loaded a list, instead of trying to preform operations on a list that hasn't yet been created?)

    Another question I had (and I'm pretty sure it has to do with dynamic memory allocation) was:

    How can you create a variable, but let the user choose the identifier?

    If that doesn't make any sense to you, then it probably doesn't have an answer.

    Thanks for your time.

  2. #2
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    I would suggest that you write a member function for List called load(), perhaps takes a string or something. All it would do is, read the first line into the name, then keep reading entries into the vector of contacts until you hit the end of the file.

    >>how to refer to the loaded list.
    I don't see what's the problem here. Just have an object of the list, and create accessor functions and manipulating functions for it. Perhaps I'm misunderstanding you?
    Code:
    List l;
    if(l.isLoaded() != true)
       l.load("classlist.txt");
    
    l.sort(BY_LASTNAME, SORT_DESCENDING);
    Contact& c = l.find(BY_FIRSTNAME, "Chelsey");
    std::cout << "Chelsey\'s last name is: " << c.lastName;
    l.erase(&c);
    l.studentAt(0).email = "[email protected]";
    >>How can you create a variable, but let the user choose the identifier?
    While this question has been asked before, I don't blame you for asking I'll give you a hint: the closest you'll get is probably by using a std::map. I'll leave the research to you though.

    Hope this helps
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. #include header files or .cpp files?
    By DoctorX in forum C++ Programming
    Replies: 3
    Last Post: 12-23-2006, 12:21 PM
  2. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  3. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM