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.
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.
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?
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.
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?
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.
However, this cannot be use because the specific type has not yet been created. Unlike the one shown above.Code:for(int x = 0; x < student.size(); x++) { cout << " ============ Student " << x+1 << " Details ============"; student[x]->printStudentDetails(); student[x]->print(); cout << endl; }
Last edited by Alexius Lim; 12-02-2013 at 08:39 AM.
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?
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?