Thread: File IO with polymorphism

  1. #1
    Registered User
    Join Date
    May 2013
    Posts
    59

    File IO with polymorphism

    How do i read inputs from file with polymorphism? How is the program going to detect if its a (lets say) a dog or a cat? and stores the information accordingly? Perhaps show me a brief example? Thanks in advanced.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    I would imagine that the file will contain some way to indicate whether a record represents a dog or a cat. do you know how to read from files? do you understand polymorphism?
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  3. #3
    Registered User
    Join Date
    May 2013
    Posts
    59
    I know how to read from file and I know polymorphism but i do not know how to implement them together.
    this is my virtual function readFromFile for localStudent.

    Code:
    void clsLocalStudent::readFromFile(vector <clsStudent*> &s)
    {
       string name, IC_no,state, gender, accNumb, accName, nationality, progName, progCode;
       int accID, times;
       float GPA, discountedFees, principle, rate, years, CI, C_Amount, progFees;
       clsDate DOB, accStart, accDOB ;
       clsLocalStudent *stud;
       stud = new clsLocalStudent();
    
    
       ifstream in(filename);
       if(in)
       {
          in >> name ;
          in >> DOB;
          in >> gender ;
          in >> nationality;
          in >> GPA ;
          in >> progName ;
          in >> progCode ;
          in >> progFees;
          in >> accNumb;
          in >> accName;
          in >> accID ;
          in >> accDOB ;
          in >> accStart;
          in >> principle ;
          in >> rate;
          in >> years ;
          in >> times;
          in >> CI ;
          in >> C_Amount;
          in >> discountedFees ;
          in >> IC_no;
          in >> state;
          in.close();
       }
       else
          cerr << "\nCannot open " << filename << " for reading" << endl;
    
    
        clsUniversityProgram program(progName, progCode, progFees);
        clsAccount objAccount(accNumb,name,accID, accDOB, accStart, principle, rate, years, times);
        stud->setName(name);
        stud->setDateOfBirth(DOB);
        stud->setGender(gender);
        stud->setNationality(nationality);
        stud->setGPA(GPA);
        stud->setProgram(&program);
        stud->setAccounts(objAccount);
        stud->setIC(IC_no);
        stud->setState(state);
    
    
    
    
        s.push_back(stud);
    
    
    }
    Last edited by Alexius Lim; 12-02-2013 at 08:26 AM.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    my guess is that you're going to have a base class called animal, and derived classes called cat and dog, and when you read from the file, you will determine whether it's a cat or dog and use new to create the appropriate type, and assign to a pointer of animal type. I'm not going to write the code for you, but I'll be happy to help you out if you put some effort into it and ask more specific questions when you get stuck on something.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  5. #5
    Registered User
    Join Date
    May 2013
    Posts
    59
    Well how do i determine if its a cat or a dog?
    my print() function here is virtual. So in this case, the program determines itself which class it's in and calls the correct print function.
    Code:
                for(int x = 0; x < student.size(); x++)
                {
                    cout << " ============ Student " << x+1 << " Details ============";
                    student[x]->printStudentDetails();
                    student[x]->print();
                    cout << endl;
                }
    However, this cannot be use because the specific type has not yet been created. Unlike the one shown above.
    Last edited by Alexius Lim; 12-02-2013 at 08:39 AM.

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by Alexius Lim View Post
    Well how do i determine if its a cat or a dog?
    having not seen the format of the file, I have no idea. if I were developing this system, I would have the first field of the record indicate what type the record is.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Elkvis View Post
    having not seen the format of the file, I have no idea. if I were developing this system, I would have the first field of the record indicate what type the record is.
    Or to maybe put it another way: if you can't tell the difference from the file, then do you actually care which it is?

  8. #8
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    if you positively can't tell, then whether you care or not is irrelevant.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Polymorphism
    By tinu73 in forum C++ Programming
    Replies: 5
    Last Post: 11-06-2012, 08:50 AM
  2. Polymorphism
    By tasha302 in forum C++ Programming
    Replies: 5
    Last Post: 11-23-2006, 01:25 PM
  3. polymorphism
    By slaveofthenet in forum C++ Programming
    Replies: 15
    Last Post: 07-10-2003, 11:50 AM
  4. what is polymorphism and how can I use it
    By Tonyukuk in forum C++ Programming
    Replies: 3
    Last Post: 01-20-2003, 04:23 PM
  5. what is polymorphism?
    By newuser in forum C++ Programming
    Replies: 1
    Last Post: 07-14-2002, 09:55 AM

Tags for this Thread